在编辑数据表的值时,这些值在屏幕上以及监听器方法中都没有更新。监听器方法:
public void onEdit(RowEditEvent event) throws ClassNotFoundException, SQLException
{
Employee e=(Employee) event.getObject();
name=e.getName();
department=e.getDepartment();
salary=e.getSalary();
place=e.getPlace();
......
..... update query
}
我只得到了所选对象及其值,但没有更新后的值。
这是我的数据表 XHTML:
<h:form id="form">
<p:growl id="messages" showDetail="true" />
<p:dataTable var="e" value="#{employees.eList}" id="elist1"
editable="true">
<f:facet name="header">
In-Cell Editing
</f:facet>
<p:ajax event="rowEdit" listener="#{employees.onEdit}"
update=":form:messages" />
<p:ajax event="rowEditCancel" listener="#{employees.onCancel}"
update=":form:messages" />
<p:column headerText="name" style="width:30%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{e.name}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{e.name}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="department" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{e.department}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{e.department}" style="width:100%"
label="department" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="salary" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{e.salary}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{e.salary}" style="width:100%" label="salary" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="place" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{e.place}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{e.place}" style="width:100%" label="place" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:6%">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
这是我的托管bean:
@ManagedBean(name = "employees")
@ViewScoped
public class UserData
{
// properties
private String name;
private String department;
private String salary;
private String place;
private ArrayList<Employee> eList;
private Employee selectedemp;
private void clear()
{
name="";
department="";
salary="";
place="";
}
//returning all employees
public ArrayList<Employee> geteList() throws ClassNotFoundException, SQLException {
Connection conn=DbConnection.connectFunc();
Statement stmt=conn.createStatement();
String sql="select * from employee";
ResultSet rs=stmt.executeQuery(sql);
eList=new ArrayList<Employee>();
while(rs.next())
{
Employee emp=new Employee();
emp.setName(rs.getString(2));
emp.setDepartment(rs.getString(3));
emp.setSalary(rs.getString(4));
emp.setPlace(rs.getString(5));
eList.add(emp);
}
return eList;
}
public void onEdit(RowEditEvent event) throws ClassNotFoundException, SQLException
{
Employee e=(Employee) event.getObject();
name=e.getName();
System.out.println(name);
department=e.getDepartment();
System.out.println(department);
salary=e.getSalary();
System.out.println(salary);
place=e.getPlace();
System.out.println(place);
Connection conn=DbConnection.connectFunc();
Statement stmt=conn.createStatement();
String sql="update employee set department='"+department+"',salary='"+salary+"',place='"+place+"'where name='"+name+"'";
int i=stmt.executeUpdate(sql);
if(i!=0)
{
FacesContext context=FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("updated successfully"));
}
conn.close();
}
//onCancel
public void onCancel(RowEditEvent event)
{
}
...setter and getters
如果我使用视图作用域,将会出现以下异常:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.io.NotSerializableException: com.solv.datatable.UserData
java.io.ObjectOutputStream.writeObject0(Unknown Source)
java.io.ObjectOutputStream.writeObject(Unknown Source)
java.util.HashMap.writeObject(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
java.io.ObjectOutputStream.writeSerialData(Unknown Source)
java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
java.io.ObjectOutputStream.writeObject0(Unknown Source)
java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
java.io.ObjectOutputStream.writeSerialData(Unknown Source)
java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
java.io.ObjectOutputStream.writeObject0(Unknown Source)
java.io.ObjectOutputStream.writeArray(Unknown Source)
java.io.ObjectOutputStream.writeObject0(Unknown Source)
java.io.ObjectOutputStream.writeObject(Unknown Source)
java.util.HashMap.writeObject(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
java.io.ObjectOutputStream.writeSerialData(Unknown Source)
java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
java.io.ObjectOutputStream.writeObject0(Unknown Source)
java.io.ObjectOutputStream.writeObject(Unknown Source)
com.sun.faces.renderkit.ClientSideStateHelper.doWriteState(ClientSideStateHelper.java:325)
com.sun.faces.renderkit.ClientSideStateHelper.writeState(ClientSideStateHelper.java:173)
com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:122)
com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:166)
com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:419)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.26 logs.