| Author |
jspSetProperty Doubt??
|
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
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.
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
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
Joined: Dec 25, 2006
Posts: 26
|
|
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.
|
SCJP 1.2 <br />SCWCD (94%)<br />SCBCD (preparing)
|
 |
VijayKumar Sivagnanam
Greenhorn
Joined: Dec 25, 2006
Posts: 26
|
|
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
Joined: Sep 26, 2006
Posts: 8795
|
|
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
Joined: Dec 25, 2006
Posts: 26
|
|
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
Joined: Sep 26, 2006
Posts: 8795
|
|
Vijaykumar, Thanks for the explanation and I got it cleared. Thanks once again!
|
 |
 |
|
|
subject: jspSetProperty Doubt??
|
|
|