一、struts2发送处理请求流程
客户端发送请求 —> tomcat接收请求 -> 根据项目工程加载处理web.xml -> 把请求交给struts filter来处理,struts filter交给doFilter处理 -> doFilter根据struts.xml找到对应的namespace、action及action处理完成返回的信息来显示调用对应的result页面 -> 返回给客户端。
二、struts2请求接收方式
1、采用基本类型接收请求参数(get/post)
在Action类中定义与请求参数同名的属性,struts2就能自动接收请求参数并赋予给同名属性。
1)get方法接收请求参数
请求路径:http://172.22.6.9:8888/struts2/login.action?username=etccbw&password=123456
employee.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> <!-- 配置struts2处理请求后缀 --> <constant name="struts.action.extension" value="do,action"></constant> <package name="employee" extends="struts-default"> <action name="login" class="com.casv.action.employeeAction" method="all"> <result name="allemp">/emp/allpage.jsp</result> </action> </package> </struts>employeeAction.java
public class employeeAction extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } // 查询 public String all() { System.out.println("查询员工信息...,"); return "allemp"; } // 添加 public String add() { return this.all(); } }allpage.jsp
<body> username:${username},password:${password} </body>2)post方法接收请求参数
请求路径:http://localhost:8888/struts2/
employee.xml、employeeAction.java及allpage.jsp不变,添加login.jsp
<form action="${pageContext.request.contextPath}/login.action" method="post"> <table align="center"> <tr> <td colspan="2">用户登陆</td> </tr> <tr> <td>账号:</td> <td><input type="text" name="username" size="30px;"></td> </tr> <tr> <td>密码:</td> <td><input type="text" name="password" size="30px;"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form>2、采用复合类型接收请求参数
employee.xml
<struts> <!-- 配置struts2处理请求后缀 --> <constant name="struts.action.extension" value="do,action"></constant> <package name="employee" extends="struts-default"> <action name="login" class="com.casv.action.employeeAction" method="all"> <result name="allemp">/emp/allpage.jsp</result> </action> </package> </struts>empEntity.java
public class empEntity { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
employeeAction.java
public class employeeAction extends ActionSupport { empEntity emp; public empEntity getEmp() { return emp; } public void setEmp(empEntity emp) { this.emp = emp; } // 查询 public String all() { System.out.println("查询员工信息...,"); return "allemp"; } }
login.jsp
<form action="${pageContext.request.contextPath}/login.action" method="post"> <table align="center"> <tr> <td colspan="2">用户登陆</td> </tr> <tr> <td>账号:</td> <td><input type="text" name="emp.username" size="30px;"></td> </tr> <tr> <td>密码:</td> <td><input type="text" name="emp.password" size="30px;"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form>allpage.jsp
<body> username:${emp.username},password:${emp.password} </body>说明:当类中的参数比较多时,建议采用复合类型,较少两者均可。