在编辑数据表值期间,不会在屏幕和侦听器方法中更新这些值。监听器方法:
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.
答案 0 :(得分:5)
您的bean必须实现Serializable接口。这就是错误信息所说的
另一个问题是你总是通过getEList返回新的List。您可以在某种具有注释@PostConstruct
。
@PostConstruct
public void init() {
//code from getElist()...
}
你的getter shuld是经典的getter,它只返回eList。 这样,您可以在创建视图时加载列表。在您的情况下,每次更新都会再次加载列表,这是不好的做法。不要在getter中获取数据。编辑不适合您,因为您每次都会返回一个新列表而丢失更新的值。
答案 1 :(得分:1)
在你的
中<p:ajax event="rowEdit" listener="#{employees.onEdit}"
update=":form:messages" />
您只更新消息而不是数据本身。试试这个:update=":form:messages, :form:elist1"
。
这对我有用
答案 2 :(得分:0)
我想你也是:
rowKey属性选项需要的工作量最少。例如:
<p:dataTable var="e" value="#{employees.eList}" id="elist1" rowKey="#{e.name}"
editable="true">
其中e.name是数据表中每个项目的标识符。它也应该是该对象的唯一值。 (比如id)
答案 3 :(得分:0)
这可能对某人有所帮助。我有一个类似的问题,我在互联网上尝试了所有可能的事情。最终我放弃了,但后来偶然修好了。问题是我有多个&lt; H:形式&GT;在我的.xhtml中使用,并且由于某种原因没有传播更改。 希望它对某人有帮助。