struts2官方入门案列curd 编辑

    偶然在查看文档时,看到这个demo,后来认真看了下真是麻雀虽小,很单一的struts2的增删改查,但是却从各方面诠释着struts2这一开源框架的精妙设计和丰富的可定制性。文档上提供是片段式的代码讲解,且是英文的,所以这里记录一下,方面以后查看。

      和以前一样,先上效果图:

 

    图一:

 

 

 

     图二:

 

 

 

     图三:

 

 

 

    图四:

 

 

            虽然从图上看的话,以上功能简单,但是代码里,时刻体现着该框架的设计之优秀,首先,我们新建一个web功能CusManager,并加入struts2的7个必要jar包。

           

             第一步,新建两个实体类Department和Employee作POJO,代码如下:

Java代码
  1. package com.aurifa.struts2.tutorial.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Department implements Serializable {  
  6.     Integer departmentId;  
  7.   
  8.     String name;  
  9.   
  10.     public Department() {  
  11.     }  
  12.   
  13.     public Department(Integer departmentId, String name) {  
  14.         this.departmentId = departmentId;  
  15.         this.name = name;  
  16.     }  
  17.   
  18.     public Integer getDepartmentId() {  
  19.         return departmentId;  
  20.     }  
  21.   
  22.     public void setDepartmentId(Integer departmentId) {  
  23.         this.departmentId = departmentId;  
  24.     }  
  25.   
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.   
  34. }  

 

Java代码
  1. package com.aurifa.struts2.tutorial.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Employee implements Serializable {  
  6.     private Integer employeeId;  
  7.   
  8.     private Integer age;  
  9.   
  10.     private String firstName;  
  11.   
  12.     private String lastName;  
  13.   
  14.     private Department department;  
  15.   
  16.     public Employee() {  
  17.     }  
  18.   
  19.     public Employee(Integer employeeId, String firstName, String lastName,  
  20.             Integer age, Department department) {  
  21.         this.employeeId = employeeId;  
  22.         this.firstName = firstName;  
  23.         this.lastName = lastName;  
  24.         this.age = age;  
  25.         this.department = department;  
  26.     }  
  27.   
  28.     public Department getDepartment() {  
  29.         return department;  
  30.     }  
  31.   
  32.     public void setDepartment(Department department) {  
  33.         this.department = department;  
  34.     }  
  35.   
  36.     public Integer getEmployeeId() {  
  37.         return employeeId;  
  38.     }  
  39.   
  40.     public void setEmployeeId(Integer employeeId) {  
  41.         this.employeeId = employeeId;  
  42.     }  
  43.   
  44.     public Integer getAge() {  
  45.         return age;  
  46.     }  
  47.   
  48.     public void setAge(Integer age) {  
  49.         this.age = age;  
  50.     }  
  51.   
  52.     public String getFirstName() {  
  53.         return firstName;  
  54.     }  
  55.   
  56.     public void setFirstName(String firstName) {  
  57.         this.firstName = firstName;  
  58.     }  
  59.   
  60.     public String getLastName() {  
  61.         return lastName;  
  62.     }  
  63.   
  64.     public void setLastName(String lastName) {  
  65.         this.lastName = lastName;  
  66.     }  
  67. }  

 

           第二步,定义dao层的接口,代码如下:

Java代码
  1. package com.aurifa.struts2.tutorial.dao;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. public interface DepartmentDao {  
  7.     public List getAllDepartments();  
  8.     public Map getDepartmentsMap();  
  9. }  

 

Java代码
  1. package com.aurifa.struts2.tutorial.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.aurifa.struts2.tutorial.model.Employee;  
  6.   
  7. public interface EmployeeDao {  
  8.     public List getAllEmployees();  
  9.     public Employee getEmployee(Integer id);  
  10.     public void update(Employee emp);  
  11.     public void insert(Employee emp);  
  12.     public void delete(Integer id);  
  13. }  

 

          第三步,完成上述接口的实现类,代码如下:

