• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp-setProperty tag whitin jsp-useBean tag

 
Fabien Krzewinski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I have searched the forum but didn't get the answer to the following problem:

Here is a piece of a jsp of mine:
<jsp:useBean id="Beer" type="demo.model.Beer">
<jsp:setProperty name="Beer" property="BeerName" value="Jupiler"/>
</jsp:useBean>


So far i get it , but when i look at the translated servlet, i get this:

getServletContext().setAttribute("Beer",new Beer("Leffe"));
demo.model.Beer Beer = null;
synchronized (_jspx_page_context) {
Beer = (demo.model.Beer) _jspx_page_context.getAttribute("Beer", PageContext.PAGE_SCOPE);
if (Beer == null){
throw new java.lang.InstantiationException("bean Beer not found within scope");
}
}


Shouldn't the if be translated into setAttribute stuff ?

I'm using Tomcat 6.0 with Jasper on Windows xp.
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't specify the class attribute an exception is thrown if there is no object in the specified scope with the specified name.
that's why if beer is equal to null then an exception will be thrown.

if you specify a class attribute the if st. will be different.
 
Fabien Krzewinski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the jsp:setProperty within jsp:useBean depends not on the existence of the object or not... I mistyped the example from the book but i got certainly a possible, tricky question of an exam

Many thanks !
[ June 12, 2008: Message edited by: Fabien Krzewinski ]
 
Kunal Jag
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using the <jsp:useBean> action tag with only id and type attributes, then you're trying to make polymorphic bean reference. This would expect a compatible bean reference to be existing in the specified scope (remember if the scope is omitted, default is the page scope). If an existing bean is not found, a new bean instance will not be created. Instead, it will throw an InstantiationException at runtime.

Regards,
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Musab Al-Rawi:
If you don't specify the class attribute an exception is thrown if there is no object in the specified scope with the specified name...


.. so the <setProperty> tag makes no sense, right?
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case it doesn't, but if you use the class attributes then the setProperty tag will execute if the bean doesn't exist.
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bottom line is, if you use "type" without "class", the container will not attempt to instantiate an object (because you failed to tell it the class). As clearly indicated in HFS, in the above instance the associated object should already exist (in some scope).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic