Struts2使用Convention插件,你需要将其JAR文件放到你应用的WEB-INF/lib目录中,你也可以在你Maven项目的POM文件中添加下面包依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.1.6</version>
</dependency>
零配置并不是没有配置,而是通过约定大于配置的方式,大量通过约定来调度页面的跳转而使得配置大大减少。所以,首先应该了解下convention-plugin的约定:
1. 默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如: <constant name="struts.convention.result.path" value="/WEB-INF/page" />
则将路径配置到了WEB-INF/page 下。
2. 默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如:
<constant name="struts.convention.package.locators" value="web,action" />
则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索
com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。
3. 接着,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类
4. 命名空间。从定义的.package.locators标示开始到包结束的部分,就是命名空间。举个例子:
Com.ustb.web.user.userAction的命名空间是:”/user”。Com.ustb.web.user.detail.UserAction的命名空间是:”/user/detail”
5. Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割,你可以设置struts.convention.action.name.separator 如
<constant name="struts.convention.action.name.separator" value="-" />
还是举个例子:
UserAction->user UserDetailAction
->user-detail。结合上面的。对于com.ustb.web.user.detail.UserDetailAction,映射的
url就是/WEB-INF/content/user/detail/user-detail.jsp
6. struts支持.jsp .html .htm .vm格式的文件。
下面是actiong和结果模版的映射关系:
URL
Result File that could match
Result Type
/hello
success
/WEB-INF/content/hello.jsp
Dispatcher
/hello
success
/WEB-INF/content/hello-success.htm
Dispatcher
/hello
success
/WEB-INF/content/hello.ftl
FreeMarker
/hello-world
input
/WEB-INF/content/hello-world-input.vm
Velocity
/test1/test2/hello
error
/WEB-INF/content/test/test2/hello-error.html
Dispatcher
实际项目的需要。所幸,convention的零配置是非常灵活的。
通过@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;
}
}
方法名
默认调用路径
默认映射路径
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.
/WEB-INF/content/action1.jsp
method1
/user/action2!method2.action
/WEB-INF/content/user/action2.jsp
通过@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”;
}
通过 /other/hello-world!method1.action 访问method1 方法。
通过 /other/url!method2.action 访问method2 方法
通过 /different /url!method3.action 访问method3 方法
与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action 已经不能访问method1 了.
@Results和@Result
1 全局的(global)。
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。
Java代码 复制代码
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
2 本地的(local)。
本地results只能在action方法上进行声明。
Java代码 复制代码
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
参考:http://hi.baidu.com/millionware/blog/item/2a672b0b81e89134b0351d46.html
分享到:
相关推荐
struts2-convention-plugin-2.3.24.1
struts2-convention-plugin-2.3.15.1.jar
然而,随着版本的更新,Struts2引入了一个名为Convention Plugin的新特性,旨在简化配置过程,实现所谓的“零配置”开发。 **什么是Struts2 Convention Plugin?** Convention Plugin是Struts2的一个插件,它基于...
struts2-convention-plugin-2.1.6.jar
struts2-convention-plugin-2.3.1.jar,使用注解的方式代替xml配置action,必须要引用这个包。
struts2-convention-plugin-2.1.8.jar
struts2-convention-plugin-2.3.32
struts2-convention-plugin-2.3.1.2.jar
通过使用Convention-Plugin,Struts2框架实现了零配置的目标,极大地简化了Web应用的开发过程。开发人员可以将更多的精力集中在业务逻辑上,而不是繁琐的配置细节上。这对于提高开发效率、减少出错率具有重要意义。...
Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置,使用Convention插件,你需要此JAR文件
struts2-convention-plugin-2.3.24.jar
struts2-convention-plugin-2.3.15.3.jar struts2配置Action注解要用到的包,真正实现零配置
不论高低版本,要使用struts2-core这个jar包,当又需struts2-convention-plugin.jar时勿必要使两者版本一致哦,否则会有DefError、Unable to read class诸等错误
struts2-convention-plugin-2.3.4.1.jar
struts2-convention-plugin-2.1.8.1.jar
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...