struts2中异常处理和注解

一、异常处理

任何成熟的MVC框架都应该提供成熟的异常处理机制,当然可以在execute方法中手动捕捉异常,当捕捉到特定异常时,返回特定逻辑视图名——但这种处理方式非常烦琐,需要在execute方法中书写大量的catch块。最大的缺点还是在于异常处理与代码耦合,一旦需要改变异常处理方式,必须修改代码,这是一种相当糟糕的方式。最好的方式是可以通过声明式的方式管理异常处理。

 

1.Struts2允许通过Struts.xml文件来配置异常的处理。看execute方法:

// 处理用户请求的execute方法,该方法抛出所有异常

public String execute() throws Exception(){

    ……

}

上面的execute()方法可以抛出全部异常,这意味着重写该方法时,完全无须进行任何异常处理,而是把异常直接抛给Struts2框架处理:Struts2框架接收到Action抛出的异常之后,将根据struts.xml文件配置的异常映射,转入指定的视图资源。

 

2.开启Struts2的异常处理机制,需要一个拦截器,在struts.xml中配置

<interceptors>

    ……

<!--执行异常处理的拦截器-->

<interceptor name=”exception” class=”com.opensymphony.xwork.interceptor.ExceptionMapping.Interceptor” />

<!--Struts2默认的拦截器栈-->

<interceptor-stack name=”defaultStack”>

    ……

    <!--引用异常映射拦截器-->

    <interceptor-ref name=”exception” />

    ……

</interceptor-stack>

</interceptors>

 

二、注解

1struts2注解的作用

使用注解可以用来替换struts.xml配置文件。

2.导包或引入依赖

必须导入struts2-convention-plugin-2.3.15.jar

或引入struts注解依赖:

<dependency>

    <groupId>org.apache.struts</groupId>

    <artifactId>struts2-convention-plugin</artifactId>

    <version>2.5.10</version>

</dependency>

3.通过配置文件学习对应的注解

@Namespace()       代替 <package>的namespace属性

         value                 指定名称空间,一般为 /

 

@ParentPackage() 代替 <package>的extends属性

         value                 指定父包名称,一般为:struts-default

@Namespace(value="/")

@ParentPackage(value="struts-default")

可简写成:

@Namespace("/")

@ParentPackage("struts-default")

 

@Action()        代替struts.xml中的<action>元素

         value        指定访问路径,代替<action>中的name属性。

         results      指定局部结果,代替<action>下的<result>元素。

 

@Result()         代替 <result>元素

         name       指定结果名称

         location   指定结果路径

@Action(value="add", results={

    @Result(name="success", location="/index.jsp"),

         @Result(name="input", location="/add.jsp")

})

 

 

注解案例:

@Namespace("/")

@ParentPackage("struts-default")

publilc class TestAction extends ActionSupport{

 

         ************************************************************

         struts.xml配置:

         <action name="add" class="com.zking.Test" method="add">

             <result name="success">/index.jsp</result>

             <result name="input">/error.jsp</result>

         </action>

         ************************************************************

 

         @Action(value="add", results={

             @Result(name="success", location="/index.jsp"),

             @Result(name="input", location="/error.jsp")

         })

         public String add(){

                  ……

                  return SUCCESS;

         }

 

         @Action(value="del", results={

             @Result(name="success", location="/index.jsp")

         })

         public String del(){

                  ……

                  return SUCCESS;

         }

}

 

三、常用的constant总结

<struts>
    <!-- 把它设置为开发模式,发布时要设置为false -->
    <constant name="struts.devMode" value="true" />
    <!-- 设置在class被修改时是否热加载,发布时要设置为false -->
    <constant name="struts.convention.classes.reload" value="true"/>
    <!-- 自动动态方法的调用,使用这个设置后可以这样调用:action!method -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- 指定jsp文件所在的目录地址 -->
    <constant name="struts.convention.result.path" value="/WEB-INF/content/" />
    <!-- 使用struts-default默认的转换器,如果是rest的使用:rest-defaultrest需要restjar插件 -->
    <constant name="struts.convention.default.parent.package" value="struts-default"/>
    <!-- 用于配置包名后缀。默认为actionactionsstruts-->
    <constant name="struts.convention.package.locators" value="actions" />
    <!-- 用于配置类名后缀,默认为Action,设置后,Struts2只会去找这种后缀名的类做映射 -->
    <constant name="struts.convention.action.suffix" value="Action"/>
    <!-- 设置即使没有@Action注释,依然创建Action映射。默认值是false。因为Convention-Plugin是约定优于配置的风格,可以不通过注解根据预先的定义就能访问相应Action中的方法 -->
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <!-- 自定义jsp文件命名的分隔符 -->
    <constant name="struts.convention.action.name.separator" value="-" />
    <!-- 国际化资源文件名称 -->
    <constant name="struts.custom.i18n.resources" value="i18n" />
    <!-- 是否自动加载国际化资源文件  -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- 浏览器是否缓存静态内容 -->
    <constant name="struts.serve.static.browserCache" value="false" />
     <!-- 上传文件大小限制设置 -->
    <constant name="struts.multipart.maxSize" value="-1" />
    <!-- 主题,将值设置为simple,即不使用UI模板。这将不会生成额外的html标签 -->
    <constant name="struts.ui.theme" value="simple" />
    <!-- 编码格式 -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
</struts>

 

猜你喜欢

转载自blog.csdn.net/LYQ2332826438/article/details/81193989
今日推荐