在Action中提供了下列几种注解:
ParentPackage
Namespace
Result
Results
注解
描述
Namespace
所期望的命名空间(在“struts.xml”文件中也有定义)的字符串值
ParentPackage
所期望的父package的字符串值
Results
“Result”注解列表
Result
提供了Action结果的映射,它有四个属性:
? name ——action方法的结果名
? type—— 结果类型
? value——任意的结果值。可以是rediect结果类型对应的action名,也可以是dispatcher结果类型对应的JSP
? parameters ——字符串参数组成的数组
Action注解配置
使用方式如下:
@ParentPackage(value="struts-default")
@Namespace(value="/test")
@Result(name="success",value="/userSuc.jsp")
@Results({
@Result(name="success",value="/userSuc.jsp",type=org.apache.struts2.dispatcher.ServletRedirectResult.class),
@Result(name="input",value="/main.jsp",type=org.apache.struts2.dispatcher.ServletRedirectResult.class)
})
在使用这些注解的时候,还需要进行额外的一些配置。在web.xml的filter配置中,需要指定哪些package是使用了注解的。配置如下所示,其中参数名必须为“actionPackages”,参数的值就是package的名称列表。
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>user.actions,other.actions</param-value>
</init-param>
</filter>
被配置过的每一个package和它的子package都会被扫描到,看其中哪些类实现了Action或者类名以“Action”结尾,然后注解就会被加入到运行时配置中去。如果没有使用namespace注解的话,那么命名空间就会由package名来生成。把“actionPackages”配置值中使用的package名称截掉,就得到了命名空间。
也就是说,如果某个被配置好的action的名字是“actions.admin.user.AddAction”,而“actionPackages”的值为“actions”,那么这个action的命名空间就是“/admin/user”。
生命周期回调注解(拦截器注解)
关于方法级的生命周期回调的注解一共有三种,每一种都是在Action处理过程中的特定时刻被调用的。生命周期回调与拦截器及Action代理不同,它们特定于被调用的Action类存在的,并不是那种可以在多个Action中使用的单个的类。
Before:被注解的方法会在Action的逻辑执行前被调用。
BeforeResult:被注解的方法的调用时机是Action的逻辑执行之后,但执行结果还未被调用之前。
After:被注解的方法的调用时机是Action的结果被调用之后,但尚未被返回给用户之前
例如:
public class TestAction extends ActionSupport {
@Before
public void doBefore(){
System.out.println("注解配置的方法在action执行之前调用");
}
@After
public void doAfter(){
System.out.println("注解配置的方法在action执行之后调用");
}
@BeforeResult
public void doBeforeResult(){
System.out.println("注解配置的方法在Result返回之前调用");
}
…….
}
Struts.xml <package name="test" extends="struts-default">
<interceptors>
<interceptor name="annoInteceptor"
class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor">
</interceptor>
.....
</interceptors>
<action name="text" class="action.TestAction">
.....
<interceptor-ref name="annoInteceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
.......
</package>
分享到:
相关推荐
Struts 2使用注解配置Action,不配置struts.xml,通过注解直接配置action
struts2 使用注解现在零配置不需要在使用struts.xml配置文件。 struts2 注解实例。可以直接跑
struts2注解配置Action及拦截器几种不同方式写法对应的路径指向.docx
使用struts2基于注解(零配置)实现的文件上传与下载的代码,可以正常运行
主要介绍了struts2中使用注解配置Action方法详解,涉及一个示例,具有一定参考价值,需要的朋友可以了解下。
都被它莫名其妙的错误搞的郁闷,而网上关于这方面的东西大多都是基于struts2.0版本的,对我们现在用的2.1以上的版本不起什么作用,所以特整理出一份文档,里面详细说明了怎样用注解出配置struts2的action,...
而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定...
使用注解配置Action示例。里面已经包涵java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils错误异常的解决方案。
求有爱的大大帮忙解决一下strut2和sprin3注解找不到action的问题
8.1.1 页面上:如何使用自定义结果组件构建Struts 2 Ajax应用程序 171 8.1.2 实现JSON结果类型 173 8.2 常用的结果类型 180 8.2.1 RequestDispatcher,也叫做dispatcher 180 8.2.2 ServletRedirectResult,也叫做...
Struts2的框架及注释和使用法,希望大家支持,我们一起努力,谢谢!
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。• 包命名习惯来指定Action位置• 命名习惯制定结果(支持JSP,FreeMarker等)路径• 类名到URL的约定转换• 包名...
struts2将请求在Action中封装为Map并将配置文件放到web-info中还可以自定义配置文件位置就是不将struts.xml放到src下但还是不如spring mvc 的封装来得方便
使用的是注解的方式完成的,因为注解的方式可以减少一些配置文件,比较方便的。 【特别强调】一定要清楚如何调试项目,因为自己的环境和下载的资源的环境可能不一样,需要修改配置文件或是其他才能正常运行。...
Struts2直接使用注解的详细配置action 去掉Struts.xml 省去多余的代码 让代码更直观
ssh2注解配置,全部是注解配置,struts2和hibernate3和spring2.5全部是注解配置,,访问路径为http://localhost:8080/mytest/student/findAll.action
struts2+Spring3.0+Mybatis组合框架开发,可以正常的运行,其中使用spring注解方式管理action
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...
3.初识struts2配置文件 4 (1).web.xml文件 4 (2).struts.xml文件 4 (3).struts.properties(参default.properties) 4 (4)struts-default.xml 4 (5)其它配置文件 4 4.让MyEclipse提示xml信息 4 5.如何...
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...