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

action类的注解:

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

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

总结常用的注解如下:

Namespace:指定命名空间。
ParentPackage:指定父包。
Result:提供了Action结果的映射。(一个结果的映射)
Results:“Result”注解列表
ResultPath:指定结果页面的基路径。
Action:指定Action的访问URL。
Actions:“Action”注解列表。
ExceptionMapping:指定异常映射。(映射一个声明异常)
ExceptionMappings:一级声明异常的数组。
InterceptorRef:拦截器引用。
InterceptorRefs:拦截器引用组。

Struts2利用注解实现action跳转的更多相关文章

  1. Struts2基于注解的Action配置

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...

  2. struts2基于注解的action

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...

  3. struts2基于注解配置action

    如果使用struts2,就需要配置文件或者注解,关于struts2的配置文件struts.xml非常熟悉,对于注解可能spring使用的比较多.配置文件的繁琐衬托出了注解的简洁方便,一条或者几条注解解 ...

  4. Struts2 从一个Action跳至另一个Action

    Struts2  从一个Action跳至另一个Action 一.注解的 @Result(name=SUCCESS,type="chain", params={"actio ...

  5. struts/Servlet,action转到jsp后,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径)

    问题:使用struts2,如何处理action的路径?还有,在action转到的jsp中,如何写js,css,图 片的路径?(例如访问 http://localhost/project/listUse ...

  6. Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

    Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...

  7. struts2学习笔记之十四:使用注解配置Action(不是和spring集成使用)

    Struts2支持使用注解配置Action,减少配置文件的配置 Struts2如果要支持注解配置Action,需要插件的支持,导入插件struts2-convention-plugin-2.1.8.1 ...

  8. Struts/Servlet,action转到jsp后,CSS失效,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径)

    问题:使用struts2,如何处理action的路径?还有,在action转到的jsp中,如何写js,css,图 片的路径?(例如访问http://localhost/project/listUser ...

  9. struts2使用注解--ACTION中的应用

    1.在类中指定包:@ParentPackage("system").其中system是在struts.xml中定义的包名.2.配置文件--->注解2.1配置文件方式(返回js ...

随机推荐

  1. spring Stack Overflow

    1. ApplicationContext 不关闭,资源泄露问题: Spring ApplicationContext - Resource leak: 'context' is never clos ...

  2. hdu_5768_Lucky7(中国剩余定理+容斥)

    题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥 ...

  3. FUSE and File System

    FUSE: File system in USErspace. So what is a file system? A file system maps file paths to file cont ...

  4. isset函数

    isset (PHP 4, PHP 5) isset — 检测变量是否设置 检测变量是否设置,并且不是 NULL. 如果 var 存在并且值不是 NULL 则返回 TRUE,否则返回 FALSE. $ ...

  5. PHP字符串函数试题

    Ctrl+A查看答案 1.把ASCII字符的字符串转换为十六进制值的函数是什么?答:bin2hex($string),例如bin2hex('ab') = 6162 2.ASCII码转字符,字符转ASC ...

  6. Qt Quick 简单教程

    上一篇<Qt Quick 之 Hello World 图文详解>我们已经分别在电脑和 Android 手机上运行了第一个 Qt Quick 示例—— HelloQtQuickApp ,这篇 ...

  7. iOS开发 GET、POST请求方法:NSURLSession篇

    NSURLConnection,在iOS 9被宣布弃用,本文不使用NSURLConnection进行网络编程,有兴趣的童鞋可以参考: iOS开发 GET.POST请求方法(NSURLConnectio ...

  8. MySql 插入10位以上长度的字符报错or截断

    当a字段为int类型时: 如果用MyBatis向MySql插入10个字符以上长度的字符串,则会报错. 如果直接在MySql中用sql语句插入10个字符以上长度的字符串,则会变成最大的int类型数值:2 ...

  9. C# tostring()汇总

    原文:http://www.cnblogs.com/xiaopin/archive/2010/11/05/1870103.html C 货币 2.5.ToString("C") ¥ ...

  10. Struts中的数据处理的三种方式

    Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...