【Struts2】9.Struts2

1.优点(相对于Struts1)
   Struts2是在WebWork2基础发展而来的,和Struts1一样,Struts2页属于MVC框架。不过需注意的是:尽管Struts2和Struts1名字类似,但两者在代码编写风格上几乎是不同的。Strut2相对于Struts1其优点在于:
    1).在软件设计上Struts没有像Struts1那样跟ServletAPI和Struts API有着紧密的耦合,Struts2的应用可以不依赖于ServletAPI和StrutsAPI。Struts2的这种设计属于无侵入式设计,而Struts1属于侵入式设计。
public class OrderListAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
    }
}
2).Struts2提供了拦截器,利用拦截器可以进行AOP编程,实现了如权限拦截等功能。
3).Struts2提供了类型转换器,我们可以把特殊的请求参数转换成需要的类型。在Struts1中,若我们需要实现同样的功能,必须 向Struts1的底层实现BeanUtil注册类型转换器才行。
4).Struts2提供支持多种表现层技术,如:JSP、freeMarker、Velocity等。
5).Struts2的输入校验可以对指定方法进行校验,解决了Struts1长久之痛。
6).Struts2提供了全局范围、包范围和Action范围的国际化资源文件管理实现。

2.开发环境搭建
    1)找到需要的jar包
    基本jar包有:
    struts2-core-xxx.jar   struts2框架的核心类库
    xwork-xxx.jar  Xwork类库
    ognl-xxx.jar     struts2框架通过其读写对象的属性
    freemarker-xxx.jar     
    commons-logging-xxx.jar 日志包   struts2使用这个日志包支持Log4j和JDK的日志记录
    commons-fileUpload-xxx.jar      文件上传组件

    2)配置struts.xml
    3)在web.xml文件中添加struts2的启动配置
在Struts1.x中,Struts框架是通过Servlet启动的,在Struts2中,Struts框架是通过Filter启动的,在web.xml中的配置是:
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <!--自从Struts2.1.4后,FilterDispatcher已经标注为过时 
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        -->  
</filter>  
<filter-mapping>
     <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>   
在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。
注意:Struts2读取到struts.xml的内容后,以JavaBean形式存放在内存中,以后Struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件。

3.action的访问方式:namespace/action名

4.action的访问顺序:
    先获取访问路径,查看该命名空间是否存在,若不存在,则逐级确认,直到命名空间存在,然后查看在该空间下,action是否存在,若不存在,则去到默认命名空间下寻找该action
  获取请求路径的URI,例如url是: http://server/struts2/path1/path2/path3/test.action, 则:
    a.首先寻找namespace为/path1/path2/path3的package,若不存在这个package则执行步骤b;若存在,则在这个package中寻找名字为test的action,当在该package中找不到action时就会直接跑到默认namespace的package里面去寻找action(默认命名空间为空字符串""),若默认namespace的package中也找不到该action,页面提示找不到action;
    b.  寻找namespace为/path1/path2的package,若不存在这个package则执行步骤c;若存在,则在这个package中寻找名字为test的action,当在该package中找不到action时就会直接跑到默认namespace的package里面去寻找action,若默认namespace的package中也找不到该action,页面提示找不到action;
    c.寻找namespace为/path1的package,若不存在这个package则执行步骤d;若存在,则在这个package中寻找名字为test的action,当在该package中找不到action时就会直接跑到默认namespace的package里面去寻找action,若默认namespace的package中也找不到该action,页面提示找不到action;
    d.寻找namespace为/的package,若存在,则在这个package中寻找名字为test的action,当在该package中找不到action或者不存在这个package时,都会去默认namespace的package里面去寻找action,若也找不到该action,页面提示找不到action。

5.struts.xml配置文件
5.1.配置中的默认值
 <package name="test" namespace="/" extends="struts-default">    	
    	<action name="helloworld" class="com.xx.xx.action.helloworldAction" method="execute">
    	 	<result name="success">/pages/hello.jsp</result>           	
        </action>
    </package>
a.若没有为action指定class,默认是ActionSupport;
b.若没有为action指定method,默认是execute;
c.若没有为result指定name属性,默认是success;
5.2.result的转发类型
result 配置类似于Struts1中的forward,但Struts2中提供了多种结果类型,常用的有dispatcher(默认值  转发)、redirect t(重定向,注意重定向rediect方式无法定向到WEB-INF下的jsp中)、redirectAction(重定向到一个Action中):通过redirect也可以便捷的实现该功能、 chain(转发到Action中)、plainText()、stream等。
在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性。
eg:
<result type="rediect">/view.jsp?id=${id}</result>
若重定向的action在同一包下:
<result type="rediectAction">helloworld</result>
若重定向的action在别的命名空间下:
<result type="rediectAction">
    <param name="actionName">helloworld</param>
    <param name="namespace">/test</param>
</result>
plainText:显示原始文件内容
<result name="source" type="plainText">
    <param name="location">/xxx.jsp</param>
    <param name="charSet">UTF-8</param><!-- 指定读取文件的编码格式 -->
</result>
5.3.配置中的常量
常量可以在struts.xml或struts.properties中配置,配置方式为:
在struts.xml文件中配置
<constant name="struts.action.extension" value="do"/>
在struts.properties文件中配置
struts.action.extension=do
因为常量可以在多个配置文件中进行定义,所以需了了解Struts2加载常量的搜索顺序:
struts-default.xml  ——》  struts-plugin.xml  ——》 struts.xml ——》 struts.properties——》web.xml
若在多个文件中配置了同一个常量,则后一个文件中配置的常量会覆盖前面文件中配置的常量。

常用常量
<!--  指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输出 -->
<constant name="struts.i18n.encoding" value="utf-8"/>
<!-- 指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理,若用户需要执行多个请求后缀则多个后缀之间用英文,隔开  -->
<constant name="struts.action.extension" value="do"/>
<!--  设置浏览器是否缓存静态内容,默认值为true(生产环境中使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!--  当Struts2 的配置文件修改后,系统是否自动重新加载该文件默认值为false( 生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息  -->
<constant name="struts.devMode" value="true"/>
<!-- 默认的视图主题  -->
<constant name="struts.ui.theme" value="simple"/>
<!-- 与Spring集成时,指定由Spring复制action对象的创建  -->
<constant name="struts.objectFactory" value="spring"/>
<!-- strut2是否支持动态方法调用,默认值为true  -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!-- 上传文件的大小限制  -->
<constant name="struts.multipart.maxSize" value="10701096"/>

5.4.动态方法调用
若Action中存在多个方法时,我们可以使用 !+方法名调用指定方法。
public class XxxAction {
	private String message;
	public String execute throws Exception{
		this.message = "test1";
		return "success";
	}
	public String other throws Exception{
		this.message = "test2";
		return "success";
	}
}
假设访问上面的action的URL为:/struts/test/Xxx.action
要访问action的other方法,则可这样调用: /struts/test/Xxx!other.action
若不想使用动态方法调用,则可在配置文件中将其关掉。
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>

5.5.使用通配符配置
public class XxxAction {
	private String message;
	public String execute throws Exception{
		this.message = "test1";
		return "success";
	}
	public String other throws Exception{
		this.message = "test2";
		return "success";
	}
}

<package name="test" namespace="/" extends="struts-default">    	
    	<action name="xxx_*" class="com.xx.xx.action.XxxAction" method="{1}">
    	 	<result name="success">/pages/hello.jsp</result>           	
        </action>
    </package>
访问other方法的URL为:/test/xxx_other.action

6.Struts2的处理流程
用户请求→StrutsPrepareAndExecuteFilter →Interceptor(Struts2内置的一些拦截器或用户自定义拦截器)→Action(用户编写的action类,类似Struts1中的Action)→Result(类似Struts1中的forward)→Jsp/html→响应用户

StrutsPrepareAndExecuteFilter是Struts2框架的核心控制器,它负责拦截由<url-pattern>/*</url-pattern>指定的所有用户请求,当用户请求到达时,该Filter会过滤用户的请求。默认情况下,若用户请求的路径不带后缀或后缀以.action结尾,这是请求将被转入Struts2框架处理,否则Struts2框架将忽略过该请求的处理。当请求转入Struts2框架处理时会先经过一系列的拦截器,然后再到Action,与Struts1不同,Struts2对用户的每一次请求都会创建一个Action,所以Struts2中的Action是线程安全的。



注:参考黎活明教学视频总结
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值