Wednesday, April 13, 2011

Chapter 41: Modifying Bean Properties

In the previous chapter, we saw how to use the jsp:useBean tag to include contents of a JavaBean into a JSP Page. What we havent seen is, how to alter the properties of the beans included in a JSP using the jseBean. This is what we are going to learn in this chapter.

So, lets get started!!!

Modifying the Properties of a Bean in a JSP:

If you have any level of Java programming experience, you would have definitely used regular JavaBeans in your code and would have definitely come across the getter and setter methods. Some people call them getter/setter while some call them mutator methods. The term used to refer to them is what is different. Their work is the same. They are used to retrieve or update the value of a bean.

Lets say we have a variable “name” in your bean. You will see the getter & setter methods as follows:

private String name = “”;
public String getName() {
return this.name;
}
public void setName(String val) {
this.name = val;
}

Looks familiar, isnt it?

If you want to get or modify an attribute of a Bean inside a JSP scriptlet, then you can do like below:

< % String city = myAddress.getCity(); % >
< % myAddress.setCity(“Singapore”) % >

Here, myAddress is the name of an object I already instantiated and then I invoke the getCity() & setCity() methods of that bean object.

Though this is a possible approach, there are other sophisticated methods of modifying the attributes of a bean. The preferred way to set and get properties of a Bean is to use the jsp:setProperty element to set a property and the jsp:getProperty to get the value of a property.

Exam Trivia:
If you place jsp:setProperty after, but outside of, a jsp:useBean element, it is always executed. However, if the jsp:setProperty is inside the jsp:useBean element body then jsp:setProperty is executed only if the jsp:useBean element instantiates the JavaBean. Remember that the body of the jsp:useBean element is not executed if it finds a JavaBean loaded already.

jsp:setProperty Action

You can use the jsp:setProperty to set the properties values of beans that have been referenced earlier in the JSP page. Though the name looks complicated and the way you use it differs, the purpose is the same as the setter methods of a regular Bean object

In the following example code snippet, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean already exists in memory, because it is not inside the body of the jsp:useBean tag.

< jsp : useBean id="beanName" ... / >
< jsp : setProperty name="beanName" property="propertyName" ... / >

However, in the next case, the jsp:setProperty is executed only the first time the JavaBean is instantiated. It is ignored if the JavaBean has already been loaded in memory, as would happen if the JavaBean was loaded on a previous page with application scope, or session scope by the same user.

< jsp : useBean id="beanName" ... >
< jsp : setProperty name="beanName" property="propertyName" ... / >
< / jsp:useBean >

There are two ways to assign values to JavaBean properties using the jsp:setProperty element.

Way 1: We just simple set the property its requisite value

< jsp : setProperty name="addressBean" property="name" value="< % = Anand % >" / >

The container assigns the value to the JavaBean attribute variable.

Way 2: We can use a shortcut between the request object parameter and the JavaBean property. You can tell the container which parameter to take the value from and which property of the bean, to assign that value to. A point to note here is that, the name in the request object doesn’t have to match the property name in the bean, but it is a good idea to keep them same to make your code cleaner and easier to understand.

< jsp : setProperty name="addressBean" property="state" param="state" / >

Or

< jsp : setProperty name="addressBean" property="state" / >

or

< jsp : setProperty name="addressBean" property="*" / >

In the first example - the property is the name of the attribute in the JavaBean, while the param is the name of the parameter in the request object.

In the second example – we have not used the param attribute because, it is optional if the names are same in the request object and the bean.

Alternately we could use the third option as well. We have used a wild card character “*” which tells the container to match as many request parameters with JavaBean properties as possible. With each match, the container takes the value from the request object and places it in the JavaBean property variable.

Let us quickly summarize the properties of the setProperty action

Attribute Definition
name The case sensitive name of a Bean instance defined by a element. The Bean instance must contain the property you want to set. The defining element must appear before the element in the same file.
property The name of the Bean property whose value you want to set. If you set propertyName to *, the tag will iterate over the current ServletRequest parameters, matching parameter names and value type(s) to property names and setter method type(s), setting each matched property to the value of the matching parameter. If a parameter has a value of “”, the corresponding property is not modified.
param The name of the request parameter (Web form or query string) whose value you want to give to a Bean property. If you omit param, the request parameter name is assumed to be the same as the Bean property name. If the param is not set in the Request object, or if it has the value of “”, the jsp:setProperty element has no effect. An action may not have both param and value attributes.
value The value to assign to the given property. This attribute can accept a request-time attribute expression as a value. An action may not have both param and value attributes.
jsp:getProperty Action

A action places the value of a Bean instance property into the implicit out object. The String representation is sent to the HTTP response stream. Unlike a scriptlet, which can get a value and assign it to a variable, the getProperty action converts the Bean property value to a String and immediately adds that String to the output stream. You cannot assign the value from this tag to a variable in most containers.

You use jsp:getProperty to get the property values of beans that have been referenced earlier in the JSP page.

Regarding when this action takes place, in the following case, the jsp:getProperty is executed regardless of whether a new bean was instantiated or an existing bean already exists in memory.

< jsp : useBean id="beanName" ... / >
< jsp : getProperty name="beanName" property="propertyName" ... / >

However, in the next case, the jsp:getProperty is executed only the first time the JavaBean is instantiated. It is ignored if the JavaBean has already been loaded in memory.

< jsp : useBean id="beanName" ... >
< jsp : getProperty name="beanName" property="propertyName" ... / >
< / jsp : useBean >

Let us quickly review the attributes of getProperty

Attribute Definition
name The case-sensitive name of a Bean instance defined by a element. The Bean instance must contain the property you want to set. The defining element must appear before the element in the same file.
property The name of the Bean property whose value you want to get. If you get a propertyName that doesn't exist, you will generate an error.

Previous Chapter: Chapter 40 - Using jsp:useBean

Next Chapter: Chapter 42 - JavaBean Scope

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers