1.使用Convention插件,你需要将其JAR文件放到你应用的WEB-INF/lib目录中.通过约定大于配置的方式,简写配置文件;
for example:
1-1.添加插件包
要使用Convention Plugin,需要把struts2-convention-plugin-2.1.8.1.jar包拷贝到web工程的WEB-INF\lib包里面。
1-2.测试零配置
首先去掉struts.xml文件,默认情况下,Convention Plugin会约定所有的结果 页面都存放在WEB-INF\content文件夹下,
(也可以通过配置属性更改默认位置),新建一个hello-world.jsp
1-3.访问方式
在浏览器中直接访问http://localhost:8080/struts2ConPlugin/hello-world
总结:由于默认的结果都约定存在content中,当没有找到Action它会直接到content中找hello-world.jsp.
也可以通过struts.xml设置新的:
<constant name="struts.convention.result.path" value="/WEB-INF/login/"></constant>
2.当我们应用了Convention插件,它会自动搜索位于action、actions、struts、struts2的包及其子包下的所有Java类,以便搜索Action类
当然我们也可以改变:
<constant name="struts.convention.package.locators" value="web,servlet"></constant>
3.Convention插件在满足包的情况下找Action类都会
1.实现com.opensymphony.xwork2.Action
2.类名以Action结尾的Java类 (HelloWorldAction)对应名称hello-word
修改默认Convention 值 参看http://blog.csdn.net/without0815/article/details/7598185
4.命名空间
1.定义的package org.ymm.actions.yy.mm 的名空间是/yy/mm
2.@Namespace注解名空间/aa覆盖了包/yy/mm的名空间
3.@Action如果加了/的名空间是一个独立的名空间(与/aa和/yymm无关),如果没有就用@Namespace(/aa)
package org.ymm.actions.yy.mm;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.glassfish.external.statistics.annotations.Reset;
@Namespace("/aa")
public class PeopleAction extends ActionSupport {
private String uname;
@Action(value="/people/liuming")
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("hello+++++++++++++");
this.setUname("LiuMing");
//return "ming";
return SUCCESS;
}
}
注:加了名空间,返回的必须在指定的文件(默认是content)加上相应的文件夹。不然找不到对应的结果jsp文件
5.@Action 和@Actions
1.1-被@action 注解后通过 (url +Action中value值)访问如上:http://localhost:8080/struts2ConPlugin/people/liuming
1.2-@actions中包含多个·@Action
@Actions({
@Action(value="bai-du",
results={@Result(name="success", location="http://www.baidu.com", type="redirect")}),
@Action("liux-ming")
})
public String say(){
System.out.println("say++++++++++");
return "success";
}
6.@Results和@result
@Actions({
@Action(value="/study",
results={@Result(name="success", location="http://www.baidu.com", type="redirect"),
@Result(name="fail", location="http://www.csdn.net/", type="redirect")}),
//@Action("liux-ming")
})
public String say(){
System.out.println("say++++++++++");
return "fail";
}
分享到:
相关推荐
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
convention-plugin 插件使用说明
struts2-convention-plugin-2.1.8.jar
struts2-convention-plugin-2.3.1.2.jar
struts2-convention-plugin-2.3.1.jar,使用注解的方式代替xml配置action,必须要引用这个包。
struts2-convention-plugin-2.3.24.jar
Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置,使用Convention插件,你需要此JAR文件
struts2采用convention-plugin实现零配置,讲述如何不通过配置文件搭建struts2
struts2-convention-plugin-2.3.4.1.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.1.8.1.jar
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
struts2-convention-plugin.pdf
struts2-convention-plugin-2.3.32.jar