知识大全 从实例看struts2运行原理
Posted 知
篇首语:历史是一面镜子,它照亮现实,也照亮未来。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 从实例看struts2运行原理相关的知识,希望对你有一定的参考价值。
简单例子
先做一个最简单的struts 的例子 在浏览器中请求一个action 然后返回一个字符串到jsp页面上显示出来
第一步 把struts 最低配置的jar包加入的项目中
mons logging jar
freemarker jar
ognl jar
struts core jar
xwork jar
第二步 在web xml中加入拦截器配置
<filter>
<filter name>struts </filter name>
<filter class> apache struts dispatcher FilterDispatcher</filter class>
</filter>
<filter mapping>
<filter name>struts </filter name>
<url pattern>/*</url pattern>
</filter mapping>
第三步 把空的struts xml配置文件放到项目src下面
<struts>
</struts>
第四部 编写自定义的action类
package test;
import opensymphony xwork ActionSupport;
public class HelloAction extends ActionSupport
private String str;
public String hello()
this str = hello!!! ;
return success ;
public String getStr()
return str;
public void setStr(String str)
this str = str;
第五步 编写struts xml配置文件
<struts>
<package name= test namespace= /np extends= struts default >
<action name= hello class= test HelloAction method= hello >
<result name= success >/hello jsp</result>
</action>
</package>
</struts>
第六步 编写hello jsp文件
<%@ page language= java contentType= text/; charset=UTF pageEncoding= UTF %>
<%@ taglib prefix= s uri= /struts tags %>
<!DOCTYPE PUBLIC //W C//DTD HTML Transitional//EN >
<>
<head>
<meta equiv= Content Type content= text/; charset=UTF >
<title>Test</title>
</head>
<body>
<h ><s:property value= str /></h >
</body>
</>
第七步 启动tomcat 在浏览器中访问
hello 是项目名字
np 命名空间 对应namespace里面的字符串
hello action 其中hello对应action里面的字符串 action 表示请求的是一个action
运行机制
)客户端在浏览器中输入一个url地址
)这个url请求通过协议发送给tomcat
)tomcat根据url找到对应项目里面的web xml文件
)在web xml里面会发现有struts 的配置
)然后会找到struts 对应的struts xml配置文件
)根据url解析struts xml配置文件就会找到对应的class
)调用完class返回一个字String 根据struts xml返回到对应的jsp
struts 流程
上图来源于Struts 官方站点 是Struts 的整体结构
一个请求在Struts 框架中的处理大概分为以下几个步骤
) 客户端初始化一个指向Servlet容器(例如Tomcat)的请求
) 这个请求经过一系列的过滤器(Filter)
) 接着FilterDispatcher被调用 FilterDispatcher询问ActionMapper来决定这个请是否需要调用某个Action
) 如果ActionMapper决定需要调用某个Action FilterDispatcher把请求的处理交给ActionProxy
) ActionProxy通过Configuration Manager询问框架的配置文件 找到需要调用的Action类
) ActionProxy创建一个ActionInvocation的实例
) ActionInvocation实例使用命名模式来调用 在调用Action的过程前后 涉及到相关拦截器(Intercepter)的调用
) 一旦Action执行完毕 ActionInvocation负责根据struts xml中的配置找到对应的返回结果
cha138/Article/program/Java/ky/201311/28861相关参考