1 搭建环境:
eclipase:Luna Release (4.4.0)
sturts2:2.3.28 下载地址:http://download.csdn.net/detail/chuck_kui/9513090
2. 类:
模拟DAO : Dao.java
package com.baidu.struts2.CRUD; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; // DAO public class Dao { private static Map<Integer,Employee> empls = new LinkedHashMap<Integer,Employee>(); // 使用 LinkedHashMap 是为了使显示看起来的更有顺序 // 使用静态 模拟 数据库 static { empls.put(1001,new Employee(1001, "Li", "Ming")); empls.put(1002,new Employee(1002, "Liu", "Ming")); empls.put(1003,new Employee(1003, "Zhang", "Ming")); empls.put(1004,new Employee(1004, "Wang", "Ming")); empls.put(1005,new Employee(1005, "Gong", "Ming")); } public List<Employee> getEmployees(){ return new ArrayList<>(empls.values()); } public void save(Employee e){ long time = -System.currentTimeMillis(); e.setEmplId((int)time); empls.put(e.getEmplId(), e ); } public void delete(Integer emplId){ empls.remove(emplId); } public void update(Employee e ){ empls.put(e.getEmplId(),e); } public Employee get(Integer emplId){ return empls.get(emplId); } }
package com.baidu.struts2.CRUD; // 提供实体类 public class Employee { private Integer emplId; private String firstName; private String lastName; public Employee() { super(); } public Employee(Integer emplId, String firstName, String lastName) { super(); this.emplId = emplId; this.firstName = firstName; this.lastName = lastName; } public Integer getEmplId() { return emplId; } public void setEmplId(Integer emplId) { this.emplId = emplId; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
package com.baidu.struts2.CRUD; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Preparable; public class EmployeeAction implements RequestAware,ModelDriven<Employee>,Preparable{ private Dao dao = new Dao(); private Integer emplId; public void setEmplId(Integer emplId) { this.emplId = emplId; } public String list(){ request.put("empls", dao.getEmployees()); return "list"; } public String save(){ dao.save(employee); return "success"; } public void prepareSave(){ // 为save 定制的prepare方法 employee = new Employee(); } public String delete(){ dao.delete(emplId); return "success"; } /** * edit()更新时需要回显,如何回显, 就是下面的三步 * 1. 获取传入的employeeId; employee.getEmployeeId() * 2. 更加emplId 数据库中获取Employee 对象emp * 3. 把栈顶的employee 对象装配好 * * Employee emp = dao.get(employee.getEmplId()); * employee.setFirstName(emp.getFirstName()); * employee.setLastName(emp.getLastName()); * * 现在被 ModelDriven 和 Preparable 却需要在Struts.xml 中配置 paramsPrepareParamsStack 为默认值栈 */ public String edit(){ return "edit"; } public void prepareEdit(){ // 为edit 定制的prepare方法 employee = dao.get(emplId); } public String update(){ dao.update(employee); return "success"; } public void prepareUpdate(){ // 为update 定制的prepare方法 employee = new Employee(); } // 为save() 准备request private Map<String, Object> request; @Override public void setRequest(Map<String, Object> arg0) { this.request = arg0; } // 为edit()回显准备 private Employee employee ; @Override public Employee getModel() { return employee; } // 为getModel() 通过model @Override public void prepare() throws Exception { System.out.println("prepare...."); } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="emp-list">Employee All List </a> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:debug></s:debug> <center> <br><br> <s:form action="emp-save" > <s:textfield name="firstName" label="FirstName"></s:textfield> <s:textfield name="lastName" label="LastName"></s:textfield> <s:submit></s:submit> </s:form> <br><br> <hr> <table border="1" cellpadding="10" cellspacing="0"> <thead> <tr> <td>EmployeeID</td> <td>FirstName</td> <td>LastName</td> <td>Edit</td> <td>Delete</td> </tr> </thead> <tbody> <s:iterator value="#request.empls" var="emp"> <tr> <td>${emplId }</td> <td>${firstName}</td> <td>${lastName }</td> <td><a href="emp-edit?emplId=${emplId }">Edit</a></td> <td><a href="emp-delete?emplId=${emplId }">Delete</a></td> </tr> </s:iterator> </tbody> </table> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:debug></s:debug> <center> <br><br> <s:form action="emp-update" > <s:hidden name="emplId"></s:hidden> <s:textfield name="firstName" label="FirstName"></s:textfield> <s:textfield name="lastName" label="LastName"></s:textfield> <s:submit></s:submit> </s:form> </center> </body> </html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <!-- 修改PrepareInterceptor 拦截器的 alwaysInvokePrepare 属性置为 false --> <interceptors> <!-- ① 为需要修改的拦截器栈起个名字 baiduStack --> <interceptor-stack name="baiduStack"> <!-- ② baiduStack 拦截器栈 指向 paramsPrepareParamsStack--> <interceptor-ref name="paramsPrepareParamsStack"> <!--③ 把 paramsPrepareParamsStack 拦截器栈的 prepare 拦截器 的alwaysInvokePrepare 属性值 修改为false--> <param name="prepare.alwaysInvokePrepare">false</param> </interceptor-ref> </interceptor-stack> </interceptors> <!--④ 启用修改后的拦截器栈 baiduStack--> <default-interceptor-ref name="baiduStack" /> <!-- 配置使用 paramsPrepareParamsStack 作为默认的拦截器栈 <default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref> --> <action name="emp-*" class="com.baidu.struts2.CRUD.EmployeeAction" method="{1}"> <result name="{1}">/emp-{1}.jsp</result> <result name="success" type="redirectAction">emp-list</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
index.jsp
emp-list.jsp
emp-edit.jsp