Forums Register Login

jsp-setProperty tag whitin jsp-useBean tag

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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,
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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).
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1101 times.
Similar Threads
Tag attributes and the setter method
regarding jsp:setProperty
jsp:useBean
Jsp and javabean
jsp action
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:43:40.