Struts2 零配置(二)
2015-06-30 12:47
651 查看
前一篇文章我们讲了struts2
零配置基本的actions定位,action的name的映射和寻找页面资源的一些约定。这一篇我们主要介绍一下Convention的Annotation.
1.Action annotation
@Action注释
@Actions注释
我们可以通过:/different/url!method1.action 或 /another/url!method1.action 来调用method1 方法。
对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp
一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。
一./hello!method1.action 映射 url:/WEB-INF/content/hello-error.jsp
二./another/url!method1.action 映射 url:/WEB-INF/content/another/url-error.jsp
可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。
2.Result annotation
一.全局的(global)。
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。
当我们访问 /hello -world !method2.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /different/url!method2.action 时,返回 /WEB-INF/fail.jsp
二.本地的(local)。
本地results只能在action方法上进行声明。
当我们调用 /other/bar!method1.action 时,返回 www.baidu.com
3.Namespace annotation
通过/other/url!method2.action 访问method2 方法
通过/different /url!method3.action 访问method3 方法
与@Action注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello-world!method1.action 已经不能访问method1了.
总结:这些都是比较小的点,没有什么技术含量。只是知道不知道的问题。基本了解一下即可。
零配置基本的actions定位,action的name的映射和寻找页面资源的一些约定。这一篇我们主要介绍一下Convention的Annotation.
1.Action annotation
@Action注释
package com.example.web; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport { @Action("action1") public String method1() { return "success"; } @Action("/user/action2") public String method2() { return "success"; } }
@Actions注释
package com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; public class HelloAction extends ActionSupport { @Actions({ @Action("/different/url"), @Action("/another/url") }) public String method1() { return “error”; }
我们可以通过:/different/url!method1.action 或 /another/url!method1.action 来调用method1 方法。
对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp
一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。
package com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; public class HelloAction extends ActionSupport { @Action("/another/url") public String method1() { return “error”; }我们调用method1方法可以通过两种方式:
一./hello!method1.action 映射 url:/WEB-INF/content/hello-error.jsp
二./another/url!method1.action 映射 url:/WEB-INF/content/another/url-error.jsp
可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。
2.Result annotation
一.全局的(global)。
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。
package com.example.actions; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; @Results({ @Result(name="failure", location="/WEB-INF/fail.jsp") }) public class HelloWorld extends ActionSupport { public String method1() { return “failure”; } @Action("/different/url") public String method2() { return “failure”; } }当我们访问 /hello -world !method1.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /hello -world !method2.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /different/url!method2.action 时,返回 /WEB-INF/fail.jsp
二.本地的(local)。
本地results只能在action方法上进行声明。
package com.example.actions; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; public class HelloWorld extends ActionSupport { @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")}) public String method1() { return “error”; } }当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp
当我们调用 /other/bar!method1.action 时,返回 www.baidu.com
3.Namespace annotation
packagecom.example.web; importcom.opensymphony.xwork2.ActionSupport; importorg.apache.struts2.convention.annotation.Action; importorg.apache.struts2.convention.annotation.Actions; @Namespace("/other") publicclass HelloWorld extends ActionSupport { public String method1() { return “error”; } @Action("url") public String method2() { return“error”; } @Action("/different/url") public String method3() { return“error”; } }通过/other/hello-world!method1.action 访问method1 方法。
通过/other/url!method2.action 访问method2 方法
通过/different /url!method3.action 访问method3 方法
与@Action注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello-world!method1.action 已经不能访问method1了.
总结:这些都是比较小的点,没有什么技术含量。只是知道不知道的问题。基本了解一下即可。
相关文章推荐
- [java]引用类型传的是地址
- [java]求质数
- [java]利用函数自动排序数组并逆序
- [java]数组最小值前置,其他顺序不变
- [java]冒泡算法:让数字从小到大排序
- [java]删除数组中的某一个元素
- java统计List中的元素重复出现的次数和对map按key或键值排序
- [java]数组复制的2种方式
- Java的StringTokenizer类
- How to use JUnit in Eclipse?
- 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面
- 【Spring学习笔记-MVC-15】Spring MVC之异常处理
- javascrip cookie
- Java中使用正则表达式获取网页中所有图片的路径
- [转载]Java 反射机制(包括组成、结构、示例说明等内容)
- eclipse下新建 JBPM6 运行环境 和运行测试
- Java获取汉字拼音的全拼和首拼实现代码分享
- Java Servlet映射配置
- Java图片处理 (文字水印、图片水印、缩放、补白)代码实例
- Java中Properties类的操作