• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access bean defined in outer jsp page  RSS feed

 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi, there:

I had outer.jsp as below:

<bean:define id="_testTag" name="testForm" property="testTag" type="java.lang.String" />
<%@ include file="/include/inner.jsp" %>

inner.jsp as below:

<%if ( !_testTag.equalsIgnoreCase("")){%>
<tr>
<td colspan="6" height="40">
<table width="100%" border="1" cellspacing="0" cellpadding="1" class="table3D">
<tr>
<%if ( _testTag.equalsIgnoreCase("message")){%>
<td class="successText" height="25"><html:errors /></td>
<%}else{%>
<td class="errorText" height="25"><html:errors /></td>
<%}%>
</tr>
</table>
</td>
</tr>
<%}%>

It seems bean was invisible in inner.jsp as i got error "_testTag can not be resolved" in eclipse workspace for inner.jsp:

Can anyone advise how to resolve ? Thanks.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Using scriptlets (Java code) in a JSP has been obsolete since the introduction of JSP 2.0 15 years ago! Why are you not using the JSTL and EL rather than 15-year-old technology?
 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not working on developing new app but maintance of existed app though.
 
Alex Lieb
Ranch Hand
Posts: 61
3
Java Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this has to do with how includes are rendered; I remember we ran into something like this not too long ago on our own website. Page-scope beans have issues when you try to share them between includes.

I could be wrong about the cause here; this is a memory of something we ran into a few weeks ago that I'm working on, but I'm pretty sure it's because of how includes are done; if you think of a JSP as a java object, when you instantiate a *local* bean in any kind of JSP, it won't have scope outside that JSP. Try giving your bean:define tag a "scope" attribute, and make the scope wider than page.

Actually yeah; I looked up some code:

If you don't specify a scope, the bean is defined with "page" scope: http://www.java-samples.com/showtutorial.php?tutorialid=1009

If a bean is defined with page scope, it makes the bean you made available to the servlet's "PageContext" object; look at lines 279 to 288 here

So... When you use the include in your outer include, it's calling this


And when that happens, it runs the include as a new servlet which prints its output directly to the page, but that include is a completely separate servlet, a completely separate java object. Then it allows that object *to die*. And since that object contains the PageContext object which knows your bean exists, your bean also dies.

If you want a bean created in one JSP to be accessible in another, I'd try setting the scope to "request."
 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alex for replying.

But i have many different outer.jsp which include inner.jsp, i tried add scope="request" in one of outer.jsp, seems still not work though:

outer.jsp
<bean:define id="_testTag" name="testForm" property="testTag" type="java.lang.String" scope="request" />
<%@ include file="/include/inner.jsp" %>

If i can do some fix in inner.jsp, that would be great, thanks.




 
Alex Lieb
Ranch Hand
Posts: 61
3
Java Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well shoot. Did you try session scope? You probably don't really want these Strings to actually be session scope, but if putting them in session or application scope works, that'll tell you something about the nature of the problem at least.

Also, I just realized I overlooked something important earlier. When the method I linked to earlier defines a bean, regardless of what scope you specify, it doesn't actually declare it as a local field, so it makes sense that you wouldn't be able to see it just by referencing the bean name even if it *is* in scope... Regardless of scope there's never going to be a local field in a JSP called anything that isn't defined in the actual JSP servlet class; there's only attributes stored in other objects, like PageContext for example... Oops. If you're really stuck using scriptlets for this, I think you might have to define it as a request scope bean first, *then* try to figure out which object it stored it in, and pull it out of that object in a scriptlet.

EDIT: Here!

http://docs.oracle.com/javaee/7/api/javax/servlet/jsp/PageContext.html#getRequest--

So if I've got this right, if it's request scope, it should store it as an attribute in the ServletRequest object, which you can get from the PageContext object by calling getRequest(), then you can get your String by calling getAttribute(String name)
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think inner.jsp should be able to reference the variable defined in outer.
The <%@ include %> directive is effectively "copy the contents of inner.jsp here, and compile the whole jsp including outer."
So theoretically the problem isn't with the inner.jsp - it's with outer.

Now this looks like very old struts code. The <bean:define> tag is not standard JSP, but rather a struts tag from waaaaaaaay back in the day.

Litmus test #1:
Replace the contents of inner.jsp with a static string. e.g. "Hello from inner.jsp"
Did that compile and print you the message?

Litmus test #2
Add the following to outer.jsp:

Does it manage to find the variable?

Litmus test #3:
View source on a successfully generated page (even if it is just the <bean:define> tag)
Do you see the bean:define tag in the source? If so, then it isn't being interpreted properly.

Add the following to outer.jsp

Does that make it work?


tldr:
My guess is that the <bean:define> tag is not being interpreted.
So it doesn't define a variable for you, so you get a compile error.

Reasons this could be a myriad but come down to:
- Tag library needs to be declared on the page
- struts-taglib.jar library needs to be in the WEB-INF/lib directory (or at least the jar file that defines the struts bean tag and its tld)

 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all replys above and do wish you guys have nice weekend !

1. We do have struts.jar which included DefineTag class under WEB-INF, so it's not library issue.

2. We also have below statement under each outer.jsp, so it's not import issue as well.
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

3. above test1, 2, 3 all done without any issue, success, so still not too sure what's the issue, thanks.
 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"above test1, 2, 3" means in Stefan Evans's reply #1, #2, #3
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well so much for that theory :-)

Now I go back and read the first post again (emphasis mine):

>It seems bean was invisible in inner.jsp as i got error "_testTag can not be resolved" in eclipse workspace for inner.jsp

I presume when you actually try and execute the page it just works?
I would expect to see the error on inner.jsp in Eclipse because it doesn't know anything about outer.jsp.

It should work just fine at runtime on the server though.
 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it works at runtime only.

The reason i want to fix error in inner.jsp included in many different outer.jsp is to enable debug whole app.

Currently i have to put print statement in app when fixing defect since the app not able to be debugged due to the erro in workspace (?), thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it works at run-time, then there is no error to be fixed. Don't waste your time trying to satisfy the IDE for things that aren't real.
 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, debug works now as well, thanks and please close this topic.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Topics do not get closed when resolved. Rather you should give thumbs up to the replies that helped you, and mark the topic as resolved (buttons on lower left of replies).
 
Anna Ai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for every one above for replying/help, the issue was still there in workspace, however both runtime and debug works for the app, so no necessary to waste time to fix error fiting workspace only.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!