在我的backing-bean中,我有一组不同子类的对象共享一个公共接口。在视图内部,ui:repeat
遍历此集合。在这个循环中,必须根据接口的具体实现呈现不同的属性。
我将问题减少到以下支持bean:
@Named
@SessionScoped
public class DummyBean implements Serializable {
private List<Type> objects = new ArrayList<Type>();
public void add1() {
objects.add(new Type1());
}
public void add2() {
objects.add(new Type2());
}
public void remove(Type o) {
objects.remove(o);
}
// Getter.
}
以及由Type
和add1
方法添加的add2
实现。它们有一些共同的属性,但它们也可能有不同的属性,例如下面的例子中的getType1OnlyMethod()
:
public class Type1 implements Type {
@Override
public String getType() { return "1"; }
public List<String> getType1OnlyMethod() {
return Arrays.asList("only1", "only2");
}
// ...
}
public class Type2 implements Type {
@Override
public String getType() { return "2"; }
// ...
}
在视图中,我想迭代存储在DummyBean
中的所有对象并显示其内容。根据对象的具体类型,可以使用Type1
- 属性为条件呈现调用仅在类rendered
中实现的方法:
<h:form id="dummyForm">
<ui:repeat value="#{dummyBean.objects}" var="_obj" >
<h:outputText value="Type of the Object is #{_obj.type}." />
<h:panelGroup rendered="#{_obj.type eq '1'}">
<ui:repeat value="#{_obj.type1OnlyMethod}" var="_oneOnlyObject" varStatus="_status" >
<h:outputText value="#{_oneOnlyObject}" />
</ui:repeat>
</h:panelGroup>
<h:commandLink value="remove" action="#{dummyBean.remove(_obj)}">
<f:ajax render="dummyForm" />
</h:commandLink>
<br/>
</ui:repeat>
<h:commandLink value="add1" action="#{dummyBean.add1}">
<f:ajax render="dummyForm" />
</h:commandLink>
<br/>
<h:commandLink value="add2" action="#{dummyBean.add2}">
<f:ajax render="dummyForm" />
</h:commandLink>
</h:form>
使用页面上的h:commandLink
我可以随意添加和删除Type1
个对象。如果我添加Type2
个对象,它们将按预期呈现。但是当我尝试删除其中一个Type2
对象时,我在渲染响应阶段得到一个例外(如果列表中还有其他元素,则不然):
2014-03-24 15:42:46,754 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (default task-11) Error Rendering View[/transition/beginTestSite.xhtml]:
javax.el.PropertyNotFoundException: /transition/beginTestSite.xhtml @23,96 value="#{_obj.type1OnlyMethod}": The class 'at.co.xss.Type2' does not have the property 'type1OnlyMethod'.
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.facelets.component.UIRepeat.getValue(UIRepeat.java:279) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.facelets.component.UIRepeat.getDataModel(UIRepeat.java:255) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.facelets.component.UIRepeat.setIndex(UIRepeat.java:523) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.facelets.component.UIRepeat.doVisitChildren(UIRepeat.java:790) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.facelets.component.UIRepeat.visitTree(UIRepeat.java:748) [jsf-impl-2.2.5-jbossorg-3.jar:]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at com.sun.faces.facelets.component.UIRepeat.visitTree(UIRepeat.java:754) [jsf-impl-2.2.5-jbossorg-3.jar:]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at javax.faces.component.UIForm.visitTree(UIForm.java:371) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.saveView(FaceletPartialStateManagementStrategy.java:472) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.application.StateManagerImpl.saveView(StateManagerImpl.java:89) [jsf-impl-2.2.5-jbossorg-3.jar:]
at javax.faces.application.StateManager.getViewState(StateManager.java:593) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at com.sun.faces.context.PartialViewContextImpl.renderState(PartialViewContextImpl.java:486) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:328) [jsf-impl-2.2.5-jbossorg-3.jar:]
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:219) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57) [primefaces-4.0.jar:4.0]
at org.richfaces.context.ExtendedPartialViewContextImpl.processPartial(ExtendedPartialViewContextImpl.java:218) [richfaces-core-impl-4.3.5.Final.jar:4.3.5.Final]
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:435) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133) [jsf-impl-2.2.5-jbossorg-3.jar:]
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) [jsf-impl-2.2.5-jbossorg-3.jar:]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) [jboss-jsf-api_2.2_spec-2.2.5.jar:2.2.5]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) [shiro-web-1.2.2.jar:1.2.2]
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) [shiro-web-1.2.2.jar:1.2.2]
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) [shiro-core-1.2.2.jar:1.2.2]
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) [shiro-core-1.2.2.jar:1.2.2]
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) [shiro-core-1.2.2.jar:1.2.2]
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) [shiro-web-1.2.2.jar:1.2.2]
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro-web-1.2.2.jar:1.2.2]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:56) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:168) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:687) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
Caused by: javax.el.PropertyNotFoundException: The class 'at.co.xss.Type2' does not have the property 'type1OnlyMethod'.
at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:730) [javax.el-3.0.0.jar:3.0.0]
at javax.el.BeanELResolver.getValue(BeanELResolver.java:351) [javax.el-3.0.0.jar:3.0.0]
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.2.5-jbossorg-3.jar:]
at com.sun.el.parser.AstValue.getValue(AstValue.java:140) [javax.el-3.0.0.jar:3.0.0]
at com.sun.el.parser.AstValue.getValue(AstValue.java:204) [javax.el-3.0.0.jar:3.0.0]
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226) [javax.el-3.0.0.jar:3.0.0]
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) [jsf-impl-2.2.5-jbossorg-3.jar:]
... 66 more
似乎在渲染响应阶段,嵌套的ui:repeat
被“执行”为当前删除的Type2
对象。
如果我删除第二个(嵌套的)ui:repeat
并添加一些普通h:outputText
来访问仅在Type1
中可用的属性,当我添加或删除时,一切都会毫无例外地起作用元素:
<h:form id="dummyForm">
<ui:repeat value="#{dummyBean.objects}" var="_obj" >
<!-- ... -->
<h:panelGroup rendered="#{_obj.type eq '1'}">
<h:outputText value="#{_obj.type1OnlyMethod.size()}" />
</h:panelGroup>
<!-- ... -->
</h:form>
有没有人对这个话题有任何想法?
(在JEE7应用服务器上运行(Wildfly 8.0.0.Final))
答案 0 :(得分:3)
这是由Mojarra <ui:repeat>
的状态管理中的错误导致的,该错误按照我的同事Arjan Tijms报告的issue 3215进行了修复。在zeef.com(Block
,LinkBlock
,ImageBlock
和TextBlock
处迭代不同FeedBlock
个实例时,我们也遇到了此问题。该修复程序在Mojarra 2.2.7中可用,而JSF 2.0 / 2.1则按Mojarra 2.1.29向后移植到issue 3221。因此,至少升级到该版本(或根据Mojarra homepage只提供最新版本)应该这样做。
否则,您最好的选择是将<ui:repeat>
替换为<c:forEach>
。
答案 1 :(得分:1)
在我的情况下,我没有选择升级Mojarra的版本并避免使用c:forEach(当与ui可选渲染组件一起使用时会产生许多副作用)我替换了ui:repeat with ap:dataList,它工作正常。你必须做一些CSS样式来隐藏子弹,但我认为它值得付出代价。我希望它可以帮助某人;)