ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:17.80KB ,
资源ID:24192497      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/24192497.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Struts2使用Convention插件用法.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Struts2使用Convention插件用法.docx

1、Struts2使用Convention插件用法Struts2使用 - Convention插件(转)(2011-04-09 01:28:30)转载标签: 杂谈 分类: struts2 1.1. 设置结果页面路径默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如:Xml代码: 则将路径配置到了WEB-INF/page 下。1.2. 设置Convention搜索包默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路

2、径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如: 则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。Com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。接着,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:com.example.actions.MainActioncom.example.actions.p

3、roducts.Display (implements com.opensymphony.xwork2.Action)pany.details.ShowCompanyDetailsAction 1.3. 命名空间命名空间。从定义的.package.locators标示开始到包结束的部分,就是命名空间。举个例子:Com.ustb.web.user.userAction的命名空间是:”/user”。Com.ustb.web.user.detail.UserAction的命名空间是:”/user/detail” 1.4. Actin类名路径分割Convention通过如下规则确定URL的具体资源部分

4、:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用-分割,你可以设置struts.convention.action.name.separator 如还是举个例子:UserAction-user UserDetailAction -user-detail。结合上面的。对于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp1.5. 支持jsp、html、htm、vm等格式struts支持.jsp .html .htm .vm格式的文件。下面是a

5、ctiong和结果模版的映射关系: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 input /WEB-INF/content/hello-world-input.vm Velocity /hello erro

6、r /WEB-INF/content/test/test2/hello-error.html Dispatcher 以上的内容来自struts2的文档http:/struts.apache.org/2.1.6/docs/convention-plugin.html当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。 1.6. Action注解 通过Action注释对如下例子:Java代码package com.example.web;import com.opensymphony.xwork2.Action;import com.op

7、ensymphony.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.actio

8、n. /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.jsp 1.7. Actions注解通过Actions注释,例子:Java代码package com.example.web; import com.opensymphony.xwork2.Actio

9、nSupport;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

10、 或 /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

11、.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/cont

12、ent/another/url-error.jsp可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。1.8. Namespace注解通过Namespace 注释package com.example.web; import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Act

13、ions;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.actio

14、n 访问method2 方法通过 /different /url!method3.action 访问method3 方法与Action 注释不同的是,该注释覆盖了默认的namespace(这里是/),此时再用hello!method1.action 已经不能访问method1 了.1.9. Results和Result注解Results和Result1.9.1.全局的(global)。全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。package com.example.actions; import com.opensym

15、phony.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/fa

16、il.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!met

17、hod2.action 时,返回 /WEB-INF/fail.jsp1.9.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.convention.ann

18、otation.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.

19、jsp当我们调用 /other/bar!method1.action 时,返回 1.10. ParentPackage 注解ParentPackage注解用来定义具体action类的父XWork包或java包,下面例子演示了在action类上使用本注解: package com.example.actions;import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annota

20、tion.ParentPackage;ParentPackage(customXWorkPackage)public class HelloWorld extends ActionSupport public String execute() return SUCCESS; 1.11. 异常注解配置ExceptionMapping 注解用来影射action抛出的异常。可以参考exception mapping documentation 获得详细信息。注解用类级别,在这种情况下,注解会应用到类里面的所有actionExceptionMappings(ExceptionMapping(excep

21、tion = java.lang.NullPointerException,result = success, params = param1, val1)public class ExceptionsActionLevelAction public String execute() throws Exception return null; 可以在ExceptionMapping注解中使用params 属性来传递具体值给结果渲染页。ExceptionMapping注解同样可以在action级别进行设置: public class ExceptionsMethodLevelAction Act

22、ion(value = exception1, exceptionMappings = ExceptionMapping(exception = java.lang.NullPointerException,result = success, params = param1, val1) ) public String run1() throws Exception return null; 1.12. 自动加载无需启动服务Convention插件可以自动重新加载配置的功能,无需重启容器,就可以刷新类中包含的action。这自动加载automatic xml 配置文件类似。你必须在struts.xml 中添加以下代码来启用本功能: 此功能没有在所有容器中进行过测试,强力建议不要在生产环境中使用。1.13. 扫描Action的Jar包默认情况下,Convention 插件不会从jar文件中寻找action。如果想实现这一功能,jar文件必须被struts.convention.action.includeJars 所定义的正则 匹配到。在例子中 myjar1.jar和 myjar2.jar 将被插件检测到: 提示:正则表达式只针对jar文件的路径进行匹配,而不是文件名。jar的URL应该包含jar文件的路径并以!/结尾。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1