gpt4 book ai didi

jsf-2 - 行编辑在数据表primefaces中不起作用

转载 作者:行者123 更新时间:2023-12-05 00:28:32 29 4
gpt4 key购买 nike

在编辑数据表值期间,屏幕和监听器方法中的值不会更新。监听器方法:

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

如果我使用 View 范围,则会发生以下异常:
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.

最佳答案

你的 bean 必须实现接口(interface) Serializable。这就是错误消息所说的

另一个问题是您总是通过 getEList 返回新列表。您可以在某种带有注释 @PostConstruct 的 init 方法中加载您的 ArayList .

@PostConstruct
public void init() {
//code from getElist()...
}

你的 setter/getter 应该是只返回 eList 的经典 setter/getter 。
通过这种方式,您可以在创建 View 时加载列表。在您的情况下,每次更新都会再次加载列表,这是不好的做法。不要在 getter 中获取数据。编辑对您不起作用,因为您每次都返回一个新列表会丢失更新的值。

关于jsf-2 - 行编辑在数据表primefaces中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18886538/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com