Tin*_*iny 6 jsf primefaces jsf-2.2
我只准备了一个简单的购物篮示例,仅用于演示目的.
XHTML页面:
<p:dataTable id="cartDataTable" value="#{testManagedBean.qtyList}"
var="cart"
selection="#{testManagedBean.selectedQtyList}"
rowKey="#{cart.id}">
<p:column selectionMode="multiple" />
<p:column>
<h:outputText value="#{cart.id}"/>
</p:column>
<p:column>
<p:inputText value="#{cart.qty}"/>
</p:column>
</p:dataTable>
<p:commandButton value="Delete" process="@this cartDataTable"
actionListener="#{testManagedBean.delete}"/>
Run Code Online (Sandbox Code Playgroud)
托管bean:
@ManagedBean
@SessionScoped
public final class TestManagedBean implements Serializable
{
private List<Cart>qtyList; //Getter only.
private List<Cart>selectedQtyList; //Getter and setter.
private static final long serialVersionUID = 1L;
@PostConstruct
public void init()
{
qtyList=new ArrayList<>();
qtyList.add(new Cart(1, 1));
qtyList.add(new Cart(2, 1));
qtyList.add(new Cart(3, 2));
qtyList.add(new Cart(4, 1));
qtyList.add(new Cart(5, 3));
}
public void delete()
{
for(Cart cart:selectedQtyList) {
System.out.println(cart.getId()+" : "+cart.getQty());
}
System.out.println("Perform deletion somehow.");
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个包含购物车的会话作用域JSF托管bean.该类Cart非常直观,只有两个类型属性,Integer即id和qty参数化构造函数.
单击给定的删除按钮时,我们需要将要删除的选定行设置为辅助bean.
要设置所选行,请将process属性<p:commandButton>设置为@this,cartDataTable并在selectedQtyList按下此按钮时将所选行设置为bean的属性.
由于这是一个会话范围的bean,如果用户不知道在按下删除按钮之前在任何行中修改了购物车中的数量,则数量的新值/被设置为列表qtyList.
这应该仅在更新购物车时发生,但在删除行时肯定不会再发生.
在实际应用程序中,删除是在单独的视图范围内的bean中完成的.
如果process属性<p:commandButton>设置为@this仅(即从此属性中process="@this"删除cartDataTable),则所选行不会设置为托管bean属性selectedQtyList.
如何处理?
据我所知,你基本上想要在<p:inputText value="#{cart.qty}">按下删除按钮时防止被处理(更新模型值).从理论上讲,它应该只在process属性中指定选择列,但遗憾的<p:dataTable>是没有吃掉它.此外,immediate="true"按钮将无法帮助,因为<p:column selectionMode>不支持.
您最好的选择是确保rendered输入字段的属性仅评估true是否未按下删除按钮或JSF是否呈现响应.
<p:inputText ... rendered="#{empty param[delete.clientId] or facesContext.renderResponse}" />
...
<p:commandButton binding="#{delete}" ... />
Run Code Online (Sandbox Code Playgroud)
JSF即不会处理未呈现的输入组件.在这种情况下,bean范围无关紧要.