【发布时间】:2014-05-03 10:53:16
【问题描述】:
给定以下 XHTML 代码,其中一个 <p:inputText> 和一个 <p:dataTable> 只有两列。
<p:remoteCommand name="updateTable" update="dataTable"/>
<p:panel id="panel">
<p:inputText id="txtValue" value="#{testManagedBean.txtValue}"
required="true"/>
<p:message for="txtValue" showSummary="false"/>
<p:commandButton actionListener="#{testManagedBean.submitAction}"
oncomplete="if(!args.validationFailed) {updateTable();}"
update="panel" value="Submit"/>
</p:panel>
<p:panel id="dataTablePanel" header="Data">
<p:dataTable id="dataTable" var="row" value="#{testManagedBean}"
lazy="true"
pageLinks="10"
editable="true"
rowsPerPageTemplate="5,10,15"
rows="10"
rowKey="#{row.catId}"
editMode="row">
<p:ajax event="rowEdit" update=":form:panel dataTable"
listener="#{testManagedBean.onRowEdit}"/>
<p:column id="id" headerText="Id">
<h:outputText value="#{row.catId}"/>
</p:column>
<p:column id="catName" headerText="Category">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.catName}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{row.catName}" label="Category">
<f:validateLength minimum="2" maximum="45"/>
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit" width="100">
<p:rowEditor/>
</p:column>
</p:dataTable>
</p:panel>
当给定的<p:commandButton>被按下时,关联的监听器submitAction()被调用,最后<p:dataTable>被<p:remoteCommand>更新只有在验证成功时。
执行此操作后,如果给定 <p:dataTable> 持有的行被更新(这反过来又通过 <p:ajax> 在 <p:dataTable> 中更新 <p:panel id="panel">。有时这是必要的),给定 <p:inputText> 在 @ 987654333@ 导致验证,其边框变为红色,表示违反了应该不发生的相关验证。
如果<p:remoteCommand> 被删除并且给定的<p:commandButton> 如下更改,
<p:commandButton actionListener="#{testManagedBean.submitAction}"
update="panel dataTable" value="Submit"/>
删除oncomplete="if(!args.validationFailed) {updateTable();}"
并且update 属性从update="panel" 更改为update="panel dataTable" 然后,当<p:dataTable> 中的一行被更新时,<p:inputText> 不会导致验证。
当<p:dataTable> 中的一行使用<p:ajax> 更新时,如何防止<p:inputText> 执行验证,而<p:panel> 又持有有问题的<p:inputText>?
<p:remoteCommand> 本身在这种情况下,不能省略。只有在没有违反验证时才需要更新<p:dataTable>。否则,即使存在验证错误,也会不必要地执行昂贵的业务服务。
关联的 JSF 托管 bean(尽管完全没有必要)。
@ManagedBean
@ViewScoped
public final class TestManagedBean extends LazyDataModel<Category> implements Serializable
{
@EJB
private final CategoryBeanLocal categoryService = null;
private String txtValue; //Getter and setter.
private static final long serialVersionUID = 1L;
@Override
public List<Category> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {
setRowCount(categoryService.rowCount().intValue());
return categoryService.getList(first, pageSize, multiSortMeta, filters);
}
public void submitAction() {
System.out.println("txtValue : " + txtValue);
txtValue = null;
}
public void onRowEdit(RowEditEvent event) {
System.out.println("onRowEdit() called.");
}
}
【问题讨论】:
标签: jsf primefaces jsf-2.2 remotecommand