二维码

struts2工作流程

1568 人阅读 | 时间:2019年12月29日 23:34


转自:http://huaxia524151.iteye.com/blog/1430148

工作流程

1.客户端提交一个HttpServletRequest请求(action或JSP页面)。

2.请求被提交到一系列Filter过滤器,如ActionCleanUp和FilterDispatcher等。

3.FilterDispatcher是Struts2控制器的核心,它通常是过滤器链中的最后一个过滤器。

4.请求被发送到FilterDispatcher后,FilterDispatcher询问ActionMapper时候需要调用某个action来处理这个Request。

5.如果ActionMapper决定需要调用某个action,FilterDispatcher则把请求交给ActionProxy进行处理。

6.ActionProxy通过Configuration Manager询问框架的配置文件struts.xml,找到调用的action类。

7.ActionProxy创建一个ActionInvocation实例,通过代理模式调用Action。

8.action执行完毕后,返回一个result字符串,此时再按相反的顺序通过Intercepter拦截器。

9.最后ActionInvocation实例,负责根据struts.xml中配置result元素,找到与之相对应的result,决定进一步输出。

基本简要流程:

1、客户端浏览器发出HTTP请求。
 
2、根据web.xml配置,该请求被FilterDispatcher接收。
 
3、根据struts.xml配置,找到需要调用的Action类和方法, 并通过IoC方式,将值注入给Aciton。
 
4、Action调用业务逻辑组件处理业务逻辑,这一步包含表单验证。
 
5、Action执行完毕,根据struts.xml中的配置找到对应的返回结果result,并跳转到相应页面。
 
6、返回HTTP响应到客户端浏览器。
 
struts2项目配置流程
 

1.新建web static project


struts2工作流程

如果没有tomcat的类库,需要添加。

 

2.将struts2的jar文件复制到WebContent/WEB-INF/lib目录中

 


struts2工作流程

 

3.配置web.xml文件


struts2工作流程
 <?xml version="1.0" encoding="UTF-8"?>

Xml代码

struts2工作流程
<web-app xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  
  
    <display-name>SSH2</display-name>  
  
    <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>  
  
    <welcome-file-list>  
        <welcome-file>index.html</welcome-file>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>  </web-app>
struts2工作流程

4.新建login.jsp,error.jsp,welcome.jsp文件

login.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"

Jsp代码

struts2工作流程
   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>  <title>用户登录</title>  </head>  <body>  <s:form action="login">  
    <s:textfield name="username" key="user"></s:textfield>  
    <s:textfield name="password" key="pwd"></s:textfield>  
    <s:submit key="login"/>  </s:form>  </body>  </html>
struts2工作流程

welcome.jsp

Java代码

struts2工作流程
<%@ 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>  <title>用户登录</title>  </head>  <body>  登录成功  
</body>  </html>
struts2工作流程

error.jsp

Java代码

struts2工作流程
<%@ 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>  <title>用户登录</title>  </head>  <body>  登录失败  
</body>  </html>
struts2工作流程

5.src目录下新建struts.xml、message.properties文件

struts.xml

Xml代码

struts2工作流程
<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "http://struts.apache.org/dtds/struts-2.0.dtd">  
  <struts>  
  
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
    <constant name="struts.devMode" value="true" />  
      
    <constant name="struts.custom.i18n.resources" value="message" />  
    <constant name="struts.i18n.encoding" value="UTF-8" />  
  
    <!-- Add packages here -->  
    <package name="auth" extends="struts-default">  
        <action name="login" class="com.tim4lover.ssh2.auth.action.LoginAction">  
            <result name="input">/login.jsp</result>  
            <result name="error">/error.jsp</result>  
            <result name="success">/welcome.jsp</result>  
        </action>  
    </package>  </struts>
struts2工作流程

message.properties

Java代码

struts2工作流程
loginPage=登录页面  
errorPage=错误页面  
successPage=成功页面  
failTip=对不起,您不能登录!  
successTip=欢迎,{0},您已经登录!  
user=用户名  
pwd=密码  
login=登录
struts2工作流程

message.properties文件的编码格式为utf-8,还必须用native2ascii命令来处理该国际化资源文件。

src目录下新建 toUTF-8.bat,运行。

toUTF-8.bat

Java代码

native2ascii -encoding UTF-8 message.properties message_zh_CN.properties

运行,生成message_zh_CN.properties

6.新建action类

Java代码

struts2工作流程
package com.tim4lover.ssh2.auth.action;  
  
import com.opensymphony.xwork2.ActionContext;  
import com.opensymphony.xwork2.ActionSupport;  
  
public class LoginAction extends ActionSupport {  
    private static final long serialVersionUID = 1L;  
  
    private String username;  
    private String password;  
  
    @Override  
    public String execute() throws Exception {  
        if("admin".equals(username) && "123456".equals(password)) {  
            ActionContext.getContext().getSession().put("user", getUsername());  
            return SUCCESS;  
        }else {  
            return ERROR;  
        }  
    }  
  
    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;  
    }  
      
}
struts2工作流程

7.配置struts.xml,action到jsp的映射。


struts2工作流程

 

  • struts2工作流程

  • 大小: 86.9 KB

  • struts2工作流程

  • 大小: 7.6 KB

  • struts2工作流程

  • 大小: 3.3 KB

  • struts2工作流程

  • 大小: 10.1 KB


©著作权归作者所有:来自ZhiKuGroup博客作者没文化的原创作品,如需转载,请注明出处,否则将追究法律责任 来源:ZhiKuGroup博客,欢迎分享。

评论专区
  • 昵 称必填
  • 邮 箱选填
  • 网 址选填
◎已有 0 人评论
搜索
作者介绍
30天热门
×
×
本站会员尊享VIP特权,现在就加入我们吧!登录注册×
»
会员登录
新用户注册
×
会员注册
已有账号登录
×