`

struts2工作流程

 
阅读更多

工作流程

 

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



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

 

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

 


 

3.配置web.xml文件



 <?xml version="1.0" encoding="UTF-8"?>

<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>
 

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

 

login.jsp<%@ 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>
<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>

 welcome.jsp

<%@ 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>

 error.jsp

<%@ 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>
 

 

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

 

struts.xml

<?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>

 message.properties

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

 

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

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

 

toUTF-8.bat

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

运行,生成message_zh_CN.properties

 

6.新建action类  

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;
	}
	
}
 

 

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

 


 

  • 大小: 86.9 KB
  • 大小: 7.6 KB
  • 大小: 3.3 KB
  • 大小: 10.1 KB
分享到:
评论

相关推荐

    Struts2工作流程图

    虽然Struts2号称是一个全新的框架,但这仅仅是相对Struts 1而言。Struts 2 与Struts 1相比,确实有很多革命性的改进,但它并不是新发布的新框架,而是在另一个赫赫有名的框架:WebWork基础上发展起来的。从某种程度...

    Struts2工作流程

    Struts2的工作流程及struts2配置文件中各属性的含义

    struts2小程序 struts2代码

    花了3个小时才搭建出来的struts2小程序

    struts2工作原理

    struts2工作原理、请求响应流程。。。。。。。。。。。。。。。

    Struts2详细工作流程

    Struts 2的工作流程相对于Struts 1要简单,与WebWork框架基本相同,所以说Struts 2是WebWork的升级版本。Struts 2框架按照模块来划分,可以分为Servlet Filters、Struts核心模块、拦截器和用户实现部分。

    Struts2执行流程

    Struts2执行流程 1. web.xml 部署描述符 2. FilterDispatcher 实现StrutsStatics, Filter接口 (1)Filter:一个filter是一个对象用于执行过滤任务为每个请求资源(一个servlet或静态内容),或响应一个资源,或两者.过滤...

    Struts2框架程序示例

    Struts2框架程序示例

    超详细struts2执行流程图

    这张流程图 深刻的描述了 struts2的执行流程 帮助了解Struts2

    Struts2的工作流程及配置文件

    Struts2的工作流程及配置文件

    struts2建立流程

    Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理...

    struts2框架处理流程

    包含了struts2的处理流程以及struts2的配置文件,并附有处理流程显示图,更加清楚直观。

    Struts2 运行流程分析

    Struts2 运行流程分析,Struts2 运行流程分析,Struts2 运行流程分析

    struts2流程与流程图

    一个请求在Struts 2框架中的处理大概分为以下几个步骤。  客户端提交一个(HttpServletRequest)请求,如上文在浏览器中输入 http://localhost: 8080/bookcode/ch2/Reg.action就是提交一个(HttpServletRequest)...

    struts2 入门示例程序

    struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序struts2 入门示例程序

Global site tag (gtag.js) - Google Analytics