如何在JSF EL显示列表#大小()的价值? [英] How to display value of List#size() in JSF EL?
问题描述
我想知道是否有一种方法的返回值绑定到JSF组件的方式。
我会更好地解释自己。
比方说,我有这样一个类:
I'd like to know if there's a way to bind the returned value of a method into a JSF component. I'll explain myself better. Let's say I have a class like this:
public class Document {
private List<Attachment> attachments;
//getter and setter here
}
这个类是通过一个名为currentDocument物业的注册管理的bean这种方式接触到JSF,并使用在JSF
this class is exposed to jsf through a registered managed bean in a property called currentDocument, and used in a jsf this way
<h:outputText value="#{myManagedBean.currentDocument.attachment.size}" />
这是不正确的,我知道。但是,什么是做到这一点的正确方法?
我应该创建文档类的属性,让我们说numberOfAttachments,并绑定到,或者有直接上一个方法的返回值绑定的方式?
This isn't correct, I know. But what is the correct way to do this? Am I supposed to create an attribute on the Document class, let's say numberOfAttachments, and bind to that, or there's a way to bind directly on a method's return value?
推荐答案
如果您运行的是EL 2.2能力的容器(Tomcat 7的,Glassfish的3,JBoss AS中6或更高版本,所有执行的Servlet 3.0),或正在使用JBoss EL,那么你应该能够通过EL调用非getter方法:
If you're running an EL 2.2 capable container (Tomcat 7, Glassfish 3, JBoss AS 6 or newer, all implementing Servlet 3.0), or are using JBoss EL, then you should be able to invoke non-getter methods by EL:
<h:outputText value="#{myManagedBean.currentDocument.attachment.size()}" />
另一种方法是使用 JSTL FN:长度()
:
<html xmlns:fn="http://java.sun.com/jsp/jstl/functions" ...>
...
<h:outputText value="#{fn:length(myManagedBean.currentDocument.attachment)}" />
如果没有那是可能的,你出于某种原因,那么你最好的选择是建立一个EL函数自己
If none of that is possible for you for some reason, then your best bet is to create an EL function yourself
<h:outputText value="#{my:size(myManagedBean.currentDocument.attachment)}" />
或额外的getter方法添加到#{myManagedBean}
返回这一点。
or to add an extra getter method to #{myManagedBean}
which returns exactly that.
<h:outputText value="#{myManagedBean.currentDocumentAttachmentSize}" />
参见:
- JSF 2.0方法调用
- JSF 2.0 method invocation
See also:
这篇关于如何在JSF EL显示列表#大小()的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!