zoukankan      html  css  js  c++  java
  • Struts2利用注解实现action跳转

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了。

    要使用注解方式,我们必须添加一个额外包:struts2-convention-plugin-2.x.x.jar。

    实例为例,供参考:

    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE struts PUBLIC  
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
    4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
    5.     
    6. <struts>    
    7.     <!-- 请求参数的编码方式 -->  
    8.     <constant name="struts.i18n.encoding" value="UTF-8"/>  
    9.     <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->  
    10.     <constant name="struts.action.extension" value="action,do,htm"/>  
    11.     <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->  
    12.     <constant name="struts.configuration.xml.reload" value="true"/>  
    13.     <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->  
    14.     <constant name="struts.devMode" value="false"/>    
    15.     <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->  
    16.     <constant name="struts.serve.static.browserCache" value="false" />  
    17.     <!-- 指定由spring负责action对象的创建     
    18.     <constant name="struts.objectFactory" value="spring" />  
    19.     -->  
    20.     <!-- 是否开启动态方法调用 -->  
    21.     <constant name="struts.enable.DynamicMethodInvocation" value="false"/>  
    22. </struts>  

    action类的注解:

    1. package com.tjcyjd.web.action;    
    2.     
    3. import org.apache.struts2.convention.annotation.Action;    
    4. import org.apache.struts2.convention.annotation.ExceptionMapping;    
    5. import org.apache.struts2.convention.annotation.ExceptionMappings;    
    6. import org.apache.struts2.convention.annotation.Namespace;    
    7. import org.apache.struts2.convention.annotation.ParentPackage;    
    8. import org.apache.struts2.convention.annotation.Result;    
    9. import org.apache.struts2.convention.annotation.Results;    
    10.     
    11. import com.opensymphony.xwork2.ActionSupport;    
    12.     
    13. /**  
    14.  * Struts2基于注解的Action配置 
    15.  *   
    16.  */    
    17. @ParentPackage("struts-default")  
    18. @Namespace("/annotation_test")  
    19. @Results( { @Result(name = "success", location = "/main.jsp"),  
    20.         @Result(name = "error", location = "/error.jsp") })  
    21. @ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })  
    22. public class LoginAction extends ActionSupport {  
    23.     private static final long serialVersionUID = 2730268055700929183L;   
    24.     private String loginName;    
    25.     private String password;    
    26.     
    27.     @Action("login") //或者写成  @Action(value = "login")   
    28.     public String login() throws Exception {    
    29.     
    30.         if ("yjd".equals(loginName) && "yjd".equals(password)) {    
    31.             return SUCCESS;    
    32.         } else {    
    33.             return ERROR;    
    34.         }    
    35.     }    
    36.     @Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") })    
    37.     public String add() throws Exception {    
    38.         return SUCCESS;    
    39.     }    
    40.     public String getLoginName() {    
    41.         return loginName;    
    42.     }    
    43.     public void setLoginName(String loginName) {    
    44.         this.loginName = loginName;    
    45.     }    
    46.     public String getPassword() {    
    47.         return password;    
    48.     }    
    49.     public void setPassword(String password) {    
    50.         this.password= password;    
    51.     }    
    52. }  

    这样就完成了一个基于注解的action配置。

    总结常用的注解如下:

    Namespace:指定命名空间。
    ParentPackage:指定父包。
    Result:提供了Action结果的映射。(一个结果的映射)
    Results:“Result”注解列表
    ResultPath:指定结果页面的基路径。
    Action:指定Action的访问URL。
    Actions:“Action”注解列表。
    ExceptionMapping:指定异常映射。(映射一个声明异常)
    ExceptionMappings:一级声明异常的数组。
    InterceptorRef:拦截器引用。
    InterceptorRefs:拦截器引用组。
  • 相关阅读:
    Mvc请求管道中的19个事件
    asp.net 验证正则表达式
    Asp.net MVC进入请求管道的过程
    MVC(二)
    Aspect Oriented Programming (AOP)
    在C#中??和?分别是什么意思?
    MVC(一)
    ASP.NET 管道事件与HttpModule, HttpHandler简单理解
    Entity Framework && Lambda
    扩展类和扩展方法
  • 原文地址:https://www.cnblogs.com/Evil-Rebe/p/4897347.html
Copyright © 2011-2022 走看看