Struts Framework: A Sample Struts Application : Struts : J2EE : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  J2EE   » [  Struts  ]  Screenshots 
 



Struts Framework: A Sample Struts Application







/*
# Title:      Struts: The Complete Reference 
# Author(s): James Holmes
# Publisher: McGraw-Hill/Osborne, 2004
# ISBN:      0-07-223131-9

*/


public class Java2sTempClassNameStruts-TheCompleteReference-Chapter-{

//index.jsp
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" 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>

//search.jsp
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" 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 id="size" name="searchForm" property="results"/>
<logic:equal name="size" value="0">
<center><font color="red"><b>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 id="result" 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>

package com.jamesholmes.minihr;

public class Employee
{
  private String name;
  private String ssNum;

  public Employee(String name, String ssNum) {
    this.name = name;
    this.ssNum = ssNum;
  }

  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;
  }
}

package com.jamesholmes.minihr;

import java.util.ArrayList;

public class EmployeeSearchService
{
  /* Hard-coded sample data. Normally this would come from a real data
     source such as a database. */
  private static Employee[] employees = 
  {
    new Employee("Bob Davidson""123-45-6789"),
    new Employee("Mary Williams""987-65-4321"),
    new Employee("Jim Smith""111-11-1111"),
    new Employee("Beverly Harris""222-22-2222"),
    new Employee("Thomas Frank""333-33-3333"),
    new Employee("Jim Davidson""444-44-4444")
  };

  // Search for employees by name.
  public ArrayList searchByName(String name) {
    ArrayList resultList = new ArrayList();

    for (int i = 0; i < employees.length; i++) {
      if (employees[i].getName().toUpperCase().indexOf(name.toUpperCase()) != -1) {
        resultList.add(employees[i]);
      }
    }

    return resultList;
  }

  // Search for employee by social security number.
  public ArrayList searchBySsNum(String ssNum) {
    ArrayList resultList = new ArrayList();

    for (int i = 0; i < employees.length; i++) {
      if (employees[i].getSsNum().equals(ssNum)) {
        resultList.add(employees[i]);
      }
    }

    return resultList;
  }
}

package com.jamesholmes.minihr;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public final class SearchAction extends Action
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception
  {
    EmployeeSearchService service = new EmployeeSearchService();
    ArrayList results;

    SearchForm searchForm = (SearchFormform;

    // Perform employee search based on what criteria was entered.
    String name = searchForm.getName();
    if (name != null && name.trim().length() 0) {
      results = service.searchByName(name);
    else {
      results = service.searchBySsNum(searchForm.getSsNum().trim());
    }

    // Place search results in SearchForm for access by JSP.
    searchForm.setResults(results);

    // Forward control to this Action's input page.
    return mapping.getInputForward();
  }
}

package com.jamesholmes.minihr;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

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 ActionError("error.search.criteria.missing"));
    }

    /* Validate format of social security number if
       it has been entered. */
    if (ssNumEntered && !isValidSsNum(ssNum.trim())) {
      errors.add("ssNum",
        new ActionError("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 == || i == 6) {
        if (ssNum.charAt(i!= '-') {
          return false;
        }
      else if ("0123456789".indexOf(ssNum.charAt(i)) == -1) {
        return false;
      }
    }

    return true;
  }
}


           
       
Download: Struts-TheCompleteReference-Chapter-2.zip   ( 1,439  K )  
Related examples in the same category
1.  Exercise 1: Building your first Struts ApplicationHas Download File Exercise 1: Building your first Struts Application
2.  Exercise 2: Improving your first Struts Application Has Download File Exercise 2: Improving your first Struts Application
3.  Exercise 3: Using JSTL, Struts-EL etcHas Download File Exercise 3: Using JSTL, Struts-EL etc
4.  Struts Recipes: Build Struts with AntHas Download File
5.  Using bean:resource to expose the struts.config.xml to your viewHas Download File
6.  Create a pluggable validator for cross-form validation 2Has Download File Create a pluggable validator for cross-form validation 2
7.  Struts: Generate a response with XSL Has Download File
8.   Hibernate and Struts Has Download File  Hibernate and Struts
9.   In-container testing with StrutsTestCase and Cactus Has Download File
10.  Exercise 4: Applying Gof and J2EE Patterns:Deploy to WebLogic and Test Has Download File
11.  Exercise 5: Search, List, Action Chaining, Editable List Form Has Download File
12.  Exercise 6: Paging Has Download File
13.  Exercise 7: Better Form and Action Handling Has Download File
14.  Exercise 8: Creating Struts ModulesHas Download File
15.  Exercise 9: Using Commons Validator with Struts Has Download File
16.  Exercise 10: Using Struts and Tiles Has Download File
17.  Essential Struts ActionHas Download File Essential Struts Action
18.  A Full Struts ApplicationHas Download File A Full Struts Application
19.  Struts Creating the ViewHas Download File Struts Creating the View
20.  Struts: Creating the ModelHas Download File Struts: Creating the Model
21.  Struts: Creating the ControllerHas Download File Struts: Creating the Controller
22.  Creating Custom TagsHas Download File Creating Custom Tags
23.  The Struts TagsHas Download File The Struts Tags
24.  The Struts and TagsHas Download File The Struts and Tags
25.  Web Services and the Validator and Tile PackagesHas Download File Web Services and the Validator and Tile Packages
26.  Struts Framework ValidatorHas Download File
27.  Struts Framework: TilesHas Download File Struts Framework: Tiles
28.  Struts Framework: Declarative Exception HandlingHas Download File
29.  Struts: Internationalizing Struts ApplicationsHas Download File
30.  Securing Struts ApplicationsHas Download File
31.  Testing Struts ApplicationsHas Download File
32.  Struts exampleHas Download File Struts example
33.  Blank Struts templateHas Download File Blank Struts template
34.  Struts FrameworkHas Download File
35.  Struts: bank applicationHas Download File Struts: bank application
36.  Struts applicationHas Download File Struts application
37.  Struts application 2Has Download File Struts application 2








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.