[ Team LiB ] Previous Section Next Section

Recipe 7.3 Setting the Properties of a JavaBean in a JSP

Problem

You want to set a JavaBean's properties to the values entered in a form.

Solution

Use the jsp:setProperty standard action, with its property attribute set to "*" and its class attribute set to the fully qualified class name of the JavaBean.

Discussion

The jsp:setProperty standard action has a built-in method for automatically mapping the values submitted in a form to a JavaBean's fields or variables. The names of the submitted parameters have to correspond to the names of the JavaBean's setter methods. Example 7-5 shows a setBean.jsp page that receives data from an HTML form:

<form method=post action="http://localhost:8080/home/setBean.jsp">

The JSP first instantiates an object of the type com.jspservletcookbook.UserBean using jsp:useBean. Then it sets the properties of the bean using jsp:setProperty. The name attribute of jsp:setProperty matches the id attribute of jsp:useBean. The property attribute of jsp:setProperty is simply set to "*".

Example 7-5. beanSet.jsp JSP that sets the UserBean's properties with form input
<%@page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<jsp:useBean id="userB" class="com.jspservletcookbook.UserBean" >

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

</jsp:useBean>
<html>
<head><title>Post Data Viewer</title></head>
<body>
<h2>Here is your posted data</h2>

    <strong>User name</strong>: 
    <c:out value="${userB.username}" /><br><br> 

     <strong>Department</strong>: 
    <c:out value="${userB.department}" /><br><br>

     <strong>Email</strong>: 
    <c:out value="${userB.email}" />

</body>
</html>

Example 7-5 uses the c:out element of the JSTL to display the bean's various values in a browser page. The value attribute of c:out uses the EL to acquire a property value, as in "${userB.email}". This syntax is the equivalent of calling the UserBean's getEmail( ) method. Example 7-6 shows the UserBean, which uses the JavaBean naming conventions to ensure that its properties can be properly set and accessed. Figure 7-3 shows the browser display of the values.

Figure 7-3. Displaying form input via a JavaBean
figs/jsjc_0703.gif

The jsp:setProperty action, as used in this recipe, sets the JavaBean's properties by using introspection to line up parameter names with the bean's setter methods. If the bean has a field named "Username," then the parameter name must be exactly "Username" and the setter method must be exactly "setUsername(String name)" (if the bean's field is a String). Watch out, it's case-sensitive!


Example 7-6. Encapsulating the posted data in a JavaBean
package com.jspservletcookbook;

public class UserBean implements java.io.Serializable{

String username;
String email;
String department;

public UserBean( ){}

public void setUsername(String _username){

    if(_username != null && _username.length( ) > 0)
        username = _username;
    else
         username = "Unknown";
}

public String getUsername( ){

    if(username != null)
        return username;
    else
        return "Unknown";}

public void setEmail(String _email){

     if(_email != null && _email.length( ) > 0)
        email = _email;
    else
         email = "Unknown";
}

public String getEmail( ){

 if(_email != null)
        return email;
    else
        return "Unknown";}

public void setDepartment(String _department){

     if(_department != null && _department.length( ) > 0)
        department = _department;
    else
         department = "Unknown";
}

public String getDepartment( ){

    if(department != null)
        return department;
    else
        return "Unknown";  }

}

Recipe 7.4 shows how to use a bean to validate form input, then set a scoped attribute to the input.

See Also

Recipe 7.2 on handling a POST request in a JSP; Recipe 7.4 on setting a scoped attribute to the value of a parameter; Recipe 7.6 on posting data from a JSP; Chapter 23 on using the JSTL.

    [ Team LiB ] Previous Section Next Section