JSF2:将cc:属性限制为List中的给定对象类型 [英] JSF2: limiting cc:attribute to a given object type within a List
问题描述
如果我具有如下所示的托管bean:
If I had a managed bean as follows:
@ManagedBean
@RequestSchoped
public class Example {
private List<String> stringList;
private List<Long> longList;
// getters, setters, etc. down here
}
,并具有一个接受列表作为属性的自定义组件:
and had a custom component which accepted a List as an attribute:
<?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:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="aList" type="java.util.List" />
</cc:interface>
<cc:implementation>
<!-- code is in here -->
</cc:implementation>
</html>
我如何确保它有效:
<myComp:previousComponent aList="#{example.stringList}" />
但这不是:
<myComp:previousComponent aList="#{example.longList}" />
换句话说,我要为cc:attribute
做的事情如下:
In other words, what I want to do for the cc:attribute
is as follows:
<cc:attribute name="aList" type="java.util.List<java.lang.String>" />
但是,众所周知,xhtml并不善于使用>或<.另外,由于仅在编译时检查泛型,所以我不确定该如何完成.有人知道这是否可能吗?
However, as we know xhtml doesn't take kindly to using > or <. Also, with Generics only being checked at compile time, I'm not sure how this would be done. Does anyone know if this is possible?
推荐答案
您可以使用#{item.class.name}
检查每个项目的类型. Class#getName()
返回表示类型的String
.例如. java.lang.String
或java.lang.Long
.您可以在rendered
属性中使用它.
You could check the type of each item using #{item.class.name}
. The Class#getName()
returns a String
denoting the type. E.g. java.lang.String
or java.lang.Long
. You could make use of it in the rendered
attribute.
添加一个表示完整合格类名的额外属性.
Add an extra attribute denoting the full qualified classname.
<my:comp list="#{bean.list}" type="java.lang.String" />
结合
<cc:attribute name="list" type="java.util.List" required="true" />
<cc:attribute name="type" type="java.util.String" required="true" />
以及cc:implementation
中的此逻辑:
<ul>
<ui:repeat value="#{cc.attrs.list}" var="item">
<h:panelGroup rendered="#{item.class.name == cc.attrs.type}">
<li>#{item}</li>
</h:panelGroup>
</ui:repeat>
</ul>
这篇关于JSF2:将cc:属性限制为List中的给定对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!