Java代码
  1. package com.aurifa.struts2.tutorial.dao;  
  2.   
  3. import java.util.*;  
  4.   
  5. import com.aurifa.struts2.tutorial.model.Department;  
  6.   
  7. public class DepartmentNoDBdao implements DepartmentDao {  
  8.     private static List departments;  
  9.     private static Map departmentsMap;  
  10.     static {  
  11.         departments = new ArrayList();  
  12.         departments.add(new Department( new Integer(100), "Accounting" ));  
  13.         departments.add(new Department( new Integer(200), "R & D"));  
  14.         departments.add(new Department( new Integer(300), "Sales" ));  
  15.         departmentsMap = new HashMap();  
  16.         Iterator iter = departments.iterator();  
  17.         while( iter.hasNext() ) {  
  18.             Department dept = (Department)iter.next();  
  19.             departmentsMap.put(dept.getDepartmentId(), dept );  
  20.         }  
  21.   
  22.      }  
  23.     public List getAllDepartments() {  
  24.         return departments;  
  25.     }  
  26.     public Map getDepartmentsMap() {  
  27.         return departmentsMap;  
  28.     }  
  29. }  

 

Java代码
  1. package com.aurifa.struts2.tutorial.dao;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.commons.logging.Log;  
  9. import org.apache.commons.logging.LogFactory;  
  10.   
  11. import com.aurifa.struts2.tutorial.model.Department;  
  12. import com.aurifa.struts2.tutorial.model.Employee;  
  13.   
  14. public class EmployeeNoDBdao implements EmployeeDao {  
  15.     private static Map departmentsMap;  
  16.     private static ArrayList employees;  
  17.   
  18.     static {  
  19.         employees = new ArrayList();  
  20.         employees.add(new Employee(new Integer(1), "John""Doe"new Integer(36), new Department(new Integer(100), "Accounting")));  
  21.         employees.add(new Employee(new Integer(2), "Bob""Smith"new Integer(25), new Department(new Integer(300), "Sales")));  
  22.         DepartmentDao deptDao = new DepartmentNoDBdao();  
  23.         departmentsMap = deptDao.getDepartmentsMap();  
  24.     }  
  25.   
  26.     Log logger = LogFactory.getLog(this.getClass());  
  27.   
  28.     public List getAllEmployees() {  
  29.         return employees;  
  30.     }  
  31.   
  32.     public Employee getEmployee(Integer id) {  
  33.         Employee emp = null;  
  34.         Iterator iter = employees.iterator();  
  35.         while (iter.hasNext()) {  
  36.             emp = (Employee)iter.next();  
  37.             if (emp.getEmployeeId().equals(id)) {  
  38.                 break;  
  39.             }  
  40.         }  
  41.         return emp;  
  42.     }  
  43.   
  44.     public void update(Employee emp) {  
  45.         Integer id = emp.getEmployeeId();  
  46.         for (int i = 0; i < employees.size(); i++) {  
  47.             Employee tempEmp = (Employee)employees.get(i);  
  48.             if (tempEmp.getEmployeeId().equals(id)) {  
  49.                 emp.setDepartment((Department)departmentsMap.get(emp.getDepartment().getDepartmentId()));  
  50.                 employees.set(i, emp);  
  51.                 break;  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56.     public void insert(Employee emp) {  
  57.         int lastId = 0;  
  58.         Iterator iter = employees.iterator();  
  59.         while (iter.hasNext()) {  
  60.             Employee temp = (Employee)iter.next();  
  61.             if (temp.getEmployeeId().intValue() > lastId) {  
  62.                 lastId = temp.getEmployeeId().intValue();  
  63.             }  
  64.         }  
  65.         emp.setDepartment((Department)departmentsMap.get(emp.getDepartment().getDepartmentId()));  
  66.         emp.setEmployeeId(new Integer(lastId + 1));  
  67.         employees.add(emp);  
  68.     }  
  69.   
  70.     public void delete(Integer id) {  
  71.         for (int i = 0; i < employees.size(); i++) {  
  72.             Employee tempEmp = (Employee)employees.get(i);  
  73.             if (tempEmp.getEmployeeId().equals(id)) {  
  74.                 employees.remove(i);  
  75.                 break;  
  76.             }  
  77.         }  
  78.     }  
  79. }  

 
          第四步,根据dao层,完成service层(因为代码较为简单,未明确分包),代码如下:

Java代码
  1. package com.aurifa.struts2.tutorial.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.aurifa.struts2.tutorial.dao.DepartmentDao;  
  6. import com.aurifa.struts2.tutorial.dao.DepartmentNoDBdao;  
  7.   
  8. public class DepartmentDaoService implements DepartmentService {  
  9.     private DepartmentDao dao;  
  10.   
  11.     public DepartmentDaoService() {  
  12.         this.dao = new DepartmentNoDBdao();  
  13.     }  
  14.   
  15.     public List getAllDepartments() {  
  16.         return dao.getAllDepartments();  
  17.     }  
  18. }  

 

Java代码
  1. package com.aurifa.struts2.tutorial.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.aurifa.struts2.tutorial.dao.EmployeeDao;  
  6. import com.aurifa.struts2.tutorial.dao.EmployeeNoDBdao;  
  7. import com.aurifa.struts2.tutorial.model.Employee;  
  8.   
  9. public class EmployeeDaoService implements EmployeeService {  
  10.     private EmployeeDao dao;  
  11.   
  12.     public EmployeeDaoService() {  
  13.         this.dao = new EmployeeNoDBdao();  
  14.     }  
  15.   
  16.     public List getAllEmployees() {  
  17.         return dao.getAllEmployees();  
  18.     }  
  19.   
  20.     public void updateEmployee(Employee emp) {  
  21.         dao.update(emp);  
  22.     }  
  23.   
  24.     public void deleteEmployee(Integer id) {  
  25.         dao.delete(id);  
  26.     }  
  27.   
  28.     public Employee getEmployee(Integer id) {  
  29.         return dao.getEmployee(id);  
  30.     }  
  31.   
  32.     public void insertEmployee(Employee emp) {  
  33.         dao.insert(emp);  
  34.     }  
  35. }  

 

           第五步,service层的接口,代码如下:

Java代码
  1. package com.aurifa.struts2.tutorial.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface DepartmentService {  
  6.     public List getAllDepartments();  
  7. }  

 

Java代码
  1. package com.aurifa.struts2.tutorial.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.aurifa.struts2.tutorial.model.Employee;  
  6.   
  7. public interface EmployeeService {  
  8.     public List getAllEmployees();  
  9.     public void updateEmployee(Employee emp);  
  10.     public void deleteEmployee(Integer id);  
  11.     public Employee getEmployee(Integer id);  
  12.     public void insertEmployee(Employee emp);  
  13. }  

 

      第六步,则是action层,代码如下:

Java代码
  1. package com.aurifa.struts2.tutorial.action;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.commons.logging.Log;  
  6. import org.apache.commons.logging.LogFactory;  
  7.   
  8. import com.aurifa.struts2.tutorial.model.Employee;  
  9. import com.aurifa.struts2.tutorial.service.DepartmentDaoService;  
  10. import com.aurifa.struts2.tutorial.service.DepartmentService;  
  11. import com.aurifa.struts2.tutorial.service.EmployeeDaoService;  
  12. import com.aurifa.struts2.tutorial.service.EmployeeService;  
  13. import com.opensymphony.xwork2.ActionSupport;  
  14. import com.opensymphony.xwork2.Preparable;  
  15.   
  16. public class EmployeeAction extends ActionSupport implements Preparable {  
  17.   
  18.     /** 
  19.      *  
  20.      */  
  21.     private static final long serialVersionUID = -6886717038958304064L;  
  22.   
  23.     private Log logger = LogFactory.getLog(this.getClass());  
  24.   
  25.     private static EmployeeService empService = new EmployeeDaoService();  
  26.   
  27.     private static DepartmentService deptService = new DepartmentDaoService();  
  28.   
  29.     private Employee employee;  
  30.   
  31.     private List employees;  
  32.   
  33.     private List departments;  
  34.   
  35.     public void prepare() throws Exception {  
  36.         departments = deptService.getAllDepartments();  
  37.         if (employee != null && employee.getEmployeeId() != null) {  
  38.             employee = empService.getEmployee(employee.getEmployeeId());  
  39.         }  
  40.     }  
  41.   
  42.     public String doSave() {  
  43.         if (employee.getEmployeeId() == null) {  
  44.             empService.insertEmployee(employee);  
  45.         } else {  
  46.             empService.updateEmployee(employee);  
  47.         }  
  48.         return SUCCESS;  
  49.     }  
  50.   
  51.     public String doDelete() {  
  52.         empService.deleteEmployee(employee.getEmployeeId());  
  53.         return SUCCESS;  
  54.     }  
  55.   
  56.     public String doList() {  
  57.         employees = empService.getAllEmployees();  
  58.         return SUCCESS;  
  59.     }  
  60.   
  61.     public String doInput() {  
  62.         return INPUT;  
  63.     }  
  64.   
  65.     /** 
  66.      * @return Returns the employee. 
  67.      */  
  68.     public Employee getEmployee() {  
  69.         return employee;  
  70.     }  
  71.   
  72.     /** 
  73.      * @param employee 
  74.      *            The employee to set. 
  75.      */  
  76.     public void setEmployee(Employee employee) {  
  77.         this.employee = employee;  
  78.     }  
  79.   
  80.     /** 
  81.      * @return Returns the employees. 
  82.      */  
  83.     public List getEmployees() {  
  84.         return employees;  
  85.     }  
  86.   
  87.     /** 
  88.      * @return Returns the departments. 
  89.      */  
  90.     public List getDepartments() {  
  91.         return departments;  
  92.     }  
  93. }  

 

           接着在同级目录下,我们添加该action的同名验证框架,代码如下:

Employeeaction-crud-validation.xml代码
  1. <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"  
  2.        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  3. <validators>  
  4.   <field name="employee.firstName">  
  5.      <field-validator type="requiredstring">  
  6.           <message key="errors.required.firstname"/>  
  7.       </field-validator>  
  8.   </field>  
  9.   <field name="employee.lastName">  
  10.      <field-validator type="requiredstring">  
  11.           <message key="errors.required.lastname"/>  
  12.       </field-validator>  
  13.   </field>  
  14.   <field name="employee.age">  
  15.      <field-validator type="required" short-circuit="true">  
  16.           <message key="errors.required.age"/>  
  17.       </field-validator>  
  18.       <field-validator type="int">  
  19.             <param name="min">18</param>  
  20.             <param name="max">65</param>  
  21.             <message key="errors.required.age.limit"/>  
  22.       </field-validator>  
  23.   </field>  
  24. </validators>  

 

           至此,代码部分大抵完成,下面在src下,新建所需的配置文件:

首先是log4j,代码如下:

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
  3. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  
  4.     <appender name="stdout" class="org.apache.log4j.ConsoleAppender">  
  5.         <layout class="org.apache.log4j.TTCCLayout"/>  
  6.     </appender>  
  7.       
  8.     <!-- log detail configuration -->  
  9.     <logger name="com.opensymphony.xwork">  
  10.         <level value="error"/>  
  11.         <appender-ref ref="stdout"/>  
  12.     </logger>  
  13.     <logger name="com.opensymphony.webwork">  
  14.         <level value="error"/>  
  15.         <appender-ref ref="stdout"/>  
  16.     </logger>  
  17.     <logger name="freemarker">  
  18.         <level value="warn"/>  
  19.         <appender-ref ref="stdout"/>  
  20.     </logger>  
  21.     <logger name="com.mevipro">  
  22.         <level value="debug"/>  
  23.         <appender-ref ref="stdout"/>  
  24.     </logger>  
  25.     <root>  
  26.         <level value="error"/>  
  27.         <appender-ref ref="stdout"/>  
  28.     </root>  
  29. </log4j:configuration>  

 

             其次是struts.xml,代码如下:

Xml代码
  1. <!DOCTYPE struts PUBLIC   
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.     <!-- Include webwork default (from the Struts JAR). -->  
  6.     <include file="struts-default.xml"/>  
  7.   
  8.     <!-- Configuration for the default package. -->  
  9.     <package name="default" extends="struts-default">  
  10.           
  11.         <!-- Default interceptor stack. -->  
  12.         <default-interceptor-ref name="paramsPrepareParamsStack"/>  
  13.         <action name="index" class="com.aurifa.struts2.tutorial.action.EmployeeAction" method="list">  
  14.             <result name="success">/jsp/employees.jsp</result>  
  15.             <!-- we don't need the full stack here -->  
  16.             <interceptor-ref name="basicStack"/>  
  17.         </action>  
  18.         <action name="crud" class="com.aurifa.struts2.tutorial.action.EmployeeAction" method="input">  
  19.             <result name="success" type="redirect-action">index</result>  
  20.             <result name="input">/jsp/employeeForm.jsp</result>  
  21.             <result name="error">/jsp/error.jsp</result>  
  22.         </action>  
  23.     </package>  
  24. </struts>  

 

            然后是struts的国际化文件,struts.properties和guest.properties,代码分别如下:

Struts.properties代码
  1. struts.custom.i18n.resources=guest  

 

Guest.properties代码
  1. #labels  
  2. application.title=Employee Maintenance Application  
  3. label.employees=Employees  
  4. label.delete=Delete  
  5. label.edit=Edit  
  6. label.employee.edit=Edit Employee  
  7. label.employee.add=Add Employee  
  8. label.firstName=First Name  
  9. label.lastName=Last Name  
  10. label.department=Department  
  11. label.age=Age  
  12.   
  13. #button labels  
  14. button.label.submit=Submit  
  15. button.label.cancel=Cancel  
  16.   
  17. ##-- errors  
  18. errors.prefix=<span style="color:red;font-weight:bold;">  
  19. errors.suffix=</span>  
  20. errors.general=An Error Has Occcured  
  21. errors.required.firstname=Name is required.  
  22. errors.required.lastname=Last name is required.  
  23. errors.required.age=Please provide an age.  
  24. errors.required.age.limit=Please provide an age between ${min} and ${max}.  
  25. errors.required.department=Department is required.  

 

 最后贴上运行时的jsp页面,代码不多,下面我分别贴出来,代码如下:

Index.jsp代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="Refresh" content="0;URL=index.action">  
  18.     <!--  
  19.     <link rel="stylesheet" type="text/css" href="styles.css">  
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     This is my JSP page. <br>  
  25.   </body>  
  26. </html>  

 

Employeeform.jsp代码
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <s:if test="employee==null || employee.employeeId == null">  
  4.     <s:set name="title" value="%{'Add new employee'}"/>  
  5. </s:if>  
  6. <s:else>  
  7.     <s:set name="title" value="%{'Update employee'}"/>  
  8. </s:else>  
  9.   
  10. <html>  
  11. <head>  
  12.     <link href="css/main.css" rel="stylesheet" type="text/css"/>  
  13.     <style>td { white-space:nowrap; }</style>  
  14.     <title><s:property value="#title"/></title>  
  15. </head>  
  16. <body>  
  17. <div class="titleDiv"><s:text name="application.title"/></div>  
  18. <h1><s:property value="#title"/></h1>  
  19. <%--<s:actionerror />--%>  
  20. <%--<s:actionmessage />--%>  
  21. <s:form action="crud!save.action" method="post">  
  22.     <s:textfield name="employee.firstName" value="%{employee.firstName}" label="%{getText('label.firstName')}" size="40"/>  
  23.     <s:textfield name="employee.lastName" value="%{employee.lastName}" label="%{getText('label.lastName')}" size="40"/>  
  24.     <s:textfield name="employee.age" value="%{employee.age}" label="%{getText('label.age')}" size="20"/>  
  25.     <s:select name="employee.department.departmentId" value="%{employee.department.departmentId}" list="departments" listKey="departmentId" listValue="name"/>  
  26. <%--    <s:select name="gender" list="%{#{'male':'Male', 'female':'Female'}}" />--%>  
  27.     <s:hidden name="employee.employeeId" value="%{employee.employeeId}"/>  
  28.     <s:submit value="%{getText('button.label.submit')}"/>  
  29.     <s:submit value="%{getText('button.label.cancel')}" name="redirect-action:index"/>  
  30. </s:form>  
  31. </body>  
  32. </html>  

 

Employees.jsp代码
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <html>  
  4. <head>  
  5.     <link href="css/main.css" rel="stylesheet" type="text/css"/>  
  6.     <title><s:text name="label.employees"/></title>  
  7. </head>  
  8. <body>  
  9. <div class="titleDiv"><s:text name="application.title"/></div>  
  10. <h1><s:text name="label.employees"/></h1>  
  11. <s:url id="url" action="crud!input" />  
  12. <a href="<s:property value="#url"/>">Add New Employee</a>  
  13. <br/><br/>  
  14. <table class="borderAll">  
  15.     <tr>  
  16.         <th><s:text name="label.firstName"/></th>  
  17.         <th><s:text name="label.lastName"/></th>  
  18.         <th><s:text name="label.age"/></th>  
  19.         <th><s:text name="label.department"/></th>  
  20.         <th>&nbsp;</th>  
  21.     </tr>  
  22.     <s:iterator value="employees" status="status">  
  23.         <tr class="<s:if test="#status.even">even</s:if><s:else>odd</s:else>">  
  24.             <td class="nowrap"><s:property value="firstName"/></td>  
  25.             <td class="nowrap"><s:property value="lastName"/></td>  
  26.             <td class="nowrap"><s:property value="age"/></td>  
  27.             <td class="nowrap"><s:property value="department.name"/></td>  
  28.             <td class="nowrap">  
  29.                 <s:url action="crud!input" id="url">  
  30.                     <s:param name="employee.employeeId" value="employeeId"/>  
  31.                 </s:url>  
  32.                 <a href="<s:property value="#url"/>">Edit</a>  
  33.                 &nbsp;&nbsp;&nbsp;  
  34.                 <s:url action="crud!delete" id="url">  
  35.                     <s:param name="employee.employeeId" value="employeeId"/>  
  36.                 </s:url>  
  37.                 <a href="<s:property value="#url"/>">Delete</a>  
  38.             </td>  
  39.         </tr>  
  40.     </s:iterator>  
  41.     </table>  
  42. </body>  
  43. </html>  

 

Error.jsp代码
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <html>  
  4. <head>  
  5.     <title>Error Page</title>  
  6.     <link href="<s:url value='/css/main.css'/>" rel="stylesheet" type="text/css"/>  
  7. </head>  
  8. <body>  
  9. <s:actionerror/>  
  10. <br/>  
  11. In order that the development team can address this error, please report what you were doing that caused this error.  
  12. <br/><br/>  
  13. The following information can help the development  
  14. team find where the error happened and what can be done to prevent it from  
  15. happening in the future.  
  16.   
  17. </body>  
  18. </html>  

 

以上页面引用到的css文件,代码如下:

Main.css代码
  1. html, body  {  
  2.     margin-left: 10px;  
  3.     margin-right: 10px;  
  4.     margin-bottom: 5px;  
  5.     color: black;  
  6.     background-color: white;  
  7.     font-family: Verdana, Arial, sans-serif;  
  8.     font-size:12px;  
  9. }  
  10. .titleDiv {  
  11.     background-color: #EFFBEF;  
  12.     font-weight:bold;  
  13.     font-size:18px;  
  14.     text-align:left;  
  15.     padding-left:10px;  
  16.     padding-top:10px;  
  17.     padding-bottom:10px;  
  18.     border:2px solid #8F99EF;  
  19. }  
  20. h1 { font-weight:bold; color: brown; font-size:15px; text-align:left;}  
  21.   
  22. td { font-size:12px; padding-right:10px; }  
  23. th { text-align:left; font-weight:bold; font-size:13px; padding-right:10px; }  
  24. .tdLabel { font-weight: bold; white-space:nowrap; vertical-align:top;}  
  25.   
  26. A { color:#4A825A; text-decoration:none;}  
  27. A:link { text-decoration:none;}  
  28. A:visited { text-decoration:none;}  
  29. A:hover { text-decoration:none; color: red;}  
  30.   
  31. .borderAll {  
  32.     border: 2px solid #8F99EF;  
  33. }  
  34.   
  35. .butStnd {  
  36.     font-family:arial,sans-serif;  
  37.     font-size:11px;  
  38.     width:105px;  
  39.     background-color:#DCDFFA ;color:#4A825A;font-weight:bold;  
  40. }  
  41.   
  42. .error {  
  43.     color: red;  
  44.     font-weight: bold;  
  45. }  
  46. .errorSection {  
  47.     padding-left:18px;  
  48.     padding-top:2px;  
  49.     padding-bottom:10px;  
  50.     padding-right:5px;  
  51. }  
  52.   
  53. .even { background-color: #EFFBEF; }  
  54. .odd { background-color: white; }  
  55.   
  56. .nowrap { white-space:nowrap; }  

              

           到这里,这个例子基本上就完成了,其主要亮点在action层的实现类和验证xml,而jsp页面的tag和struts.xml也比较耐看,将web.xml配置好struts2的监听以后,部署到tomcat之后,浏览器键入:localhost:8080/CusManager,即可得到以上效果图所示。

 

            代码我是全部都贴上了,连css也有,不会有任何地方的缺失,假使大家运行报错,请认真查看错误,并检查自己的web.xml的配置,除此之外不会有任何错误。

 

 

posted @ 2013-03-28 22:04  xinyuyuanm  阅读(290)  评论(0编辑  收藏  举报