Using a method (with paramter) as a value?

I'm using JSF 1.2 and am trying to accomplish the following:

Say I have a class, Entity, which has a method:

public String getResult(String name)

In my web page, I'd like to loop through some values for name and display each result in a <h:outputText ...>, using things like 'entity.getResult("foo")' in the value attribute, where foo is one of the names in the list I'm looping through.

Is something like this possible, or would I just be better off building a HashMap or something with the names and results?

Thanks,

Matthew

[580 byte] By [mrwilsoxa] at [2007-10-3 1:45:32]
# 1

JSF doesn't do that out-of-the-box. There is a custom EL-Resolver library at

http://el-functors.sourceforge.net/

that lets you do it.

Otherwise, you can wrap it in a bean where you set the parameter ("foo") then get the result with a no-arg method.

To set the parameter, you can either bind it to an input field, or explicitly use a jsp:setProperty tag.

BlackBeana at 2007-7-14 18:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Isn't an option to collect the data in a List and use the h:dataTable to display them in a table?

Check http://balusc.xs4all.nl/srv/dev-jep-dat.html how to use datatables.

Anyway, if this isn't an option, then use f:attribute:

JSF<h:outputText id="myId" value="#{myBean.text}"><f:attribute name="text" value="blah" /></h:outputText>

MyBeanpublic String getText() {

UIComponent component = FacesContext.getCurrentInstance().getViewRoot().findComponent("myId");

String text = (String) component.getAttributes().get("text");

return text;

}

This will show "blah" in the outputText. Of course you can use this to fill in the virtual param of getRequest().

BalusCa at 2007-7-14 18:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Nice idea with the f:attribute, but it won't actually work within a "c:forEach" loop.

Firstly, "myId" will need the ids of any naming containers pre-pended (eg, in a form with id "myform", it becomes "myform:myid").

Secondly, the "myId" will be augmented each time round the loop, so the id will be "myform:myid", "myform:myidj_id_1", "myform:myidj_id_2".

If you invoke

viewRoot.findComponent("myform:myid");

you will always get the first value only.

You need some other way of getting a reference to the component from which the expression is being evaluated.

BlackBeana at 2007-7-14 18:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Ur right. But h:dataTable is a better solution I guess. Looping through the results sounds like tabular data.

Another way to use f:attribute anyway:

JSF<h:outputText binding="#{myBean.textElement}" value="#{myBean.textValue}">

<f:attribute name="text" value="blah" />

</h:outputText>

MyBeanprivate HtmlOutputText textElement;

public HtmlOutputText getTextElement() {

return textElement;

}

public void setTextElement(HtmlOutputText textElement) {

this.textElement = textElement;

}

public String getTextValue() {

return (String) textElement.getAttributes().get("text");

}

Untested in a foreach loop though.

BalusCa at 2007-7-14 18:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Of course, if you use a "binding" on your component, then you will be able to get to the attributes directly. Something like

<h:outputText binding="#{myBean.theOutput}" value="#{myBean.text}">

<f:attribute name="text" value="blah" />

</h:outputText>

Then in your bean you need a "UIOutput" property called "theOutput", and then the "getText" method can look like this:

public String getText() {

String text = (String) getTheOutput().getAttributes().get("text");

return "Got " + text;

}

BlackBeana at 2007-7-14 18:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
LOL, I've edited my msg at the same time ;)
BalusCa at 2007-7-14 18:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...