我在页面中有多个表单,每个表单都有数据表。当我选择映射到该控件的数据表时,应该显示在页面上。在使用primefaces开发应用程序时面临以下异常:
javax.faces.view.facelets.TagAttributeException: //C:/Workspace/Application/WebContent/transfer.xhtml @28,102 rendered="#{transferMB.mySelectedValues('1')}" Error Parsing: #{transferMB.mySelectedValues('1')}
at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:428)
at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:378)
at com.sun.faces.facelets.tag.jsf.ComponentRule$ValueExpressionMetadata.applyMetadata(ComponentRule.java:107)
at com.sun.faces.facelets.tag.MetadataImpl.applyMetadata(MetadataImpl.java:81)
at javax.faces.view.facelets.MetaTagHandler.setAttributes(MetaTagHandler.java:129)
at javax.faces.view.facelets.DelegatingMetaTagHandler.setAttributes(DelegatingMetaTagHandler.java:102)
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
Caused by: javax.el.ELException: Error Parsing: #{transferMB.mySelectedValues('1')}
at com.sun.el.lang.ExpressionBuilder.createNodeInternal(Unknown Source)
at com.sun.el.lang.ExpressionBuilder.build(Unknown Source)
at com.sun.el.lang.Expres
sionBuilder.createValueExpression(Unknown Source)
at com.sun.el.ExpressionFactoryImpl.createValueExpression(Unknown Source)
at com.sun.faces.facelets.tag.TagAttributeImpl.getValueExpression(TagAttributeImpl.java:412)
... 94 more
您应该使用逗号来分隔您要更新的表单,例如:
<p:ajax update=":transForm, :sprdForm" />
这意味着有两个表单需要更新。
如果您编写如下代码:
<p:ajax update=":transForm :sprdForm" />
意味着你想在transForm
中更新sprdForm
,这是不可能的,因为你不能把一个表单放在另一个表单中。
您正在尝试将列表与单个字符串进行比较:
rendered="#{transferMB.selectedItems == '1'}"
但是,你应该检查列表是否包含这个字符串:
在html:
rendered="#{denemeBean.controlSelectedValues('1')}"
在bean类中:
// list should be initialized (unless, we'll get NullPointer
// when using the below method)
private List<String> selectedItems = new ArrayList<>();
// This methods returns true if parameter is included in the
// list of selected items
public boolean controlSelectedValues(String needed) {
for (String string : selectedItems) {
if (string.equals(needed)) {
return true;
}
}
return false;
}
最后,我不确定您是否真的需要将这些数据表放在表单中。下面你可以看到我所做的工作副本:(当然你应该用你的数据表替换outputtext)
// bean file
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name = "denemeBean")
@ViewScoped
public class DenemeBean implements Serializable {
private List<String> selectedItems = new ArrayList<>();
/**
* @return the selectedItems
*/
public List<String> getSelectedItems() {
return selectedItems;
}
/**
* @param selectedItems the selectedItems to set
*/
public void setSelectedItems(List<String> selectedItems) {
this.selectedItems = selectedItems;
}
public boolean controlSelectedValues(String needed) {
for (String string : selectedItems) {
if (string.equals(needed)) {
return true;
}
}
return false;
}
}
// html file:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</h:head>
<h:body>
<h:form>
<h:selectManyCheckbox value="#{denemeBean.selectedItems}">
<f:selectItem itemValue="1" itemLabel="Transfer Status" />
<f:selectItem itemValue="2" itemLabel="Spread Status" />
<f:selectItem itemValue="3" itemLabel="Number1 - 3" />
<p:ajax event="change" update=":tables" />
</h:selectManyCheckbox>
</h:form>
<p:panel id="tables" >
<p:panel id="transForm" rendered="#{denemeBean.controlSelectedValues('1')}">
<h:outputText value="table1 Here" />
</p:panel>
<p:panel id="sprdForm" rendered="#{denemeBean.controlSelectedValues('2')}">
<h:outputText value="table 2 Here" />
</p:panel>
</p:panel>
</h:body>
</html>
使用<p:ajax update=":transForm :sprdForm" />
是完全有效的。你还有很多其他的问题。
第一点:您的渲染属性比较列表与字符串(selectedItems=='1'
)。它应该测试列表是否包含字符串(selectedItems.contains('1')
)。(注意:必须使用EL 2.2)
第二点是:我认为<p:panelGrid>
需要columns
-属性。我剥离了你的代码,这对我来说很好:
<h:body>
<h:form>
<p:selectManyCheckbox value="#{transferMB.selectedItems}">
<f:selectItem itemValue="1" itemLabel="Transfer Status" />
<f:selectItem itemValue="2" itemLabel="Spread Status" />
<f:selectItem itemValue="3" itemLabel="Number1 - 3" />
<p:ajax update=":transForm :sprdForm"/>
</p:selectManyCheckbox>
</h:form>
<h:form id="transForm">
<p:panelGrid columns="1" rendered="#{transferMB.selectedItems.contains('1')}">
transForm
</p:panelGrid>
</h:form>
<h:form id="sprdForm">
<p:panelGrid columns="1" rendered="#{transferMB.selectedItems.contains('2')}">
sprdForm
</p:panelGrid>
</h:form>
</h:body>
我使用<p:selectManyCheckbox>
,因为<h:selectManyCheckbox>
与内部<p:ajax>
导致java.lang.ClassCastException: java.lang.Boolean cannot be cast to org.primefaces.component.api.ClientBehaviorRenderingMode
。我认为这可能是我的JSF实现JSF -impl-2.1.7- jbosssorg -2.jar.