Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

binding - How to display value of List#size() in JSF EL?

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
}

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}" />

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()}" />

An alternative is to use JSTL fn:length():

<html xmlns:fn="http://java.sun.com/jsp/jstl/functions" ...>
...
<h:outputText value="#{fn:length(myManagedBean.currentDocument.attachment)}" />

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)}" />

or to add an extra getter method to #{myManagedBean} which returns exactly that.

<h:outputText value="#{myManagedBean.currentDocumentAttachmentSize}" />

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...