• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About the id attribute of useBean action

 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following JSP will not translate successfully because the value of the id attribute cannot be used in more than one <jsp:useBean> action in a single translation unit. Why do they have this restriction? Both are declared in different scopes. The instance variable "address" in the if block would not be the same as the one in the else block. So what's the problem?

<% if (true) { %>
<jsp:useBean id="address" class="AddressBean" scope="session" />
<% } else {%>
<jsp:useBean id="address" class="AddressBean" scope="session" />
<% } %>
 
David Bridgewater
author
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edmond,

I can't think of a technical reason why the restriction is imposed. I can think of practical reasons. Mostly, using the same id for different <jsp:useBean> occurrences would be a mistake; so the simplest way of dealing with that is to disallow the possibility completely. It's an easy parsing operation to check whether id is duplicate, but a much harder one to spot occasions when it might be OK according to the logic of the generated servlet.

To turn the question on its head... when would you (practically speaking) want the ability to have beans of the same id created in different blocks in different circumstances?

Best,

David.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic