本篇文章是笔者在工作之余梳理关于struts2框架的知识内容,以便日后自己回顾以及记录一下自己对框架本身开始的认识。文中若有错误之处,望请指点迷津。
Struts2是一个基于MVC设计模式的Web应用框架,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。
2 第一个struts2程序
2.1 导入jar包
其中struts2核心jar包有以下8个:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
freemarker-2.3.18.jar
javassist-3.11.0.GA.jar
ognl-3.0.3.jar
struts2-core-2.3.1.1.jar【struts2核心包】
xwork-core-2.3.1.1.jar
freemarker是模板引擎,javassist是java字节码操作类库,ognl是对象图形导航语言.
因为会对xml进行操作,所以再导入两个jar包:
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar(dom4j的基础包,在使用XPath会调用)
2.2 创建helloAction,并且继承由struts2框架提供的ActionSupport类
import com.opensymphony.xwork2.ActionSupport;
public class helloAction extends ActionSupport {
public String helloMethod() throws Exception{
System.out.println("helloMethod");
return "ok";
}
}
2.3 在src目录下配置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>
<package name="app01Package" extends="struts-default" namespace="/">
<action name="helloRequest"
class="yang.zhiran.app01.helloAction"
method="helloMethod" >
<result name="ok" type="dispatcher">
/app01/ok.jsp
</result>
</action>
</package>
</struts>
2.4 在web.xml中配置struts2的核心过滤器
<filter>
<filter-name>
StrutsPrepareAndExecuteFilter
</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>
StrutsPrepareAndExecuteFilter
</filter-name>
<url-pattern>
/*
</url-pattern>
</filter-mapping>
2.5 在/WebRoot/app01/目录下创建ok.jsp页面
2.6 部署并启动tomcat服务器
在浏览器地址栏输入:
localhost:8080/mystruts2/helloRequest.action(可以不加.action后缀)
2.7 struts.xml配置文件说明
1)struts.xml文件需要这样开头:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
这个文件头可以在struts2-core-2.3.1.1.jar下的struts-2.0.dtd文件内容中找到。
2)struts标签的含义:
package标签用来分类管理具有相同功能的action。
extends属性表示继承struts-default,struts2-core.jar下的struts-default.xml内置了struts2框架的众多功能,继承之后可以直接重用struts2框架定义的功能。
namespace属性是名称空间,通过URL可以映射到具体的模块上。默认是”/”。
action标签是name指定请求名,class指定处理类全名路径,默认是ActionSupport类,method指定处理类下的具体方法,默认是execute方法。
result标签定义怎样响应结果,name值是处理类action指定的处理方法method返回的结果值,默认是success。type指结果类型,默认是dispatcher转发。
struts-default.xml中定义了10个结果类型,32个拦截器。
3)action访问扩展名(后缀)
struts2框架action的访问后缀名默认是*.action和无扩展名,这是因为struts2-core-2.3.1.1.jar/org.apache.struts2/struts.properties属性文件中定义了struts.action.extension=action,,
如果想要更改扩展名有两种方法:
1.在src/struts.xml文件中配置
<constant name="struts.action.extension" value="do,yzr"/>
2.在src/struts.properties文件中配置
struts.action.extension=do,yzr
配置顺序:struts.xml文件配置的优先级高于src/struts.properties文件配置
4)分模块加载struts.xml
在src目录下的struts.xml文件下可以使用以下方式加载加载进来.
<include file="yang/zhiran/test03/struts-test01.xml"></include>
5)Action的生命周期
每一个请求正确通过struts2框架映射过来时,就会创建Action实例,由web服务器在一次请求结束后,销毁该Action 实例,即Action实例会绑定在request域对象中,Action是多例模式,不需要考虑线程安全.
本文介绍了Struts2框架的基本概念及首个程序的搭建过程。包括核心jar包的引入、Action类的设计、struts.xml配置详解及核心过滤器的设置。
2万+

被折叠的 条评论
为什么被折叠?



