一、配置web.xml <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.test.action</param-value> </init-param> </filter>
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
二、加入注解 @Namespace(value="/test") @Action(value = "file-manager", interceptorRefs = { @InterceptorRef(value = "fileUpload",params={"maximumSize","1024000","allowedTypes","image/pjpeg"}), @InterceptorRef(value = "basicStack")}, results = {@Result(name = ActionSupport.SUCCESS, location = "/view/file-manager-sucess.jsp"), @Result(name = ActionSupport.ERROR, location = "/view/file-manager-error.jsp") }, exceptionMappings = {@ExceptionMapping(exception = "java.lang.Exception", result = ActionSupport.ERROR)} )
验证注解: @Validations( requiredStrings={ @RequiredStringValidator(fieldName="username",message="用户名不能为空!"), @RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!") }, regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(//+86|0|1)//d{10,11}$", message="电话号码格式不正确!")} )
跳过验证注解: @SkipValidation
三、Convention的Annotation 1)与Action相关的两个Annotation是@Action 和@Actions 2)@Action中可指定一个value属性。类似于指定<action name=””/>属性值 3)@Action中还可以指定一个params属性,该属性是一个字符串数组,用于该Acion指定的参数名和参数值。params属性应遵守如下格式:{“name1”,”value1”,”name2”,”value2”} 4)@Actions 也用于修饰Action类里的方法,用于将该方法映射到多个URL.@Actions用于组织多个@Action.因此它可将一个方法映射成多个逻辑Action。
四、与Result配置相关的Annotation 1)@ResultPath @Result 和Results 2)@Results用于组织多个@Result因此它只需指定一个value属性值,该value属性值为多个@Result 3)@Result相当于struts.xml文件中的<result/>元素。使用@Result必须指定一个name属性,相当于<result name=””/>另外,它还有几个可选的属性。      ☆ type 相当于<result type=””/>指定返回视图资源的类型      ☆ location 相当于<result>…..</result>中间部分,用于指定实际视图位置      ☆ params:该属性相当于<result/>元素里多个<param../>子元素的作用,用于为该Result指定参数值。该属性应满足{“name1”,”value1”,”name2”,”value2”}格式 4)@Result有以下两种用法 1.Action级的Result映射:以@Actions组合多个@Action后修饰的Action类。这种Result映射对该Action里的所有方法都有效。 2.方法级Result映射:将多个@Result组成数组后作为@Action的results属性值。这种Result映射仅对被修饰的方法有效。 5)@ResultPath则用于修饰包和Action类,用于改变被修饰Action所对应的物理视图资源的根路径。举例说:默认情况下,Convention都会到WEB-INF/content路径下找物理视图资源,一旦我们使用@ResultPath("/view")修饰该Action,系统将回到view目录下寻找物理视图资源。
五、与包和命名空间相关的Annotation: @Namespace:修饰Action类或其所在的包。该Annotation中指定一个value属性值,用于指定被修饰的Action所在的命名空间 @Namespaces:修饰Action类或其所在的包,用于组合多个@Namespace @ParentPackage: 用于指定被修饰的Action所在包的父包。
六、与异常处理相关的Annotation @ExceptionMappings 用于组织多个@ExceptionMapping,因此它只需指定一个value属性值,该value属性值为多个@ExceptionMapping。 @ExceptionMapping 用于定义异常类和物理视图之间的对应关系,也相当于struts.xml文件里<exception-mapping../>元素的作用 使用时,必须注意以下两个属性: exception: 用于指定异常类 result:用于指定逻辑视图 @ExceptionMpping有如下两种用法 Action级的异常定义:以@ExceptionMappings组合多个@ExceptionMapping后修饰的Action类。这种异常定义对Action中的所有方法有效 方法级的异常定义:将多个@ExceptionMapping组成数组后作为@Action的exceptionMappings属性值,这种异常定义仅对被修饰的方法有效。
七、与拦截器配置相关的Annotation 与拦截器配置的Annotation有@InterceptorRef、@InterceptorRefs和@DefaultInterceptorRef @InterceptorRefs用于组织多个@InterceptorRef,因此它只需要指定一个value属性值,该value属性值为多个@InterceptorRef @InterceptorRef用于为指定Action引用lanjieq或者是拦截器栈。也就相当于strut.xml中位于<action../>元素内部的<interceptor-ref../>子元素的作用。使用@InterceptorRefAnnotation时,必须制定一个value属性,用于指定所引用的拦截器或拦截器栈的名字。相当于<interceptor-ref../>子元素里name属性的作用。
八、查看struts2配置 为了看到struts2应用里的Action等各种资源的影射情况,struts2提供了Config Browser插件。 使用方法:将struts2-config-browser-plugin-2.1.6.jar文件复制到struts2应用的WEB-INF/lib目录中。 打开首页地址:http://localhost:8080/应用名字/config-browser/actionNames.action 这里可以看到Config Browser插件的首页。

struts2注解实例:

使用注解完成一个Action的流程必须要如下的7个jar包 1.commons-fileupload-1.2.1.jar 2.commons-io-1.3.2.jar 3.freemarker-2.3.15.jar 4.ognl-2.7.3.jar 5.struts2-convention-plugin-2.1.8.1.jar 6.struts2-core-2.1.8.1.jar 7.xwork-core-2.1.6.jar

