使用注解来配置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. H5的新应用-使用Web Worker为学生考试计时

    -------------------------- <script type="text/javascript">            //初始化函数       ...

  2. find文件后cp、rm

    复制: find <src-path> -name 'some names' -exec cp {} <dest-path> \; find <src-path> ...

  3. golang vs java

    http://programmers.stackexchange.com/questions/83780/how-fast-can-go-go In terms of language design, ...

  4. ios中的关键词@property @synthesize

    @interface Person : NSObject{    int myNumber;} @property(nonatomic) int myNumber;//这个关键字是可以带套get 与s ...

  5. CentOS ulimit

    Linux 文件系统设置 文件句柄数 ulimit -n 查看 echo "* - nofile 65536" >> /etc/security/limits.conf ...

  6. hostent h_addr_list

    struct hostent { char FAR * h_name; /* official name of host */ char FAR * FAR * h_aliases; /* alias ...

  7. Qt之操作系统环境

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc90102uy9k.html Qt中操作系统环境,官方解释如下: QStringList QProcess::syst ...

  8. error while loading shared libraries: libmcrypt.so.4

    /usr/local/php/sbin/php-fpm: error while loading shared libraries: libmcrypt.so.4: cannot open share ...

  9. C++随机崩溃捕捉处理

    1. 会引起异常的几个原因(主要记录目前遇到过的几个问题) 程序读取了无效的内存地址 堆栈的溢出,比如无限循环导致那段内存溢出,比如把size为20的缓存拷贝到size为10的缓存块等 无法申请到有效 ...

  10. Servlet Filter 过滤器

    Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源: 例如Jsp, Servlet, 静态图片文件或静态 ht ...