Alternative View Technologies


Reviewing the View Layer of the Mini HR Application

To solidify your understanding of the View layer of Struts applications, it will be helpful to review the View layer of the Mini HR application developed in Chapter 2. Doing so clearly illustrates the core components involved in creating the View layer.

Mini HR's View layer consists of a Form Bean, two JSPs, and a resource bundle properties file. The SearchForm Form Bean is shown next:

package com.jamesholmes.minihr;     import java.util.List;     import javax.servlet.http.HttpServletRequest;     import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage;     public class SearchForm extends ActionForm {   private String name = null;   private String ssNum = null;   private List results = null;       public void setName(String name) {     this.name = name;   }       public String getName() {     return name;   }       public void setSsNum(String ssNum) {     this.ssNum = ssNum;   }       public String getSsNum() {     return ssNum;   }       public void setResults(List results) {     this.results = results;   }       public List getResults() {     return results;   }       // Reset form fields.   public void reset(ActionMapping mapping, HttpServletRequest request)       {     name = null;     ssNum = null;      results = null;   }       // Validate form data.   public ActionErrors validate(ActionMapping mapping,     HttpServletRequest request)   {     ActionErrors errors = new ActionErrors();         boolean nameEntered = false;     boolean ssNumEntered = false;         // Determine if name has been entered.     if (name != null && name.length() > 0) {         nameEntered = true;     }             // Determine if social security number has been entered.     if (ssNum != null && ssNum.length() > 0) {       ssNumEntered = true;     }         /* Validate that either name or social security number        has been entered. */     if (!nameEntered && !ssNumEntered) {       errors.add(null,             new ActionMessage("error.search.criteria.missing"));     }         /* Validate format of social security number if          it has been entered. */     if (ssNumEntered && !isValidSsNum(ssNum.trim())) {         errors.add("ssNum",         new ActionMessage("error.search.ssNum.invalid"));     }         return errors;   }       // Validate format of social security number.   private static boolean isValidSsNum(String ssNum) {       if (ssNum.length() < 11) {        return false;     }         for (int i = 0; i < 11; i++) {       if (i == 3 || i == 6) {         if (ssNum.charAt(i) != '-') {           return false;         }       } else if ("0123456789".indexOf(ssNum.charAt(i)) == -1) {         return false;       }     }         return true;   } }

The SearchForm class is a basic Form Bean with a few properties and implementations for the reset( ) and validate( ) method hooks. Mini HR uses this Form Bean to capture search criteria from the search page using the name and ssNum fields. The results field is used to transfer search results back to the search page after a search has been performed.

The index.jsp page, shown next, is a simple JSP used as an example menu page for linking to Mini HR's functions:

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>     <html> <head> <title>ABC, Inc. Human Resources Portal</title> </head> <body>     <font size="+1">ABC, Inc. Human Resources Portal</font><br> <hr width="100%" noshade="true">     &#149; Add an Employee<br> &#149; <html:link forward="search">Search for Employees</html:link><br>     </body> </html>

This page is the opening page for the Mini HR application and provides a link to the Mini HR search page.

The search.jsp page shown here provides the core interface to the Mini HR search functionality. It serves as the search criteria page as well as the search results page.

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>     <html> <head> <title>ABC, Inc. Human Resources Portal - Employee Search</title> </head> <body>     <font size="+1"> ABC, Inc. Human Resources Portal - Employee Search </font><br> <hr width="100%" noshade="true">     <html:errors/>     <html:form action="/search">     <table> <tr> <td align="right"><bean:message key="label.search.name"/>:</td> <td><html:text property="name"/></td> </tr> <tr> <td></td> <td>-- or --</td> </tr> <tr> <td align="right"><bean:message key="label.search.ssNum"/>:</td> <td><html:text property="ssNum"/> (xxx-xx-xxxx)</td> </tr> <tr> <td></td> <td><html:submit/></td> </tr> </table>     </html:form>     <logic:present name="searchForm" property="results">     <hr width="100%" size="1" noshade="true">     <bean:size  name="searchForm" property="results"/> <logic:equal name="size" value="0"> <center><font color="red"><cTypeface:Bold>No Employees Found</b></font></center> </logic:equal>     <logic:greaterThan name="size" value="0"> <table border="1"> <tr> <th>Name</th> <th>Social Security Number</th> </tr> <logic:iterate  name="searchForm" property="results"> <tr> <td><bean:write name="result" property="name"/></td> <td><bean:write name="result" property="ssNum"/></td> </tr> </logic:iterate> </table> </logic:greaterThan>     </logic:present>     </body> </html>

This page uses Struts tag library tags to determine whether or not it is being executed before or after a search has been submitted. If the page is being displayed before a search, it simply displays the search criteria form. However, if the page is being displayed after a search, it displays the search criteria form in addition to the search results.

The MessageResources.properties resource bundle file, shown next, is used by the SearchForm Form Bean and the JSPs to retrieve externalized content from a central repository:

# Label Resources label.search.name=Name label.search.ssNum=Social Security Number     # Error Resources error.search.criteria.missing=Search Criteria Missing error.search.ssNum.invalid=Invalid Social Security Number errors.header=<font color="red"><cTypeface:Bold>Validation Error(s)</b></font><ul> errors.footer=</ul><hr width="100%" size="1" noshade="true"> errors.prefix=<li> errors.suffix=</li>

The SearchForm Form Bean uses this file to store validation error messages. The JSPs use this file to store field labels. Together they are able to leverage Struts' built-in resource bundle mechanism for externalizing content. Doing so allows for easy updates to the content and provides a simple interface for internationalizing the content if necessary.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net