struts2工作流程
从struts2Demo1中看struts2的工作流程
login.jsp
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%@ taglib prefix="s" uri="/struts-tags"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 9 <title>Login</title> 10 </head> 11 <body> 12 13 <s:form action="Login"> 14 <s:label>登录系统</s:label> 15 <s:textfield name="userName" label="用户名:"></s:textfield> 16 <s:password name="password" label="密码:"></s:password> 17 <s:submit value="登录"></s:submit> 18 </s:form> 19 20 </body> 21 </html>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 8 <display-name>struts2Demo1</display-name> 9 10 <filter> 11 <filter-name>struts2</filter-name> 12 <!-- StrutsPrepareAndExecuteFilter用来初始化struts2,并处理所有的Web请求 --> 13 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 14 </filter> 15 16 <filter-mapping> 17 <filter-name>struts2</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping> 20 21 <welcome-file-list> 22 <welcome-file>index.html</welcome-file> 23 <welcome-file>index.jsp</welcome-file> 24 </welcome-file-list> 25 26 </web-app>
LoginAction.java
1 package edu.dzu.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class LoginAction extends ActionSupport { 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 private String userName; 13 private String password; 14 15 @Override 16 public String execute(){ 17 if( userName.equals("admin") && password.equals("1234") ){ 18 return SUCCESS; 19 }else{ 20 return LOGIN; 21 } 22 } 23 24 public String getUserName() { 25 return userName; 26 } 27 public void setUserName(String userName) { 28 this.userName = userName; 29 } 30 public String getPassword() { 31 return password; 32 } 33 public void setPassword(String password) { 34 this.password = password; 35 } 36 37 }
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <package name="package" extends="struts-default"> 9 <global-results> 10 <result name="login">/login/login.jsp</result> 11 </global-results> 12 <action name="Login" class="edu.dzu.action.LoginAction"> 13 <result name="success">/login/loginSuccess.jsp</result> 14 </action> 15 </package> 16 <!-- Add packages here --> 17 18 </struts>
loginSuccess.jsp
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%@ taglib prefix="s" uri="/struts-tags" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 9 <title>Login Success</title> 10 </head> 11 <body> 12 <h1> 用户 <s:property value="userName" /> 登录成功</h1> 13 <hr/> 14 </body> 15 </html>
Struts2工作流程:
1、访问JSP页面http:localhost:8080/strutsDemo1/login/login.jsp
2、提交表单后数据提交给/struts2Demo/Login.action
3、struts2截获所有请求
4、查找struts.xml文件,找到Login.action的映射类LoginAction
5、生成一个LoginAction实例,将提交的数据set到action实例中
6、调用action实例的execute()方法处理业务逻辑
7、forward到JSP页面显示结果,并销毁该action实例
login.jsp -> web.xml -> struts.xml -> LoginAction -> struts.xml -> loginSuccess.jsp or login.jsp
struts2官方文档中的struts2架构图
注意:
org.apache.struts2.dispatcher.FilterDispatcher
Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using
the ActionContextCleanUp filter in addition to this one