Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

jsf 2 - How to pass method arguments to an ActionListener?

I'm creating a dynamically generated menu. In the database, I have stored the directory (String) of each submenu and stored in an ArrayList in the bean. This is a part of the code:

<c:forEach items="#{controlWeb.algorithms}" var="alg">
    <h:form>
        <h:commandLink actionListener="#{controlWeb.setContent(PROBLEM)}"  value="About"/>
    </h:form>
</c:forEach>

The problem is in "PROBLEM". I want to write #{alg.algorithm1} and it doesn't work because I think it isn't posible to nest EL expressions.

The actionListener set the String #{alg.algorithm1} in another String. How can I achieve the requirement?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Use action, not actionListener. The actionListener must be bound to a method which takes ActionEvent as the sole argument, no excuses. Also, an actionListener isn't intented to invoke actions, but to listen on actions (so that you can log it or do some preprocessing task or whatever).

<c:forEach items="#{controlWeb.algorithms}" var="alg">
    <h:form>
        <h:commandLink action="#{controlWeb.setContent(alg.algorithm)}" value="About"/>
    </h:form>
</c:forEach>

Note that passing arguments in EL is only supported since EL 2.2 which is shipped along Servlet 3.0 / Java EE 6. You need to make sure that you're targeting a Servlet 3.0 compatible container with a Servlet 3.0 compatible web.xml.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...