




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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下。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从前一步找到的package以及其子package包中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:com.example.actions.MainAducts.Display (implements com.opensymphony.xwork2.Action)pany.details.ShowCompanyDetailsAction1.4 命名空间从定义的struts.convention.package.locators【标示开始到包结束】的部分,就是命名空间。如:配置com.xxx.web.user.userAction的命名空间是:“/user”;com.xxx.web.user.detail.UserAction的命名空间是:“/user/detail”。1.5 Actin类名路径分割Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用-分割,你可以设置.separator 如:如:UserAction-user UserDetailAction -user-detail。结合上面配置,对于com.xxx.web.user.detail.UserDetailAction,映射的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/WEB-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.htmlDispatcher/test/test2/hellodetail/WEB-INF/content/test/test3/hello- detail.htmlDispatcher以上的内容来自struts2的文档/2.1.6/docs/convention-plugin.html当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。1.7 Action注解通过Action注释对如下例子:Java代码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.jspmethod2/hello!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.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”; 我们可以通过:/different/url!method1.action或/another/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;import org.apache.convention.annotation.Actions;public class HelloAction extends ActionSupport Action(/another/url) public String method1() return “error”; 我们调用method1方法可以通过两种方式:1、/hello!method1.action映射 url:/WEB-INF/content/hello-error.jsp2、/another/url!method1.action映射 url:/WEB-INF/content/another/url-error.jsp可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。1.9 Namespace注解通过Namespace 注释package com.example.web;import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Actions;Namespace(/other)public class HelloWorld extends ActionSupport public String method1() return “error”; Action(url) public String method2() return “error”; Action(/different/url) public String method3() return “error”; 通过/other/hello-world!method1.action访问method1方法。通过/other/url!method2.action访问method2方法通过/different /url!method3.action访问method3方法与Action 注释不同的是,该注释覆盖了默认的namespace(这里是/),此时再用hello!method1.action 已经不能访问method1了.1.10 Results和Result注解Results和Result1 全局的(global)。全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。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.jsp2 本地的(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.convention.annotation.Result;import org.apache.convention.annotation.Results;public class HelloWorld extends ActionSupport Action(value=/other/bar,results=Result(name = error, location = ,type=redirect) public String method1() return “error”; 当我们调用/hello-world!method1.action时,返回/WEB-INF/content/hello-error.jsp当我们调用/other/bar!method1.action时,返回1.11 ParentPackage 注解ParentPackage注解用来定义具体action类的父XWork包或java包,下面例子演示了在action类上使用本注解:packagecom.example.actions;importcom.opensymphony.xwork2.ActionSupport;importorg.apache.struts2.convention.annotation.Action;importorg.apache.struts2.convention.annotation.ParentPackage;ParentPackage(customXWorkPackage)publicclassHelloWorldextendsActionSupportpublicStringexecute()returnSUCCESS;1.12 异常注解配置ExceptionMapping 注解用来影射action抛出的异常。可以参考exception mapping documentation 获得详细信息。注解用类级别,在这种情况下,注解会应用到类里面的所有actionExceptionMappings(ExceptionMapping(exception=java.lang.NullPointerException,result=success,params=param1,val1)publicclassExceptionsActionLevelActionpu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 平面构成课件道客
- Firefly-luciferase-mRNA-5moU-生命科学试剂-MCE
- FA-PEG-COOH-MW-3400-Folate-PEG-COOH-MW-3400-生命科学试剂-MCE
- Ethyl-3-methyl-2-methylenebutanoate-生命科学试剂-MCE
- 农发行绍兴市嵊州市2025秋招无领导模拟题角色攻略
- 2025年安康事业单位真题
- 央视群星配音大师课件
- 农发行陇南市成县2025秋招笔试EPI能力测试题专练及答案
- 平翘舌部分的课件
- 农发行盐城市滨海县2025秋招笔试综合模拟题库及答案
- 右江盆地低温金、锑矿床热液石英:显微结构与地球化学特征的成矿密码
- 小学学校“十五五”(2026-2030)发展规划
- 压力容器安全风险管控清单
- 2025年乡村产业发展笔试模拟题库
- 2025云南黄金矿业集团股份有限公司第二次招聘8人笔试备考试题及答案解析
- 第2课《中国人首次进入自己的空间站》教学设计统编版八年级语文上册
- 基础化学(第五版)课件 第一章 物质结构基础
- 化疗药物使用顺序课件
- 福州市晋安区社区工作者招聘笔试真题2024
- 教学课件模板美术
- 三基三严培训课件
评论
0/150
提交评论