문제

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?

도움이 되었습니까?

해결책

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

다른 팁

${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}"/>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top