2017-07-14 109 views
0

我正在尝试开发一个从列表中输出文本的组合。要显示的列表和项目属性必须设置为参数。所以我创造了这个复合(简化我只是不停什么是必要的):按名称访问bean属性

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core" 
    xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui" 
    xmlns:composite="http://xmlns.jcp.org/jsf/composite" xmlns:bt="http://xmlns.jcp.org/jsf/composite/tags/bt"> 
<composite:interface> 

    <composite:attribute name="id" /> 
    <composite:attribute name="list" shortDescription="Output list (items itself)" /> 
    <composite:attribute name="listValue" shortDescription="Bean attribute to be displayed in the list" /> 

</composite:interface> 
<composite:implementation> 
    <h:panelGroup id="#{cc.attrs.id}" layout="block"> 
     <p:repeat var="item" value="#{cc.attrs.list}"> 
      <h:outputText value="#{item[cc.attrs.listValue]}" /> 
     </p:repeat> 
    </h:panelGroup> 
</composite:implementation> 
</html> 

用法:

<bt:selectToDataTable id="rolesSd" list="#{aclControlBean.componentSecurity.roles}" listValue="name" /> 

componentSecurity.roles是Hibernate的JPA实体和角色是一个集合。

当我打开页面,获取此异常:

Caused by: javax.el.PropertyNotFoundException: The class 'org.hibernate.collection.internal.PersistentSet' does not have the property 'name'. 
    at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:568) 
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:229) 
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) 
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) 
    at com.sun.el.parser.AstValue.getValue(AstValue.java:139) 
    at com.sun.el.parser.AstValue.getValue(AstValue.java:203) 
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226) 
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) 
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) 
    at com.sun.faces.facelets.el.ContextualCompositeValueExpression.getValue(ContextualCompositeValueExpression.java:158) 
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) 
    ... 105 more 

看来“变种”属性不被认为是渲染视图时,因为它正试图从“角色”从PersistentSet获得的,而不是一个项目属性组。

对发生了什么有什么想法?

在此先感谢。

+0

它没有复合工作吗?它是否与'ui:repeat'一起使用?把错误文本放在谷歌? – Kukeltje

+0

你好@Kukeltje,它在复合材料之外也不起作用。很奇怪的是,访问bean属性的相同代码在f:selectItems中工作:

+0

@DavidFlorez,这是一个EL声明,是的,在理论上我可以得到任何财产的访问那样。使用XHTML反射是不可能的。看一看:https://docs.oracle.com/cd/E19798-01/821-1841/bnahx/index.html –

回答

0

好的,发现问题了。要在这里添加我的答案以帮助其他人解决同一问题。

发生了什么事情是在JSF中我无法遍历Set,它必须是List类型。所以我从设置更改为列表,现在它正在工作。

不管怎么样,你们帮助你们!