一、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。
通过@Action注释
对如下例子:
-
packagecom.example.web;
-
-
importcom.opensymphony.xwork2.Action;
-
importcom.opensymphony.xwork2.ActionSupport;
-
-
publicclassHelloActionextendsActionSupport{
-
@Action("action1")
-
publicStringmethod1(){
-
returnSUCCESS;
-
}
-
-
@Action("/user/action2")
-
publicStringmethod2(){
-
returnSUCCESS;
-
}
-
}
方法名 |
默认调用路径 |
默认映射路径 |
method1 |
/hello!method1.action . |
/WEB-INF/content/hello.jsp |
method2 |
/hello!method2.action. |
/WEB-INF/content/hello.jsp |
通过@Action注释后
方法名 |
@Action注释后调用路径 |
@Action注释后映射路径 |
method1 |
/action1!method1.action.或简写/action1 |
/WEB-INF/content/action1.jsp |
method1 |
/user/action2!method2.action或/user/action2 |
/WEB-INF/content/user/action2.jsp |
注:
@Action("/user")则是/user,对应的映射路径是/WEB-INF/content/action3.jsp,该注释覆盖了默认的namespace(“/”)
通过@Actions注释
-
packagecom.example.web;
-
-
importcom.opensymphony.xwork2.ActionSupport;
-
importorg.apache.struts2.convention.annotation.Action;
-
importorg.apache.struts2.convention.annotation.Actions;
-
-
publicclassHelloActionextendsActionSupport{
-
@Actions({
-
@Action("/different/url"),
-
@Action("/another/url")
-
})
-
publicStringmethod1(){
-
return“error”;
-
}
我们可以通过:/different/url!method1.action(/different/url)或/another/url!method1.action来调用method1方法。
对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp
可能误导了大家,一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。比如对于如下例子:
-
packagecom.example.web;
-
-
importcom.opensymphony.xwork2.ActionSupport;
-
importorg.apache.struts2.convention.annotation.Action;
-
importorg.apache.struts2.convention.annotation.Actions;
-
-
publicclassHelloActionextendsActionSupport{
-
@Action("/another/url")
-
publicStringmethod1(){
-
return“error”;
-
}
我们调用method1方法可以通过两种方式:
1/hello!method1.action映射url:/WEB-INF/content/hello-error.jsp
2/another/url!method1.action映射url:/WEB-INF/content/another/url-error.jsp
可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。
通过@Namespace 注释
-
packagecom.example.web;
-
-
importcom.opensymphony.xwork2.ActionSupport;
-
importorg.apache.struts2.convention.annotation.Action;
-
importorg.apache.struts2.convention.annotation.Actions;
-
@Namespace("/other")
-
publicclassHelloWorldextendsActionSupport{
-
-
publicStringmethod1(){
-
return“error”;
-
}
-
@Action("url")
-
publicStringmethod2(){
-
return“error”;
-
}
-
-
@Action("/different/url")
-
publicStringmethod3(){
-
return“error”;
-
}
-
}
通过/other/hello-world!method1.action访问method1方法。
通过/other/url!method2.action访问method2方法映射url:/WEB-INF/content//other/url/hello-error.jsp
通过/different /url!method3.action访问method3方法
与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action已经不能访问method1了.
@Results和@Result
1 全局的(global)。
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。
-
packagecom.example.actions;
-
-
importcom.opensymphony.xwork2.ActionSupport;
-
importorg.apache.struts2.convention.annotation.Action;
-
importorg.apache.struts2.convention.annotation.Actions;
-
importorg.apache.struts2.convention.annotation.Result;
-
importorg.apache.struts2.convention.annotation.Results;
-
-
@Results({
-
@Result(name="failure",location="/WEB-INF/fail.jsp")
-
})
-
publicclassHelloWorldextendsActionSupport{
-
publicStringmethod1(){
-
return“failure”;
-
}
-
@Action("/different/url")
-
publicStringmethod2(){
-
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
2 本地的(local)。
本地results只能在action方法上进行声明。
-
packagecom.example.actions;
-
-
importcom.opensymphony.xwork2.ActionSupport;
-
importorg.apache.struts2.convention.annotation.Action;
-
importorg.apache.struts2.convention.annotation.Actions;
-
importorg.apache.struts2.convention.annotation.Result;
-
importorg.apache.struts2.convention.annotation.Results;
-
-
publicclassHelloWorldextendsActionSupport{
-
@Action(value="/other/bar",results={@Result(name="error",location="www.baidu.com",type="redirect")})
-
publicStringmethod1(){
-
return“error”;
-
}
-
}
当我们调用/hello-world!method1.action时,返回/WEB-INF/content/hello-error.jsp
当我们调用/other/bar!method1.action时,返回www.baidu.com
分享到:
相关推荐
struts2-convention-plugin-2.3.32
struts2-convention-plugin-2.3.24.1
struts2-convention-plugin-2.3.15.1.jar
struts2-convention-plugin-2.1.6.jar
struts2-convention-plugin-2.1.8.jar
不论高低版本,要使用struts2-core这个jar包,当又需struts2-convention-plugin.jar时勿必要使两者版本一致哦,否则会有DefError、Unable to read class诸等错误
struts2-convention-plugin-2.3.1.2.jar
struts2-convention-plugin-2.3.1.jar,使用注解的方式代替xml配置action,必须要引用这个包。
struts2-convention-plugin-2.3.24.jar
struts2-convention-plugin-2.3.15.3.jar struts2配置Action注解要用到的包,真正实现零配置
struts2-convention-plugin-2.3.4.1.jar
Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置,使用Convention插件,你需要此JAR文件
struts2-convention-plugin-2.1.8.1.jar
struts2采用convention-plugin实现零配置,希望大家用的到!
struts2-convention-plugin-2.3.32.jar
struts2-convention-plugin.pdf
2.5.10.1.jar,struts2-config-browser-plugin-2.5.10.1.jar,struts2-convention-plugin-2.5.10.1.jar,struts2-dwr-plugin-2.5.10.1.jar,struts2-embeddedjsp-plugin-2.5.10.1.jar,struts2-gxp-plugin-2.5.10.1....
一:升级高版本的struts2.5.22 无法访问Action解决办法 1.最主要原始struts版本与freemarker-2.3.22版本不一致...2.具体原因就研究struts2-core-2.3.32.jar 与 freemarker-2.3.22.jar 关系依赖 更新如下8个jar即可。