如下用户登录的Action通过注解的方式验证通过:

  1. package com.huawei.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. * @name
  12. * @date 2011-3-24
  13. * @action LoginAction.java
  14. * @time 下午11:23:58
  15. * @package_name com.huaweiaction
  16. * @project_name steutsAction
  17. */
  18. /*
  19. * 这个小Demo的主要作用就是温故一下Struts2 Action的注解
  20. * 一般在一个项目实施开发中是不会配置struts.xml进行Action的转发或重定向的,其都是通过注解的方式来配置Action的
  21. */
  22. ///////////使用注解来配置Action///////////////////////////
  23. @ParentPackage("struts-default")
  24. // 父包
  25. @Namespace("")
  26. @Results( {
  27. @Result(name = com.opensymphony.xwork2.Action.SUCCESS, location = "/msg.jsp"),
  28. @Result(name = com.opensymphony.xwork2.Action.ERROR, location = "/erlogin.jsp") })
  29. // @ExceptionMappings 一级声明异常的数组
  30. // @ExceptionMapping 映射一个声明异常
  31. @ExceptionMappings( {
  32. @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
  33. public class LoginAction extends ActionSupport {
  34. private static final long serialVersionUID = -2554018432709689579L;
  35. private String loginname;
  36. private String pwd;
  37. // @Action(value="login") 指定某个请求处理方法的请求URL。注意,它不能添加在Action类上,要添加到方法上。
  38. @Action(value = "loginName")
  39. public String login() throws Exception {
  40. if ("HEFE".equalsIgnoreCase(loginname.trim())&&"123".equalsIgnoreCase(pwd.trim()))  {
  41. return SUCCESS;
  42. }
  43. else {
  44. System.out.println("===========");
  45. return ERROR;
  46. }
  47. }
  48. @Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") })
  49. public String add() throws Exception {
  50. return SUCCESS;
  51. }
  52. public String getLoginname() {
  53. return loginname;
  54. }
  55. public void setLoginname(String loginname) {
  56. this.loginname = loginname;
  57. }
  58. public String getPwd() {
  59. return pwd;
  60. }
  61. public void setPwd(String pwd) {
  62. this.pwd = pwd;
  63. }
  64. }

struts2注解的更多相关文章

  1. Struts2注解使用说明

    Struts2注解 1 Struts2注解的作用 使用注解可以用来替换struts.xml配置文件!!! 2 导包 必须导入struts2-convention-plugin-2.3.15.jar包, ...

  2. Struts2注解 特别注意

    1 Struts2注解的作用 使用注解可以用来替换struts.xml配置文件!!! 2 导包 必须导入struts2-convention-plugin-2.3.15.jar包,它在struts2安 ...

  3. Annotation(四)——Struts2注解开发

    Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...

  4. Struts2注解学习1

    这是开博的第一篇,我希望每天把我学到的东西记录下来,成为一个知识库,方便以后的学习和分享 在项目中看到用struts2注解来做,很方便,做了一个用户登录的例子 1.加载所需jar包 commons-f ...

  5. Struts2 注解(转)

    转自:http://blog.csdn.net/wwwqvod/article/details/6214431 也叫Zero Configuration(零配置),它省去了写xml文件的麻烦,可以直接 ...

  6. struts2注解redirect传递参数解决方案时,中国的垃圾问题

    struts2注解redirect传递参数解决方案时,中国的垃圾问题 试过很多方法  tomcat 编码  .字符串转换 .URLEncoder  .. 但是,没有解决方案,然后仔细阅读   stru ...

  7. Struts2注解 及 约定优于配置

    Struts2注解 1 Struts2注解的作用 使用注解可以用来替换struts.xml配置文件!!! 2 导包 必须导入struts2-convention-plugin-2.3.15.jar包, ...

  8. Struts2注解开发

    Hibernate和spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...

  9. Spring整合Struts2 注解版

    1.jar包 <!--spring配置--> <dependency> <groupId>org.springframework</groupId> & ...

随机推荐

  1. acm小知识

    __builtin_popcount(i); __builtin_popcountll(i) ;//计算i的二进制表示中1的个数 int a[M] , b[M] ; memcpy(a+i , b+j ...

  2. C++ 与OpenCV 学习笔记

    联合体:当多个数据需要共享内存或者多个数据每次只取其一时,可以利用联合体(union) 1. 联合体是一种结构: 2. 他的所有成员相对于基地址的偏移量均为0: 3. 此结构空间要大到足够容纳最&qu ...

  3. User space 与 Kernel space

    学习 Linux 时,经常可以看到两个词:User space(用户空间)和 Kernel space(内核空间). 简单说,Kernel space 是 Linux 内核的运行空间,User spa ...

  4. web加密的基本概念

    1.需求 了解web加密的一些基础概念. 2.基本概念 a.对称加密方式 对称加密方式 加密和解密用同一个密钥 不足之处是,交易双方都使用同样钥匙,安全性得不到保证.此外,每对用户每次使用对称加密算法 ...

  5. 【河北省队互测】 gcd BZOJ 2818

    Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sa ...

  6. 原生JavaScript实现Ajax

    var getXmlHttpRequest = function() { if (window.XMLHttpRequest) { //主流浏览器提供了XMLHttpRequest对象 return ...

  7. docker swarm-mode

    root@node1:~# docker versionClient: Version: 1.12.3 API version: 1.24 Go version: go1.6.3 Git commit ...

  8. npm提速

    解决办法:npm --> cnpm https://npm.taobao.org

  9. xpath 总结

    例如 <table id="MatchTable"> <tr id="Explain_1228761" style="display ...

  10. php学习中——知识点(1)

    php是嵌入式脚本语言(意义也就不言而喻) 标识:<?php ....  ?>         输出:echo "**"; 使用美元符号($)后跟变量名表示变量,区分大 ...