gpt4 book ai didi

java - 更改 PrimeFaces 中每页行值后数据表的奇怪行为

转载 作者:行者123 更新时间:2023-12-02 00:32:33 24 4
gpt4 key购买 nike

当我使用 PrimeFaces 的表格就地编辑功能编辑行时,更改似乎没有应用。

这是我的代码:

properties.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">

<f:event type="javax.faces.event.PreRenderViewEvent" listener="${propertyManagedBean.preRenderView}"/>

<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Properties</title>
</h:head>

<h:body>
<div id="content" class="left_content">
<h:form>
<p:dataTable id="tbl" var="property" value="#{propertyManagedBean.properties}" paginator="true" rows="10" rowsPerPageTemplate="5,10,20">

<p:ajax event="rowEdit" update="@this" listener="#{propertyManagedBean.propertyRowEdit}"/>

<f:facet name="header">
Properties Manager
</f:facet>

<p:column headerText="Key" >
<h:outputText value="#{property.propKey}" />
</p:column>

<p:column headerText="Value">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{property.propValue}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{property.propValue}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>

<p:column headerText="Options" style="width:15px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</div>
</h:body>
</html>

PropertyManagedBean.java

@ManagedBean
@SessionScoped
public class PropertyManagedBean {

private PropertyJpaController controller;
private List<Property> properties;

/** Creates a new instance of propertyManagedBean */
public PropertyManagedBean() {
controller = new PropertyJpaController();
extractProperties();
}

public void propertyRowEdit(RowEditEvent event) {
Property prop = (Property) event.getObject();
try {
controller.update(prop);
} catch (Exception ex) {
//TODO: Customize work with exceptions
}
extractProperties();
}

public List<Property> getProperties() {
return properties;
}

public void preRenderView() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
}

private void extractProperties() {
properties = controller.findPropertyEntities();
}
}

编辑页面上的一行属性后出现问题。更改此行后,我得到表中属性的旧值。

最佳答案

我的猜测是 controller.update(prop) 中发生了您未显示的异常,因此更新实际上并未发生。

如果我使用模拟服务快速测试您的代码,它会按预期工作。

小面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
>

<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Properties</title>
</h:head>

<h:body>
<h:form>
<p:dataTable id="tbl" value="#{propertyManagedBean.properties}" var="property" paginator="true" rows="10" rowsPerPageTemplate="5,10,20">

<p:ajax event="rowEdit" update="@this" listener="#{propertyManagedBean.propertyRowEdit}"/>

<f:facet name="header">
Properties Manager
</f:facet>

<p:column headerText="Key" >
#{property.propKey}
</p:column>

<p:column headerText="Value">
<p:cellEditor>
<f:facet name="output">
#{property.propValue}
</f:facet>
<f:facet name="input">
<p:inputText value="#{property.propValue}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>

<p:column headerText="Options" style="width:15px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>

支持 bean:

@ManagedBean
@SessionScoped
public class PropertyManagedBean {

private PropertyJpaController controller;
private List<Property> properties;

public PropertyManagedBean() {
controller = new PropertyJpaController();
extractProperties();
}

public void propertyRowEdit(RowEditEvent event) {
Property property = (Property) event.getObject();
try {
controller.update(property);
} catch (Exception ex) {
// TODO: Customize work with exceptions
}
extractProperties();
}

public List<Property> getProperties() {
return properties;
}

private void extractProperties() {
properties = controller.findPropertyEntities();
}
}

模拟服务:

public class PropertyJpaController {

private List<Property> properties = new ArrayList<Property>();

{
properties.add(new Property("KeyA", "ValueA"));
properties.add(new Property("KeyB", "ValueB"));
}

public List<Property> findPropertyEntities() {
// Copy list and entries to prevent direct in-memory updating
List<Property> newList = new ArrayList<Property>();
for (Property property : properties) {
newList.add(new Property(property.getPropKey(), property.getPropValue()));
}

return newList;
}

public void update(Property updatedProperty) {
for (Property property : properties) {
if (property.getPropKey().equals(updatedProperty.getPropKey())) {
property.setPropValue(updatedProperty.getPropValue());
}
}
}
}

模拟模型:

public class Property {

private String propKey;
private String propValue;

public Property(String propKey, String propValue) {
this.propKey = propKey;
this.propValue = propValue;
}

public String getPropKey() {
return propKey;
}

public void setPropKey(String propKey) {
this.propKey = propKey;
}

public String getPropValue() {
return propValue;
}

public void setPropValue(String propValue) {
this.propValue = propValue;
}
}

请注意,preRenderView 事件监听器是相当不必要的。另外,通常您不会创建支持 bean @SessionScoped,但对于模拟情况,它可以方便地测试更改是否确实“持续”。

顺便说一句,这是在 JBoss AS 7 上使用 PrimeFaces 3rc2 进行测试的。

关于java - 更改 PrimeFaces 中每页行值后数据表的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8654385/

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