struts2 优点

来源:互联网 发布:微信群控软件破解版 编辑:程序博客网 时间:2024/05/04 04:05
 

上午

struts2有以下优点:
1 > Struts2没有像struts1那样跟Servlet API和strutsAPI有着紧密的耦合,Struts2的应用可以不依赖于Servlet API和struts API。Struts2的这种设计属于无侵入式设计,而Struts1却属于侵入式设计。
public class OrderListAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
 }
}
2> Struts2提供了拦截器,利用拦截器可以进行AOP编程,实现如权限拦截等功能。
3> Strut2提供了类型转换器,我们可以把特殊的请求参数转换成需要的类型。在Struts1中,如果我们要实现同样的功能,就必须向Struts1的底层实现BeanUtil注册类型转换器才行。
4> Struts2提供支持多种表现层技术,如:JSP、freeMarker、Velocity等
5> Struts2的输入校验可以对指定方法进行校验,解决了Struts1长久之痛。


搭建Struts2环境时,我们一般需要做以下几个步骤的工作:
1》找到开发Struts2应用需要使用到的jar文件.
2》编写Struts2的配置文件
3》在web.xml中加入Struts2 MVC框架启动配置


struts2.1以后就需要fileupload的jar包


通过包来管理功能相似的模块


在struts1中,通过<action path=“/test/helloworld”>节点的path属性指定访问该action的URL路径。在struts2中,情况就不是这样了,

访问struts2中的action的URL路径由两部份组成:包的命名空间+action的名称,

例如访问本例子HelloWorldAction的URL路径为:/test/helloworld (注意:完整路径为:http://localhost:端口/内容路径/test/helloworld)。另外我们也可以加上.action后缀访问此Action。


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

基于网页上传文件大小不要超过5M
否则应考虑开发插件来保证上传的稳定


下午

struts2对于每一个请求,都会创建一个新的action

struts1:create-->cache

动态方法调用(不推荐使用该方法,建议关闭)
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>

使用通配符定义action


自定义类型转换器


ActionContext:
The ActionContext is the context in which anAction is executed. Each context is basically a container of objects anaction needs for execution like the session, parameters, locale, etc.


The ActionContext is thread local which means that values storedin the ActionContext are unique per thread. See the ThreadLocal classfor more information. The benefit of this is you don't need to worryabout a user specific action context, you just get it:
ActionContext context = ActionContext.getContext();
Finally, because of the thread local usage you don't need to worry about making your actions thread safe.

 

ServletActionContext:
Web-specific context information foractions. This class subclasses ActionContext which provides access tothings like the action name, value stack, etc. This class adds accessto web objects like servlet parameters, request attributes and thingslike the HTTP session.

文件上传
FileUtils工具类

自定义拦截器

拦截器栈

因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defaultStack,这样应用才可以使用struts2框架提供的众多功能。

如果希望包下的所有action都使用自定义的拦截器,可以通过<default-interceptor-refname=“permissionStack”/>把拦截器定义为默认拦截器。注意:每个包只能指定一个默认拦截器。另外,一旦我们为该包中的某个action显式指定了某个拦截器,则默认拦截器不会起作用。

 

原创粉丝点击