JavaServer Pages - Action Elements
(Page 7 of 7 )
The last set of JSP elements we will look at are the action elements. These elements are also known as Standard Actions. Standard actions are defined by the JSP specification (which is one reason why they are called standard).
As we will see in the next chapter, we can define our own actions that can be used in a JSP page.
The JSP 2.0 specification defines these standard actions:
- <jsp:useBean>
- <jsp:setProperty>
- <jsp:getProperty>
- <jsp:param>
- <jsp:include>
- <jsp:forward>
- <jsp:plugin>, <jsp:params>, <jsp:fallback>
- <jsp:attribute>
- <jsp:body>
- <jsp:invoke>
- <jsp:doBody>
We will look at <jsp:include>, <jsp:forward>, and <jsp:param> later in the chapter.
The <jsp:plugin>, <jsp:params>, and <jsp:fallback> elements are used to include applets or JavaBeans in the HTML page generated by a JSP page. Using these over hand-coding the HTML allows the server to create browser-specific HTML from the JSP tags. These tags are not discussed further in this book.
The elements <jsp:attribute> and <jsp:body> are used with standard and custom actions. The elements <jsp:invoke> and <jsp:doBody> are only valid in tag libraries, which we will cover in the next chapter.
The <jsp:useBean> Action
This element makes a JavaBean available to the page. A JavaBean (which is not the same as an Enterprise JavaBean, or EJB) is simply a Java class that follows certain requirements. The two requirements that are important for our purposes are:
- The JavaBean class has a no-argument constructor.
- Every property of the bean that is provided for client use has a method to set the value of the parameter, and a method to get the value of the parameter. The methods have this form:
publictypegetSomeParameter() { return someParameter; } public boolean isSomeParameter() { return someBooleanParameter; }
public void setSomeParameter(typesomeParameter) {
// Set the parameter
}
The name of every setter and getter uses the name of the parameter, with the first letter capitalized, appended to the token set, get, or is. The getter method has the form isXXX() for boolean properties, and getXXX() otherwise.
The <jsp:useBean> element has these attributes:
Attribute | Description |
id | The name used to access the bean in the rest of the page. It must be |
| unique. It is essentially the variable name that references the bean |
| instance. |
scope | The scope of the bean. Valid values are page, request, session, or |
| | application. The default is page. See the Scope section below for more |
| information. |
class | The fully qualified class name of the bean class. |
beanName | The name of a bean, as expected by the instantiate() method of the |
| | java.beans.Beans class. Most often you will use the class attribute, |
| rather than beanName. Refer to the JavaBeans specification at |
| | http://java.sun.com/products/javabeans for more information on how to |
| supply a name to the instantiate() method. |
type | The type to be used for the variable that references the bean. This |
| | follows Java rules, so it can be the class of the bean, any parent class of |
| the bean, or any interface implemented by the bean or by a parent class. |
The <jsp:useBean> element causes the container to try to find an existing instance of the object in the specified scope and with the specified id. If no object with the specified id is found in that scope, and a class or bean name is specified, the container will try to create a new instance of the object. You can use the class, beanName, and type attributes in these combinations:
- classCreates an instance of the class that can be referred to by the given id.
- class, typeCreates an instance of the given class; the variable that refers to the bean will have the given type.
- beanName, typeCreates an instance of the given bean; the variable that refers to the bean will have the given type.
- typeIf an object of the given type exists in the session, the id will refer to that object.
You must create a reference to a JavaBean using the <jsp:useBean> element before you can use <jsp:setProperty> or <jsp:getProperty>.
The <jsp:setProperty> Action
Sets the property for a JavaBean. The <jsp:setProperty> element has these attributes:
Attribute | Description |
name | The id of the bean. |
property | The name of the property to set. |
| | The value can explicitly name a property of the bean, in which case the setXXX() method for the property will be called. |
| | The value can also be "*", in which case, the JSP will read all the parameters that were sent by the browser with the clients request, and set the properties in the bean that have the same names as the parameters in the request. We will see an example of this in the next Try It Out section. |
param | The parameter name in the browser request whose value will be used to set the property. Allows the JSP to match properties and parameters with different names. |
value | The value to assign to the property. |
The name and property attributes are always required. The param and value elements are mutually exclusive. If neither param nor value are used, the jsp:setProperty element attempts to use the request parameter with the same name as the property attribute. I will show examples of request parameters in the next section.
Suppose we have a JavaBean that holds information about a user of the system. This bean might look like this:
public class User {
private String id;
private String surname;
public void setId(String id) { this.id = id; }
public String getId() { return id; }
public void setSurname(String surname) { this.surname
= surname; }
public String getSurname() { return surname; }
}
Here is one simple example of using the <jsp:setProperty> element with a literal value, and an expression:
<jsp:useBean id="userA" class="User" />
<jsp:setProperty id="userA" property="surname"
value="Smith" />
<jsp:setProperty id="userA" property="id"
value="<%= validateId("86753") %>" />
After this code in the compiled JSP executes, the surname property of the instance of User has a value of "Smith" and the id property has whatever value is returned by the hypothetical validateId() expression. What occurs is that the JSP translator takes the elements above and translates them into code that creates an instance of the User class, and then calls the setSurname() and setId() methods of the object.
The <jsp:getProperty> Action
This element retrieves the value of a property from a JavaBean. The <jsp:getProperty> element has these attributes:
Attribute | Description |
name | The id of the bean. |
property | The name of the property to get. |
The name and property attributes are always required. When used within a JSP, the value of the property will be output as part of the response. Given the example in the previous section, you could write template data that used <jsp:getProperty> like this:
The user with id <jsp:getProperty id="userA"
property="id" />
has a surname of <jsp:getProperty id="userA"
property="surname" />
When the JSP page is translated into Java code, this will result in calls to the getSurname() and getId() methods of the object. The return values are then output with the template data to the response, so that the client sees this in his browser:
The user with id 86753 has a surname of Smith
This article will be concluded next week.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter three of Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413). Check it out at your favorite bookstore today. Buy this book now.
|
|