1
votes

I'm trying to do something like (in a JSP page, running with JavaServer Faces):

<%
String myTitle = "Cool Action!";
String myAction = "coolAction";
%>

<h:commandLink value="#{myTitle}" action="#{myAction}" />

This isn't working, because JSF can't see the JSP variables (at least with this notation?).

Is there a way to access JSP variables from JSF tags (without using a session/page-scoped bean), OR is there a way to define JSF tags like commandLink from inside a <% %> JSP block? Thanks!

1

1 Answers

3
votes

You need to put them in at least the request scope. EL resolves among others the attributes of HttpServletRequest, HttpSession and ServletContext.

<%
request.setAttribute("myTitle", "Cool Action!");
request.setAttribute("myAction", "coolAction");
%>

But this is totally the wrong way of using JSF. To follow the proper MVC design ideology, you should not use old fashioned JSP scriptlets in any way. Even more, JSP's successor Facelets does not support any form of scriptlets.

Depending on the concrete functional requirement, you could create a backing bean class wherein you specify them as bean properties,

<h:commandLink value="#{bean.myTitle}" action="#{bean.myAction}" />

or just "hardcode" it directly in the <h:commandLink>.

<h:commandLink value="Cool Action!" action="coolAction" />

or put at least the value in an i18n resource bundle which you load by <f:loadBundle> or register by <resource-bundle> in faces-config.xml:

<h:commandLink value="#{text['myTitle']}" action="coolAction" />