struts2零配置Action路径到Result页面路径的自动映射.doc
《struts2零配置Action路径到Result页面路径的自动映射.doc》由会员分享,可在线阅读,更多相关《struts2零配置Action路径到Result页面路径的自动映射.doc(7页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Struts2约定优于配置(Action路径到Result页面路径的自动映射)1 Convention插件1.1 需要的jar包struts2-convention-plugin-2.2.1.jar1.2 设置Convention结果result页面存放路径目录struts-plugin.xml文件中:默认配置所有的结果result页面都存储在WEB-INF/content下,通过设置struts.convention.result.path属性的值改变结果result页面到其他路径。如:Xml代码则将result路径配置到了WEB-INF/page 下;则将result路径配置到了/page
2、下。1.3 设置Convention的Action类存在路径搜索包struts-plugin.xml文件中:默认配置包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。通过设置struts.convention.package.locators属性来修改这个配置。如:Xml代码则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。com.xxx.web.*/com.xxx.action.*都将被视为含有Action的包路径而被搜索。接着,Convention从前一步找到的pack
3、age以及其子package包中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:com.example.actions.MainActioncom.example.actions.products.Display (implements com.opensymphony.xwork2.Action)pany.details.ShowCompanyDetailsAction1.4 命名空间从定义的struts.convention.package.locators【标示开始到包结束】的部分,就是命名空间。如:配置com.xxx.web.use
4、r.userAction的命名空间是:“/user”;com.xxx.web.user.detail.UserAction的命名空间是:“/user/detail”。1.5 Actin类名路径分割Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用-分割,你可以设置struts.convention.action.name.separator 如:如:UserAction-user UserDetailAction -user-detail。结合上面配置,对于com.xxx.web.user.detail.UserDetai
5、lAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp1.6 支持jsp、html、htm、vm等格式struts支持.jsp, .html, .htm, .vm格式的文件。下面是action和结果模版的映射关系:URLResultFile that could matchResult Type/hellosuccess/WEB-INF/content/hello.jspDispatcher/helloupdate/WEB-INF/content/hello-update.jspDispatcher/hellosuccess/W
6、EB-INF/content/hello-success.htmDispatcher/hellosuccess/WEB-INF/content/hello.ftlFreeMarker/hello-worldinput/WEB-INF/content/hello-world-input.vmVelocity/test/test1/helloerror/WEB-INF/content/test/test1/hello-error.htmlDispatcher/test/test2/hellonew/WEB-INF/content/test/test2/hello-new.htmlDispatche
7、r/test/test2/hellodetail/WEB-INF/content/test/test3/hello- detail.htmlDispatcher以上的内容来自struts2的文档http:/struts.apache.org/2.1.6/docs/convention-plugin.html当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。1.7 Action注解通过Action注释对如下例子:Java代码package com.example.web;import com.opensymphony.xwork2.A
8、ction;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.jspmethod2/hello!
9、method2.action/WEB-INF/content/hello.jsp通过Action注释后方法名Action注释后调用路径Action注释 后映射路径method1/action1!method1.action/WEB-INF/content/action1.jspmethod1/user/action2!method2.action/WEB-INF/content/user/action2.jsp1.8 Actions注解通过Actions注释Java代码package com.example.web;import com.opensymphony.xwork2.ActionSu
10、pport; 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”; 我们可以通过:/different/url!method1.action或/a
11、nother/url!method1.action来调用method1方法。对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp可能误导了大家,一个方法被Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。比如对于如下例子:Java代码com.example.web;import com.opensymphony.xwork2.ActionSupport; import org.apache.convention.annotation.Action
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- struts2 配置 Action 路径 Result 页面 自动 映射
限制150内