Well we're trying to render a shitload of lists. Some of them have only one entry and we want them to appear as read only input fields (so users don't get fooled and it's easier to read). But it seems kinda hard to access the inner selectitems size from outside. I had my go with overwriting the htmlselectonemenu tag... Is there a different nicer way? Even possible to access it on tag level?
/**
* In case there is only one or less elements in the select list -&开发者_运维技巧gt; set readonly(true)
*/
public class HtmlSelectOneMenuModf extends HtmlSelectOneMenu {
@Override
public boolean isReadonly() {
for (Iterator iterator = getChildren().iterator(); iterator.hasNext();) {
Object obj = iterator.next();
if ( obj instanceof UISelectItems) {
UISelectItemsi = (UISelectItems) obj;
if(i.getSelectItems().size() <=1)
super.setReadonly(true);
}
}
return super.isReadonly();
}
}
We're chilling on JSF 1.2 btw...
Use JSTL fn:length()
.
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<h:selectOneMenu value="#{bean.item}" readonly="#{fn:length(bean.items) le 1}">
<f:selectItems value="#{bean.items}" />
</h:selectOneMenu>
精彩评论