Question

These are my first steps in tag file. Maybe this question is very simple. But I can't solve it.

I have the following tag file

<% 
Foo foo=new Foo();
%>
<jsp:include page="${foo.getFileName()}"/>

It seems to me that jasper doesn't see foo variable. What am I doing wrong?

Was it helpful?

Solution

Using the expression language ${...} your variable must be accessible in one of the PageContext, Request, Session, Application... scopes.

In order to make your code work, you must change it to:

<% 
Foo foo=new Foo();
pageContext.setAttribute("foo", foo);
%>
<jsp:include page="${foo.getFileName()}"/>

If you are using a tag file, then prefer maybe jspContext instead of pageContext:

  <% 
    Foo foo=new Foo();
    jspContext.setAttribute("foo", foo);
    %>
    <jsp:include page="${foo.getFileName()}"/>

OTHER TIPS

${some variable name} takes variable name from a scope e.g. request/session/application.

But your foo object has not been set in any scopes.

just for a try, use session.setAttribute("foo", foo) or pageContext.setAttribute(...) inside the scriptlet and try.

Now just try to understand the scopes and which scope better fits in your application.

Here, scrptlet foo is not recognized at <jsp:include/> EL

Use <jsp:useBean/> action to use at <jsp:include/> as EL

 <jsp:useBean id="foo"  class="packeage.Foo"  scope="page"/>
 <jsp:include page="${foo.fileName}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top