【发布时间】:2017-05-23 13:41:13
【问题描述】:
我在这里发布 MCVE 是因为我对 JSF 的行为感到困惑。 我有 index.xhtml:
<h:form id="entityForm" enctype="multipart/form-data" styleClass="entityForm">
<p:dataTable id="dataTableId1" value="#{testBean.testEntityList}" var="tent" rowKey="#{tent.id}">
<p:column style="width:16px">
<p:rowToggler/>
</p:column>
<p:column>
<h:outputText value="#{tent.id}"/>
</p:column>
<p:column>
<h:outputText value="#{tent.name}"/>
</p:column>
<p:rowExpansion>
<p:selectBooleanCheckbox value="#{tent.something}"/>
<p:inputText value="#{tent.strNextToSomethingCheckBox}" />
</p:rowExpansion>
</p:dataTable>
<p:commandButton id="button1" ajax="true" value="Do something" update="entityForm"/>
</h:form>
豆码:
@ManagedBean(name = "testBean")
@ViewScoped
public class TestBean implements Serializable {
private static final Logger LOG = Logger.getLogger(TestBean.class);
private List<TestEntity> testEntityList;
@PostConstruct
public void init() {
LOG.debug("init call");
testEntityList = new ArrayList<>();
testEntityList.add(new TestEntity(1L, "Entry 1", true, "Value 1 str"));
testEntityList.add(new TestEntity(2L, "Entry 2", false, "Value 2 str"));
testEntityList.add(new TestEntity(3L, "Entry 3", false, "Value 3 str"));
}
public class TestEntity {
Long id;
String name;
Boolean something;
String strNextToSomethingCheckBox;
public TestEntity(Long id, String name, Boolean something, String strNextToSomethingCheckBox) {
this.id = id;
this.name = name;
this.something = something;
this.strNextToSomethingCheckBox = strNextToSomethingCheckBox;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getSomething() {
return something;
}
public void setSomething(Boolean something) {
LOG.debug("setSomething call");
this.something = something;
}
public String getStrNextToSomethingCheckBox() {
return strNextToSomethingCheckBox;
}
public void setStrNextToSomethingCheckBox(String strNextToSomethingCheckBox) {
LOG.debug("setStrNextToSomethingCheckBox call");
this.strNextToSomethingCheckBox = strNextToSomethingCheckBox;
}
}
public List<TestEntity> getTestEntityList() {
return testEntityList;
}
public void setTestEntityList(List<TestEntity> testEntityList) {
this.testEntityList = testEntityList;
}
}
场景:当你打开页面时,展开第一行,你会看到复选框状态为选中,当你按下按钮时,表单被更新并且复选框仍然被选中(你需要再次展开行才能看到)
问题场景:打开页面,不展开任何行,点击按钮,表单更新后,展开第一行,你会看到值从true设置为false
问题:为什么值是从真到假?这是错误还是预期的行为?我怎样才能避免这种情况? 谢谢。
【问题讨论】:
-
如果你从
h:form中删除这个:enctype="multipart/form-data"呢? -
@Kukeltje 相同,值丢失/重置为 false
标签: ajax primefaces jsf-2