user2022068
user2022068

Reputation:

How to get a variable from scriplet in tag file

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?

Upvotes: 1

Views: 647

Answers (3)

Masudul
Masudul

Reputation: 21981

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}"/>

Upvotes: 0

benjamin.d
benjamin.d

Reputation: 2881

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()}"/>

Upvotes: 1

santu
santu

Reputation: 685

${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.

Upvotes: 0

Related Questions