struts2+spring注解配置简介

上传人:第*** 文档编号:49633509 上传时间:2018-07-31 格式:PPT 页数:25 大小:2.42MB
返回 下载 相关 举报
struts2+spring注解配置简介_第1页
第1页 / 共25页
struts2+spring注解配置简介_第2页
第2页 / 共25页
struts2+spring注解配置简介_第3页
第3页 / 共25页
struts2+spring注解配置简介_第4页
第4页 / 共25页
struts2+spring注解配置简介_第5页
第5页 / 共25页
点击查看更多>>
资源描述

《struts2+spring注解配置简介》由会员分享,可在线阅读,更多相关《struts2+spring注解配置简介(25页珍藏版)》请在金锄头文库上搜索。

1、Struts2+spring注解配置简简 介Struts2中使用注解配置Action 与Action配置相关的注解注解(Annotation)是J2SE5.0中提供的新特性,主要作用用于代替XML 配置文件,使用注解来提供配置信息。Struts2也提供了使用注解的方式来配 置Action,这样就不再需要struts.xml文件,以实现零配置。(需要导入struts2 -convention-plugin-2.1.6.jar, 从 struts2.1 版本开始, Convention Plugin 作为替换替换 Codebehind Plugin 来实现 Struts2 的零配置.同时导入 Co

2、nvention 与Codebehind 会出现启动异常) 在此插件中包含如下annotations :注解名称备注作用域Action指定一个action名称 类,方法, actions注解中 Actions给一个action指定多个名称方法ParentPackage指定继承的包(struts2)名 包,类 Namespace指定action所属的名称空间包,类 Result定义一个result映射 类,Results 注解中 Results定义一组result映射类,action注 解中 Namespaces指定一组action所属的名称空间包,类 InterceptorRef一个拦截器类,

3、 InterceptorRef s注解中 InterceptorRefs一组拦截器集合类,action注 解中Struts2中使用注解配置Action ParentPackageParentPackage注解的参数,如下:NameSpace注解的参数如下表:如: Namespace(value=“/”) 或NameSpace(“/list”)参数类型是否必需默认值描述valueString是无指要继承的父包注意:如果注解中只有一个名为value的参数,或者有其他参数,但其 他参数都有默认值,那么在为value参数设值时可以忽略value和等号 (=),例如: ParentPackage(val

4、ue=“struts-default”) ParentPackage(“struts-default”) 这二种配置是一样的参数类型是否必需默认值描述valueString是无指定action所属的名称空间Struts2中使用注解配置Action Nampspace及NamespacesNameSpace注解的参数如下表:如: Namespace(value=“/”) 或NameSpace(“/list”)注意:如果没有给定Namespace选项,会默认为从定义的.package.locators 标示开始到包结束的部分,就是命名空间,比如: com.ustb.web.user.userAct

5、ion的命名空间是:”/user”。 com.ustb.web.user.detail.UserAction的命名空间是:”/user/detail”Namepspaces用于声明一组名称空间。参数如下表所示:参数类型是否必需默认值描述valueString是无指定action所属的名称空间参数类型是否必 需默认值描述valueNamespace否无在里面定义一Namespace 的数组Namespaces(Namespace(“/crud“),Namespace(“/user“) public class BookAction extends ActionSupport. 也就是可以通过发起

6、:/crud/book!add.action/user/book!add.action 二个uri 请求都能访问到Action中的方法Struts2中使用注解配置Action 与注解有关的参数Result注解的参数如表所示:对应Results和Result可分为全局的与本地的 1. 全局的(global) 2. 本地的(local)参数类型是否 必需默认值 描述nameString否Actoin.SUCCESS指定result的逻辑名, 即代码结果 locationString是无指定result对应的 URL typeString否dispatcher指定result的类型paramsStr

7、ing否为result传递参数, 格式为 key1,value1,key2, value2Results( Result(name=“failure“, location=“/WEB- INF/fail.jsp“) ) public class HelloWorld extends ActionSupport public String execute()return “failure“; public class HelloWorld extends ActionSupport Action(value=“/other/bar“,results=Result(n ame = “error“,

8、 location = ““,type=“redirect“) public String method1() return “error“; Struts2中使用注解配置Action 与注解有关的参数 Results注解的参数如表所示:参数类型是否必需默认值描述valueResult是无为action定义一组result映射示例: Results(Result(name=“input”,value=“/input.jsp”),Result(name=“success” value=“/success.jsp”) )/上例中定义了两个result映射。一个逻辑名是input,资源位置是 /in

9、put.jsp。另一个逻辑名是success,资源位置是/success.jspStruts2中使用注解配置Action Action在默认的情况下,当Action的类名以Action字符结尾(如: HelloAction),不进行任何配置,将默认采用去掉”Action”的字符且把首字 母变成小写(如:HelloAction-hello)作为action的名字。如:通过使用Action可以ExceptionsMappings定action的名称。public class HelloAction extends ActionSupport public String method1() . pu

10、blic String method2() . /如果要调用上述的method1()及method2() 方法,可以用下面URI method1/hello!method1.action method2/hello!method2.action参数类型描述valueString指定action的名称resultsResults指定此action中的result 集合 interceptorRefsInterceptorRefs指定此action下对应的拦 截器 paramsString提供的参数exceptoinMappingsExceptionMappings指定的异常处理public cl

11、ass HelloAction extends ActionSupport Action(“action1“) public String method1() . Action(“/user/action2“) public String method2() .Action(value=“/user/action3“,results=Result(name=“viewsuccess“,location=“/view/showbooks.jsp“), Result(name=“viewinput“,location=“/view/showbooks.jsp“,type=“redirectActi

12、on“),interceptorRefs= InterceptorRef(“mydefaultStack“) ) public String method3() . /如果采用此种方式,可以使用如下方式访问 method1/action1!method1.action. method2/user/action2!method2.action method3 /user/action3!method3.actionStruts2中使用注解配置Action Actions可以为action类定义一组访问的action名称,主要有如下参数:比如:一个方法被Action或Actions注释后,只是多了

13、一种调用方式,而不 是说覆盖了原来的调用方式。参数名类型描述valueAction用于包含一组Action注解public class HelloAction extends ActionSupport Actions( Action(“/different/url“), Action(“/another/url“) ) public String method1() . /different/url!method1.action 或/another/url!method1.action 来调用method1 方法。public class HelloAction extends Action

14、Support Action(“/another/url“) public String method1() . 我们调用method1方法可以通过两种方式: 1 /hello!method1.action 2 /another/url!method1.action 可见,两种方式均可对method1方法进行调用,唯一的区别就是, 两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一 个非常好的选择。Struts2中使用注解配置Action 当Namespace与Action配合使用注意事项如果在Action注解中包括”/”字符地,则意味着该注释覆盖了默认的 namespace(这里是/

15、),此时访问action就有一些小的改动。如下例:Namespace(“/other“) public class HelloWorld extends ActionSupport public String method1() . Action(“url“) public String method2() . Action(“/different/url“) public String method3() . 通过 /other/hello-world!method1.action 访问method1 方法。 通过 /other/url!method2.action 访问method2 方法 通过 /different /url!method3.action 访问method3 方法 与Action 注释不同的是,该注释覆盖了默认的namespace(这里是/) ,此时再用hello!method1.action 已经不能访问method1 了.Struts2中使用注解配置Action InterceptorRef及InterceptorRefs可以使用InterceptorRef来为一个action定义一个拦截器,注意此注 解只是在此acti

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号