Passing parameters to a method in h:outputtext tag

2019-06-16 15:59发布

I would like to display a text in jsf screen by passing an attribute to a method implemented in backing bean. I have to pass a dynamic value as an attribute. I tried the below but it seems to be an incorrect syntax -

<h:outputText value="#{getValue(#{item.product}).component.address}" />

标签: jsf el
2条回答
狗以群分
2楼-- · 2019-06-16 16:10

JSF 1.2 w/ EL 2.1 (the usual pairing) doesn't support calling methods with parameters on beans using EL expressions. There exists one hack, abusing the fact that JSF EL expressions work on maps:

value="#{myBean['product']}"

This will assume myBean is implementing Map interface and will call get method with 'product' as parameter. I don't know if the key can be dynamically computed but it's worth a try. Syntax would be:

value="#{myBean[item.product].component.address}"

Assuming here that myBean implements Map interface and returns the appropriate object and item.product is returns a string. I am not sure this will work, but it's your only option beside implementing a getter that looks up item.product value in its implementation.

查看更多
等我变得足够好
3楼-- · 2019-06-16 16:23

Apart from the syntax error (you can never nest EL expressions like as #{#{}}), the following is valid in EL 2.2 which is in turn part of Servlet 3.0 / Java EE 6:

<h:outputText value="#{bean.getValue(item.product).component.address}" />

So if you have a Servlet 3.0 compatible target runtime (Tomcat 7, Glassfish 3, JBoss 6, etc) with a Servlet 3.0 compatible web.xml, then you can invoke non-getter methods with arguments like this.

However, based on your question history you're using JSF 1.2 and the chance is big that you're also targeting an older container where the above wouldn't work. If it is a Servlet 2.5 container, then you could use JBoss EL to get this EL syntax to work.

See also:

查看更多
登录 后发表回答