struts如何实现零配置

来源:互联网 发布:小天才早教软件 编辑:程序博客网 时间:2024/05/22 05:30

事实上不是零配置,而是约定大于配置而已

 

1,配置如下常量

<struts>

<constant name="struts.convention.result.path" value="/pages"> 【配置返回页面所在路径】</constant>

<constant name="struts.convention.package.locators" value="action"></constant>【哪些包下面的文件会被当做Action类来检索

<constant name="struts.convention.action.name.separator" value="-" />【这里就是配置的如何分Actionname - 注意是去掉最后的Action

</struts>  

2,创建action

@Action("/test11")//注意这里最好是action的名称【开头小写】

public class TestAction extends ActionSupport{

public String method1() throws Exception {//访问路径 test11!method1.action

System.out.println("method1 execute");

return "method1-err"; //对应的返回页面 /pages/test11-method1-err.jsp【配置的存储页面的路径/actionname【这个是上面定义的,不一定是该Action类的name,但最好一致】-methodname-returnstring.jsp

}

public String method2() throws Exception {

System.out.println("method2 execute");

return "method2-success";

}

}

 

这是我自己总结的,没有举例所有的方式,个人认为,只需要有一种适合自己的就可以了O(∩_∩)O~【类-方法-返回值.jsp  我认为这样就已经可以区分所有的返回页面了,而且还很清晰O(∩_∩)O~】