1.在Web.xml配置文件中添加
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.learn.action</param-value>
</init-param>
</filter>
这样sturts2框架会自动扫描action包中的命名为*Action或者集成ActionSupport的类,例如一个类命名为SearchAction,那么Struts2框架会自动扫描它,认为它为一个Action类,并且将所有search.action的请求都转发给该类进行处理,这时我们又遇到了一个问题,如果这个Action是多Method的Action那么我们又该怎么办呢?因为不能像在XML中那样配置Method属性,这时我们可以在请求是加上!MethodName以请求特定的Method即可,例如想要请求SearchAction的search方法,我们可以直接这样请求:search!search.action.
以上讲到的是Struts2框架如何找到没有在XML中定义的Action,并且怎么样将请求转发给适当的Action来进行处理.
我们都知道在XML中可以定义Action处理完以后的跳转,同样利用Annotation也可以做到这一点:
2.Results的Annotation定义
我们只需要在Action Class的类的前面加上@Result即可以定义Action的跳转
例如:
@Results({
@Result(name="input" value="/input.jsp" type=NullResult.class),
@Reuslt(name="success" value="/success.jsp" type=NullResult.class),
@Result(name="error" value="/error.jsp" type=NullResult.class)
})
上面是一个典型的多Result配置,name属性指定返回的字符串,value指定要跳转的页面,type指定Result的类型,type是一个很重要的属性,他有一下几种情况:
1)NullResult : 默认的可以省略
2)ActionChainResult : 用于从一个Action跳转到另外一个Action
例如:
@Result
(name = "search",value= "search",type=ActionChainResult.class,
params={"method","search"})
这个配置是如果返回值为"search"就跳转掉SearchAction的search方法,如果不指定params就跳转到SearchAction的execute方法
3)StreamResult:用于文件下载
例如:
@Result
(name="success",value="inputStream",type=StreamResult.class,
params={"bufferSize",FileConstant.DOWNLOAD_BUFFER_SIZE})
分享到:
相关推荐
struts2 annotation 注解的详细介绍及使用方法
struts2 使用Annotation配置实现的小例子,转载供大家学习使用,里面包括了所需的jar包
struts2,json-plugin, 并全注解零配置
Struts2的“零配置”特性是Struts2的新功能,可能会出现一些小Bug,所以企业开发者请慎重使用该特性, ...如果用的是Annotation的Struts2,就要将struts.xml去掉,否则即使将struts.xml中的内容注销,也会报错;
通过产生临时文件下载之后,再通过线程删除临时文件
前段时间很困惑的文件下载问题,现贴出代码,希望能帮到需要的人吧
NULL 博文链接:https://zhenghuazhi.iteye.com/blog/1467214
参考博文:http://blog.csdn.net/u011638419/article/details/41743049
struts2-Annotation
马士兵视频里的struts2 hibernate3 spring2.5 annotation 整合 绝对好用 里面还有jar包 部署就可以用
博文链接:https://flym.iteye.com/blog/174358
本文档对利用struts2 注解 annotation 实现文件下载作了简单介绍,并有代码为例,希望对学习struts2注解annotation的人有帮助,特别是需要动态传参方面。
用注解(annotation)方式配置struts2 (webwork) 中action的拦截器(interceptor)
struts annotation.ppt 欢迎下载
Struts2中使用annotation实现零配置
Spring3Annotation+Struts2Annotation+Hibernate3Annotation 全注解集成开发,操作Emp表
struts2 annotation Convention plugin允许action类通过注解来改变映射的url
1、shiro+strut2 2、shiro注解 + struts2注解 2、shiro 缓存:因为使用了缓存,第一使用admin登录,如果没有logout,再用test登录,则test具有admin的权限
Struts2 Spring Hibernate 框架整合 Annotation Maven project