struts2工作流程

mac2022-06-30  61

转自: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

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

 

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

 

 

3.配置web.xml文件

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

Xml代码

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

Jsp代码

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

Java代码

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

Java代码

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

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

Java代码

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

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

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

转载于:https://www.cnblogs.com/wxh04/p/4166477.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)