Learn Java for Web Development

(Tina Meador) #1

132 CHAPTER 3: Best Practices in Java EE Web Development


The Action


The JSTL set tag or the action sets the value of a scoped variable or a property of a target
object. The action is a better alternative to using the JSP action. Unlike


, which allows you to set only the bean property, the tag can do the following:
   Set bean properties
 Set Map values
 Create scoped variables on the page, request, session, or application scope

Table 3-10 describes the attributes of the action.


Table 3-10. Attributes


Name Type Description

value Object Expression to be evaluated.
var String Name of the exported scoped variable to hold the value specified in the action.
scope String Scope for var.
target Object Target object whose property will be set. This must evaluate to a JavaBeans object
with the setter property property or to a java.util.Map object.
property String Name of the property to be set in the target object.

The tag is used to do the following:


   Set the value of a scoped variable in any JSP scope.
 Set a property of a specified target object. The target must evaluate to a
JavaBean or Map object.

Setting the Value of a Scoped Variable in Any JSP Scope


One of the tasks of the action is to set variables that can be used by other actions on pages.


Here’s the syntax:


<c:set value=""value""var=""varName" " [scope=""{page|request|session|application}""]/>


Listing 3-36 shows an example of using to set the value of the scoped variable helloVar.


Listing 3-36. Using



Listing 3-36 creates an attribute named helloVar with the value "hello" in the default scope, which
is page scope. You can also create attributes in another scope, say session scope; in that case, you
need to specify the scope with attribute scope="". Listing 3-37 creates a variable on session
scope using .

Free download pdf