Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bean:write - scope problem?

 
Rich Smyth
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain why <%= bean %> and <bean:write name="bean" /> give different results in the simplified code below.
<jsp:useBean id="menuFactory" scope="application" class="menu.MenuFactory" />
<jsp:useBean id="menu" scope="session" class="menu.Menu" />
<% menu = menuFactory.getMenu("menu2"); %>
<%= "1 " + menu %><br>
2  <bean:write name="menu" /><br>
gives this output
1 Menu:menu2
2 Menu:null <-- why null and not menu2?
I thought <bean:write> searched all scopes?
Rich
 
Rich Smyth
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got my answer!
Turns out <jsp:useBean> as I used it, creates an instance of my bean (since none exists) and creates *2* references to it: one reference is stored in the session scope and is used for all the get/setProperty operations, the other reference is used as a scripting variable and has page scope. In my example code, when I assign a new value to the scripting variable the variable in session scope still has a reference to the bean created by <jsp:useBean>. Voila! now I understand my results.
Rich
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic