jsf Invoking Arbitrary Methods via EL

2019-08-10 04:34发布

问题:

I am starter on JSF and have trouble with invokation of arbitrary methods via EL.
According to what I read version 2.1 of the Unified Expression Language, included in Java EE 6, has support for invoking arbitrary methods and passing arguments to them.
I am using MyFaces2.0.3, JRE6Up21, Tomcat 6.0.29 and Eclipse.
I have the following snippet in a facelet:

<h:form>
<p>
<h:link outcome="NextPage" value="Link for Next Page">
<f:param name="word1" value="hello" />
<f:param name="word2" value="world" />
</h:link>
</p>
</h:form>

and in the facelet NextPage.xhtml

<h:form>
<p>
This is the method invocation #{model.printArgs(param.word1,param.word2)}
</p>
</h:form>  

Model is a managed bean(request scoped) that has this method printArgs that returns a string concatenation of word1 and word2.

@ManagedBean
@SessionScoped
public class Model {

public String printArgs(String word1, String word2) {
        String result = "No arguments??????";

        if (null != word1 && !word1.isEmpty() && null != word2 && !word2.isEmpty()) {
            result = word1+" "+word2;
        }
        return result;

    }

}

When I press the link the url is http://localhost:8080/RegJSF/faces/NextPage.xhtml?word1=hello&word2=world
but I get the error

org.apache.el.parser.ParseException: Encountered " "(" "( "" at line 1, column 25. Was expecting one of: "}" ... "." ... "[" ... ">" ... "gt" ... "<" ... "lt" ... ">=" ... "ge" ... "<=" ... etc

If i remove #{model.printArgs(param.word1,param.word2)} the NextPage is loaded, so the problem is the method invocation.
What is the problem here? Is it configuration issue?
Note Same issue if I use Mojarra2.0.3
Can someone help please?

Thanks!

回答1:

According to what I read version 2.1 of the Unified Expression Language, included in Java EE 6, has support for invoking arbitrary methods and passing arguments to them.

I think you are mistaken. This support was introduced in JSP 2.2 (which is required to meet the JEE6 spec).

From the JSP 2.2 spec:

Changes between Maintenance 1 and Maintenance Release 2

The main change in this release is the addition of method invokations with parameters in the EL, such as #{trader.buy(“JAVA”)}.

Since Tomcat 6 is a JSP 2.1 container, you might consider upgrading to Tomcat 7.



回答2:

write

This is the method invocation <h:outputText value="#{model.printArgs(param.word1,param.word2)}"/>

Double quotes are necessary here. Also, try outputText instead.



标签: java jsf jsf-2 el