Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jspSetProperty Doubt??

 
Joe Harry
Ranch Hand
Posts: 10085
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers,

What it means by saying, "setProperty code will not be executed if the bean with the id already exists"....what exactly does that mean?? For example, assume that the id is a person with a name, so the setProperty won't be executed if the person has already a name?? Am I right?? Anyone please help me.

Thanks in advance.
 
Joe Harry
Ranch Hand
Posts: 10085
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, the code below is from Marcus Green's website,



The above being the bean class...

<jsp:useBean id="person" type="com.examulator.Person" class="com.examulator.Person" scope="session" />
<html>
<%
out.print(person.getFirstName());
%>
</html>

The above jsp page will print joe.

Now, my question is that if I don't give the values joe and soap, what will the o/p be if I execute the above jsp page?? Help me please guys.
[ January 01, 2007: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
VijayKumar Sivagnanam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi,

Consider the below example:

<jsp:useBean id="person" class="com.bean.Person" scope="session">
<jsp:setProperty name="person" property="name" value="x"/>
</jsp:useBean>

If the container is able to find a bean named person in the Session scope, then the <jsp:setProperty> will be skipped. Otherwise, if it is unable to find an object named person in the session scope, then a new object of com.bean.Person will be created and the name property will be set to the value "x".

The <jsp:setProperty> is like passing a value to the constructor of the bean while initializing.
 
VijayKumar Sivagnanam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Now, my question is that if I don't give the values joe and soap, what will the o/p be if I execute the above jsp page?? Help me please guys.


if you dont give any default values, null will be print.
 
Joe Harry
Ranch Hand
Posts: 10085
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijayakumar,

Thanks for the reply, and if I include the jspSetProperty line,

<jsp:setProperty name="person" property="firstName" value="Marcus"/>
<jsp:setProperty name="person" property="lastName" value="Green"/>

then the above values will be printed out right??
 
VijayKumar Sivagnanam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I include the jspSetProperty line,

<jsp:setProperty name="person" property="firstName" value="Marcus"/>
<jsp:setProperty name="person" property="lastName" value="Green"/>

then the above values will be printed out right??


The place where you use <jsp:setProperty> matters. If you use it in the body of the <jsp:useBean> tag, then it will get executed if the object with the given id is not found in the given scope.

if it is used outside <jsp:useBean> as below, it will get executed for both new and existing object.
<jsp:useBean id="person" class="com.bean.Person" scope="session"/>

<jsp:setProperty name="person" property="firstName" value="Marcus"/>
<jsp:setProperty name="person" property="lastName" value="Green"/>

Hope you got it cleared. Let me know if you still have some doubts.
 
Joe Harry
Ranch Hand
Posts: 10085
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijaykumar,

Thanks for the explanation and I got it cleared. Thanks once again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic