Tuesday, 24 September 2013

jsp:setProperty action

<jsp:setProperty>


Defn:
Sets property values in a Bean.


  • In a similar manner I would , there could be a need where I want to set the property values in my jsp page again so that I can use this bean again the property value is someother place later point of time, How can i do that? I can do that using the jsp setProperty() method so similarty jsp:getProperty set the property values in a bean and basically setProperty and getProperty actions are used in consuntion with jsp:useBean action so whenever you use jsp:useBean you have the setProperty and getProperty actions, can't exists by its own.
  • So syntax is similar to getProperty:

Syntax:
<jsp:setProperty name="{beanInstanceName}"
         property="*"/property="propertyName"
        param="{parameter name"}
        value="{param value}" }/>


  •  This syntax is similar to <jsp:getProperty>, so  name again would be beanInstanceName 
  • Now lets ignore property= *(star), and you can either set a value for single property name (i.e: property="propertyName"). Lets say I want to set a value for user name , that is property="username"
  • And value would be whatever value you want to set to the "username" attribute 
  • Now alternatively I can also set a value based on certain input entered in my page so lets say that I have  a field for username for my page and I want to update that value whatever value value was entered in that field for username to this property in this userBean , so How can I do that ? I can basically map that to the "param", (i.e param="{parameter name"}) so here this jsp  setProperty element also have the param attribute( see the syntax) , so i can simply map that field name , where i want to , which would be updated for the user to this "param" attribute. In this case whatever value is entered in that username input textbox would be updated to this bean for that particular property (ie. property="*"/property="propertyName")  if we say "username" here to be updated to the username property. 

  • Now What does the *(star) does, if I put a * here means? it means that basically for all the property values in my bean, simply map (param) all parameters to the same name, so lets say that I have two properties in my bean that is user id and username and similarly I have two parameters two input fields in my jsp page , here one is username and one is user id so here the catches the you should have the same name in the input field for the so that it matches the input field name with the property name so  if have the properties as username and user id my input text box name should also be username and user id so that this can automatically map these values to that input fields and update the value of the bean with whatever values you entered input fields 
  • So that's how you can use the * version of the properties attribute so basically you can use jsp:setProperty element in side the jsp useBean  body and and out side the jsp useBean body as well. What is the difference between using inside and outside? Ans is when you use it out side the jsp use Bean body it automatically sets the value of that particular property to the value which you said irrespective of what value it was earlier when it was brought over from the servlet right ! so lets say that i set a value of userid =1 in servlet  and I passed this to the jsp  page and in the jsp usebean action it instantiate the useBean and the value of the id brought in us 1 (one) and suddenly in my page I again do a setProperty i say userid=2 now and it simply sets the value to 2 now , it changes the value whatever was brought but if I use <jsp:setProperty> action inside my jsp use bean body it will change the value or basically it will set the value of that property only when the userBean was not  found so we said earlier that this user useBean action if there is already bean with this name (i.e beanInstanceName)  instantiated its simply gets the reference to that but if it does not find any thing it is going to create a new object and it is going to instantiate the bean class so if you use the <jsp:setProperty> within the bean class , it sets the property value only if it didnt find the bean class  and instantiated the bean class in to the jsp otherwise it would not set the value of that property to the new value simply it is going to retain whatever it is brought from the servlet .


Example:

<jsp:useBean id="userBean"
        scope="session"
        class="com.sharmani.UserBean">
<jsp:setProperty name="userBean"
       property="userName"
       value="Rajendra"/>
</jsp:userBean>
<jsp:getProperty name="userBean" property="userName"/>


  • Jsp:useBean tag with set and get property, i just explained earlier here we are using the setProperty action  (i.e: <jsp:setProperty name="userBean")  within the body of the useBean jsp, here you see, whatever id i have specified in the <jsp:useBean> element i have used as name as setProperty and getProperty actions 

No comments:

Post a Comment