最近在看Struts2的showcase,本打算先找个简单的看看,就选择了person,要么说没有中彩票的运气,第一个例子我就看不明白了,person的index.jsp里是这样调用action的
<html>
<head>
<title>Acme Corp</title>
</head>
<body>
<ul>
<li><a href="newPerson!input.action">Create</a> a new person</li>
<li><a href="listPeople.action">List</a> all people</li>
</ul>
</body>
</html>
但是在struts-person.xml里却没有这个action的声明,只是声明了一个包,也没有默认的action类,后来”百度“了一下才知道,这个是Struts2的新特性,使用注解配置Action。
使用注解配置Action第一个要做的就是在web.xml中配置FilterDispatcher,为FilterDispatcher设置 actionPackages参数,指定包含了Action类的包的列表,多个包之间用(,)分割,这些包和他们的子包都会被扫描。扫描的对象是包中所有实现了Action接口,或者以"Action"结尾的类。顺便说一下类名的匹配方法,把类名的第一个字母小写,如果以Action结尾的,去掉 Action。比如说一个类叫 DeletePersonAction 那么他的请求应该是deletePersion.action。
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>org.apache.struts2.showcase.person</param-value>
</init-param>
</filter>
这个配置的意思就是会扫描org.apache.struts2.showcase.person包里所有实现了Action接口,或者以"Action"结尾的类,所以"newPerson!input.action"这个请求对应的类应该是NewPersonAction的input 方法。
我们再看看NewPersonAction.java这个文件
@ParentPackage("person")
public class NewPersonAction extends ActionSupport {
private static final long serialVersionUID = 200410824352645515L;
PersonManager personManager;
Person person;
public void setPersonManager(PersonManager personManager) {
this.personManager = personManager;
}
public String execute() {
personManager.createPerson(person);
return SUCCESS;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
所谓的零配置就是把相关的注解写到Action类里,比如ParentPackage,Namespace,Result,Results
例如:
@Namespace("/person") //指定名称空间
@ParentPackage("person") //指定继承的包,struts-default无需指定
@Result("/success.jsp") //指定success,因为success是默认的,所以不用指定name
@Results({
@Result("name="input", value="/input.jsp"),
@Result("name="error", value="/error.jsp", type=ServletDispatcherResult.class)
})
public class NewPersonAction extends ActionSupport {
。。。。
}
但是NewPersonAction.java里并没有指定result,那它是怎么转移的呢?这个是codebehind插件的作用。在struts.xml里有这样的设定,也就是说person这个包应用了codebehind的功能
<constant name="struts.codebehind.defaultPackage" value="person" />
参考网址是
http://struts.apache.org/2.0.6/docs/codebehind-plugin.html
我想因为这个,所以NewPersonAction会跳转到newPerson.ftl上。
<!-- -->
分享到:
相关推荐
Struts 2使用注解配置Action,不配置struts.xml,通过注解直接配置action
struts2 使用注解现在零配置不需要在使用struts.xml配置文件。 struts2 注解实例。可以直接跑
struts2注解配置Action及拦截器几种不同方式写法对应的路径指向.docx
使用struts2基于注解(零配置)实现的文件上传与下载的代码,可以正常运行
主要介绍了struts2中使用注解配置Action方法详解,涉及一个示例,具有一定参考价值,需要的朋友可以了解下。
使用注解配置Action示例。里面已经包涵java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils错误异常的解决方案。
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
都被它莫名其妙的错误搞的郁闷,而网上关于这方面的东西大多都是基于struts2.0版本的,对我们现在用的2.1以上的版本不起什么作用,所以特整理出一份文档,里面详细说明了怎样用注解出配置struts2的action,...
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。• 包命名习惯来指定Action位置• 命名习惯制定结果(支持JSP,FreeMarker等)路径• 类名到URL的约定转换• 包名...
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将请求在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
8.使用default-Action配置统一访问 14 小结Action 14 9.使用通配符 14 10.使用0配置:ZERO Annotation 15 11.Result配置详解 15 探讨type类型: 16 Type类型值 16 作用说明 16 对应类 16 chain 16 用来处理Action链 ...
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...
主要介绍了详解在Java的Struts2框架中配置Action的方法,讲解了包括struts.xml中的action配置及基于注解方式Action配置的两个方式,需要的朋友可以参考下
使用的是注解的方式完成的,因为注解的方式可以减少一些配置文件,比较方便的。 【特别强调】一定要清楚如何调试项目,因为自己的环境和下载的资源的环境可能不一样,需要修改配置文件或是其他才能正常运行。...
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。 • 包命名习惯来指定Action位置 • 命名习惯制定结果(支持JSP,FreeMarker等)路径 • 类名到URL的约定转换 • 包名...
2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立...