Struts Framework: A Sample Struts Application : Struts « J2EE « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » J2EE » StrutsScreenshots 
Struts Framework: A Sample Struts Application
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

*/



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


           
       
Struts-TheCompleteReference-Chapter-2.zip( 1,439 k)
Related examples in the same category
1. Exercise 1: Building your first Struts ApplicationExercise 1: Building your first Struts Application
2. Exercise 2: Improving your first Struts Application Exercise 2: Improving your first Struts Application
3. Exercise 3: Using JSTL, Struts-EL etcExercise 3: Using JSTL, Struts-EL etc
4. Struts Recipes: Build Struts with Ant
5. Using bean:resource to expose the struts.config.xml to your view
6. Create a pluggable validator for cross-form validation 2Create a pluggable validator for cross-form validation 2
7. Struts: Generate a response with XSL
8.  Hibernate and Struts  Hibernate and Struts
9.  In-container testing with StrutsTestCase and Cactus
10. Exercise 4: Applying Gof and J2EE Patterns:Deploy to WebLogic and Test
11. Exercise 5: Search, List, Action Chaining, Editable List Form
12. Exercise 6: Paging
13. Exercise 7: Better Form and Action Handling
14. Exercise 8: Creating Struts Modules
15. Exercise 9: Using Commons Validator with Struts
16. Exercise 10: Using Struts and Tiles
17. Essential Struts ActionEssential Struts Action
18. A Full Struts ApplicationA Full Struts Application
19. Struts Creating the ViewStruts Creating the View
20. Struts: Creating the ModelStruts: Creating the Model
21. Struts: Creating the ControllerStruts: Creating the Controller
22. Creating Custom TagsCreating Custom Tags
23. The Struts TagsThe Struts Tags
24. The Struts and TagsThe Struts and Tags
25. Web Services and the Validator and Tile PackagesWeb Services and the Validator and Tile Packages
26. Struts Framework Validator
27. Struts Framework: TilesStruts Framework: Tiles
28. Struts Framework: Declarative Exception Handling
29. Struts: Internationalizing Struts Applications
30. Securing Struts Applications
31. Testing Struts Applications
32. Struts exampleStruts example
33. Blank Struts templateBlank Struts template
34. Struts Framework
35. Struts: bank application
36. Struts applicationStruts application
37. Struts application 2Struts application 2
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.