




免费预览已结束,剩余18页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
简述STRUTS2 Convention零配置从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定自动配置。如何使用Convention1. 将struts-Convention-plugin-2.1.6.jar文件复制到WEB-INF/lib路径下2. 对于Convention插件而言,它会自动搜索位于action,actions,struts,struts2包下的所有java类,Convention插件会把如下两种java类当成Action处理:1) 所有实现了com.opensymphony.xwork2.Action的java类2) 所有类名以Action结尾的java类3. Convention插件还允许设置如下三个常量:1) struts.Convention.exclude.packges:指定不扫描哪些包下的java类,位于这些包结构下的java类将不会自动映射成Action;2) struts.convention.package.locators:Convention插件使用该常量指定的包作为搜寻Action的根包。对于actions.fore.LoginAction类,按约定原本应映射到/fore/login;如果将该常量设为fore,则该Action将会映射到/login3) struts.convention.action.packages:Convention插件以该常量指定包作为根包来搜索Action类。Convention插件除了扫描action,actions,struts,struts2四个包的类以外,还会扫描该常量指定的一个或多个包,Convention会视图从中发现Action类。注意:struts.convention.package.locators和struts.convention.action.packages两个常量的作用比较微妙,开发者在利用这两个常量时务必小心。如:下面Action所在包被映射的命名空间如下:com.fun.actions.LoginAction 映射到 /com.fun.actions.myoffice.CarInfoAction 映射到 /myofficecom.fun.struts.myoffice.EntINfoAction 映射到 /myofiice4. 映射Action的name时,遵循如下两步规则:1) 如果该Action类名包含Action后缀,将该Action类名的Action后缀去掉。否则不做任何处理。2) 将Action类名的驼峰写法(每个单词首字母大写、其他字母小写的写法)转成中画线写法(所有字母小写,单词与单词之间以中画线隔开)如:LoginAction映射的Acion的name属性为login,GetBooks映射的Action的name的属性为get-books,AddEmployeeAction映射的Action的name属性为add-employee5. 默认情况下。Convention总会到WEB应用的WEB-INF/content路径下定位物理资源,定位资源的约定是:actionRUL+resultCODE+suffix。当某个逻辑视图找不到对应的视图资源时,Convention会自动视图使用ActionURL+suffix作为物理视图资源。 如:actions.fore.LoginAction 返回success字符串时,Convention优先考虑使用WEB-INF/content/fore目录下的login-success.jsp作为视图资源。如果找不到该文件,login.jsp也可作为对应的视图资源。如果返回input字符串,Convention会将WEB-INF/content/fore里面查找login-input.jsp6. 为了看到struts2应用里的Action等各种资源的影射情况,struts2提供了Config Browser插件。使用方法,将struts2-config-browser-plugin-2.1.6.jar文件复制到struts2应用的WEB-INFlib目录中。打开首页地址:http:/localhost:8080/应用名字/config-browser/actionNames.action这里可以看到Config Browser插件的首页。注意:这里不管开发者是否使用struts.xml文件进行配置,一样可以看到struts的配置信息。7.Action链的约定如果希望一个Action处理结束后不是进入一个视图页面,而是进行另一个Action形成的Action链。通过Convention插件则只需遵守如下三个约定即可。1) 第一个Action返回的逻辑视图字符串没有对应的视图资源2) 第二个Action与第一个Action处在同一个包下3) 第二个Action影射的URL为:firstActionURL+resultCODE如,第一个Action为OneAction,里面有个方法返回为“two”,那么就得保证,WEB-INF/content/下没有one.jsp或one-two.jsp 对于第二个action 它的名字应该是OneTwoAction,而对应的url应该是:“one-two.action”注意:由于Convention插件根据Action和jsp页面来动态生成映射的,因此不管是Acion的改变,还是JSP页面的改变都需要Convention插件重新加载映射。那么只要我们为struts2应用配置如下两个常量就可以了。几个重要的常量struts.convention.action.disableJarScanning-是否从包中搜索Actionstruts.convention.action.package-Convention插件以该常量指定包作为根包struts.convention.result.path -设置Convention插件定位视图资源的根路径。默认值为/WEB-INF/contentstruts.convention.result.flatLayout如果是为false则可以将视图放置Action对应的目录下,无需放入WEB-INF/content7. Convention的Annotation1) 与Action相关的两个Annotation是Action 和Actions2) Action中可指定一个value属性。类似于指定属性值3) Action中还可以指定一个params属性,该属性是一个字符串数组,用于该Acion指定的参数名和参数值。params属性应遵守如下格式:“name1”,”value1”,”name2”,”value2”4) Actions 也用于修饰Action类里的方法,用于将该方法映射到多个URL.Actions用于组织多个Action.因此它可将一个方法映射成多个逻辑Action。如:package com.fun.actions;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Actions;import com.fun.service.LoginService;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport private String str;Actions(Action(value=login1,params=str,这是已经注入的了!),Action(value=login2)public String login()return str;Action(value=ggg)public String abc()return abc;public String getStr() return str;public void setStr(String str) this.str = str;我们可以通过/login1.action访问,而在访问时,str这个属性已经有值,为str=”这是已经注入的!” 返回的视图是login1-str.jsp当我们用/login2.action访问时,str的值为null。返回的视图为 login2-str.jsp而我们通过/ggg.action调用的是abc()方法,返回的视图为/ggg-abc.jsp8. 与Result配置相关的Annotation1)ResultPath Result 和Results2)Results用于组织多个Result因此它只需指定一个value属性值,该value属性值为多个Result3)Result相当于struts.xml文件中的元素的做哟欧诺个。使用Result必须指定一个name属性,相当于另外,它还有几个可选的属性。 type 相当于指定返回视图资源的类型 location 相当于.中间部分,用于指定实际视图位置 params:该属性相当于元素里多个子元素的作用,用于为该Result指定参数值。该属性应满足“name1”,”value1”,”name2”,”value2”格式4)Result有以下两种用法1 Action级的Result映射:以Actions组合多个Action后修饰的Action类。这种Result映射对该Action里的所有方法都有效。2方法级Result映射:将多个Result组成数组后作为Action的results属性值。这种Result映射仅对被修饰的方法有效。5)ResultPath则用于修饰包和Action类,用于改变被修饰Action所对应的物理视图资源的根路径。举例说:默认情况下,Convention都会到WEB-INF/content路径下找物理视图资源,一旦我们使用ResultPath(“/abc”)修饰该Action,系统将回到abc目录下寻找物理视图资源。举例:在默认情况下,Convention都会到WEB-INF/content路径下需找物理视图资源,一旦我们使用ResultPath(“/abc”)修饰该Action,系统会到abc目录下寻找物理视图资源。9 与包和命名空间相关的Annotation: Namespace:修饰Action类或其所在的包。该Annotation中指定一个value属性值,用于指定被修饰的Action所在的命名空间 Namespaces:修饰Action类或其所在的包,用于组合多个Namespace ParentPackage: 用于指定被修饰的Action所在包的父包。10 异常处理相关的Annotation ExceptionMappings 用于组织多个ExceptionMapping,因此它只需指定一个value属性值,该value属性值为多个ExceptionMapping。 ExceptionMapping 用于定义异常类和物理视图之间的对应关系,也相当于struts.xml文件里元素的作用 使用时,必须注意以下两个属性: exception: 用于指定异常类 result : 用于指定逻辑视图ExceptionMpping有如下两种用法 Action级的异常定义:以ExceptionMappings组合多个ExceptionMapping后修饰的Action类。这种异常定义对Action中的所有方法有效 方法级的异常定义: 将多个ExceptionMapping组成数组后作为Action的exceptionMappings属性值,这种异常定义仅对被修饰的方法有效。11.拦截器配置相关的Annotation 与拦截器配置的Annotation有InterceptorRef、InterceptorRefs和DefaultInterceptorRefInterceptorRefs用于组织多个InterceptorRef,因此它只需要指定一个value属性值,该value属性值为多个InterceptorRefInterceptorRef用于为指定Action引用lanjieq或者是拦截器栈。也就相当于strut.xml中位于元素内部的子元素的作用。使用InterceptorRefAnnotation时,必须制定一个value属性,用于指定所引用的拦截器或拦截器栈的名字。相当于子元素里name属性的作用。IntroductionThe Convention Plugin is bundled with Struts since 2.1 and replaces the Codebehind Plugin and Zero Config plugins. It provides the following features: Action location by package naming conventions Result (JSP, FreeMarker, etc) location by naming conventions Class name to URL naming convention Package name to namespace convention SEO compliant URLs (i.e. my-action rather than MyAction) Action name overrides using annotations Interceptor overrides using annotations Namespace overrides using annotations XWork package overrides using annotations Default action and result handling (i.e. /products will try com.example.actions.Products as well as ducts.Index) The Convention Plugin should require no configuration to use. Many of the conventions can be controlled using configuration properties and many of the classes can be extended or overridden.SetupIn order to use the Convention plugin, you first need to add the JAR file to the WEB-INF/lib directory of your application (if you package your actions in jar files, make sure that struts.convention.action.disableJarScanning is set to true). Hello worldNow that the Convention plugin has been added to your application, lets start with a very simple example. This example will use an actionless result that is identified by the URL. By default, the Convention plugin assumes that all of the results are stored in WEB-INF/content. This can be changed by setting the property struts.convention.result.path in the Struts properties file to the new location. Dont worry about trailing slashes, the Convention plugin handles this for you. Here is our hello world JSP:WEB-INF/content/hello-world.jspHello world!If you start Tomcat (or whichever J2EE container you are using) and type in http:/localhost:8080/hello-world into your browser you should get this result:WEB-INF/content/hello-world.jspHello world!This illustrates that the Convention plugin will find results even when no action exists and it is all based on the URL passed to Struts.Code behind hello worldLets expand on this example and add a code behind class. In order to do this we need to ensure that the Convention plugin is able to find our action classes. By default, the Convention plugin will find all action classes that implement com.opensymphony.xwork2.Action or whose name ends with the word Action in specific packages. These packages are located by the Convention plugin using a search methodology. First the Convention plugin finds packages named struts, struts2, action or actions. Any packages that match those names are considered the root packages for the Convention plugin. Next, the plugin looks at all of the classes in those packages as well as sub-packages and determines if the classes implement com.opensymphony.xwork2.Action or if their name ends with Action (i.e. FooAction). Heres an example of a few classes that the Convention plugin will find:Classescom.example.actions.MainAducts.Display (implements com.opensymphony.xwork2.Action)pany.details.ShowCompanyDetailsActionEach of the action classes that the plugin finds will be configured to respond to specific URLs. The URL is based on the package name that the class is defined in and the class name itself. First the plugin determines the namespace of the URL using the package names between the root package and the package the class is defined in. For our examples above, the namespaces would be:Namespacescom.example.actions.MainAction - /ducts.Display - /pany.details.ShowCompanyDetailsAction - /company/detailsNext, the plugin determines the URL of the resource using the class name. It first removes the word Action from the end of the class name and then converts camel case names to dashes. In our example the full URLs would be:Full URLscom.example.actions.MainAction - /ducts.Display - /products/pany.details.ShowCompanyDetailsAction - /company/details/show-company-detailsYou can tell the Convention plugin to ignore certain packages using the property struts.convention.exclude.packages. You can also tell the plugin to use different strings to locate root packages using the property struts.convention.package.locators. Finally, you can tell the plugin to search specific root packages using the property struts.convention.action.packages.Here is our code behind action class:com.example.actions.HelloWorldpackage com.example.actions;import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport private String message; public String getMessage() return message; public String execute() message = Hello World!; return SUCCESS; If you compile this class and place it into your application in the WEB-INF/classes, the Convention plugin will find the class and map the URL /hello-world to it. Next, we need to update our JSP to print out the message we setup in the action class. Here is the new JSP:WEB-INF/content/hello-world.jspThe message is $messageIf start up the application server and open up http:/localhost:8080/hello-world in our browser, we should get this result:ResultThe message is Hello World!Results and result codesThe Convention Plugin will pre-configure all of you action classes when Struts is started. By default, this configuration will also contain results for any JSPs that it can find within the application. The JSPs have an additional feature that allows different JSPs to be used based on the result code of the action. Since action methods return Strings and these Strings are traditionally used to locate results for the action, the Convention plugin allows you to define different results based on the result code.Building on our example from above, lets say we want to provide a different result if the result code from our action is the String zero rather than success. First, we update the action class to return different result codes:com.example.actions.HelloWorldpackage com.example.actions;import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport private String message; public String getMessage() return message; public String execute() if (System.currentTimeMillis() % 2 = 0) message = Its 0; return zero; message = Its 1; return SUCCESS; Next, we add a new JSP to the application named WEB-INF/content/hello-world-zero.jsp. Notice that the first part of the file name is the same as the URL of the action and the last part of the name is the result code. This is the convention that the plugin uses to determine which results to render. Here is our new JSP:WEB-INF/content/hello-world.jspThe error message is $messageNow, if you compile the action and restart the application, based on the current time, youll either see the result from WEB-INF/content/hello-world.jsp or WEB-INF/content/hello-world-zero.jsp.The result type is based on the extension of the file. The supported extensions are: jsp,ftl,vm,html,html. Examples of Action and Result to Template mapping:URLResultFile that could matchResult Type/hellosuccess/WEB-INF/content/hello.jspDispatcher/hellosuccess/WEB-INF/content/hello-success.htmDispatcher/hellosuccess/WEB-INF/content/hello.ftlFreeMarker/hello-worldinput/WEB-INF/content/hello-world-input.vmVelocity/test1/test2/helloerror/WEB-INF/content/test/test2/hello-error.htmlDispatcherChainingIf one action returns the name of another action in the same package, they will be chained together, if the first action doesnt have any result defined for that code. In the following example:com.example.actions.HelloWorldpackage com.example.actions;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport Action(foo) public String foo() return bar; Action(foo-bar) public String bar() return SUCCESS; The foo action will be executed, because no result is found, the Convention plugin tries to find an action named foo-bar on the same package where foo is defined. If such an action is found, it will be invoked using the chain result.XWork packagesActions are placed on a custom XWork package which prevents conflicts. The name of this package is based on the Java package the action is defined in, the namespace part of the URL for the action and the parent XWork package for the action. The parent XWork package is determined based on the property named struts.convention.default.parent.package(defaults to conventionDefault), which is a custom XWork package that extends strutsDefault. Therefore the naming for XWork packages used by the Convention plugin are in the form:XWork package naming#Using our example from above, the XWork package for our action would be:XWork package namingcom.example.actions#/#conventionDefaultAnnotation referenceThe Convention plugin uses a number of different annotations to override the default conventions that are used to map actions to URLs and locate results. In addition, you can modify the parent XWork package that actions are configured with.Action annotationThe Convention plugin allows action classes to change the URL that they are mapped to using the Action annotation. This annotation can also be used inside the Actions annotation to allow multiple URLs to map to a single action class. This annotation must be defined on action methods like this:com.example.actions.HelloWorldpackage com.example.actions;import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action;public class HelloWorld extends ActionSupport Action(/different/url) public String execute() return SUCCESS; Our action class will now map to the URL /different/url rather than /hello-world. If no Result (see next section) is specified, then the namespace of the action will be used as the path to the result, on our last example it would be /WEB-INF/content/different/url.jsp.A single method within an action class can also map to multiple URLs using the Actions annotation like this:com.example.actions.HelloWorldpackage com.example.actions;import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Actions;public class HelloWorld extends ActionSupport Actions( Action(/different/url), Action(/another/url) ) public String execute() return SUCCESS; Another usage of the Action or Actions annotation is to define multiple action methods within a single action class, each of which respond to a different URL. Here is an example of multiple action methods:com.example.actions.HelloWorldpackage com.example.actions;import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Actions;public class HelloWorld extends ActionSupport Action(/different/url) public String execute() return SUCCESS; Action(url) public String doSomething() return SUCCESS; The previous example defines a second URL that is not fully qualifi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中央废墟清缴管理办法
- 贵金属交易管理办法
- 《水利工程管理办法》
- 设计后评估管理办法
- 管理办法算不算制度
- 上门收费人员管理办法
- 磷矿石防盗管理办法
- 仓头中学餐馆管理办法
- 素数分析可再生能源-洞察及研究
- 网格员经费管理办法
- 国际投资学(investment)讲义课件
- 施工机具进场检查验收记录
- 二年级健康成长上册教案
- 民俗学概论 第一章 概述课件
- 时代邻里4度°服务美学品质关怀体系
- 供水公司主要安全风险公告栏(总)
- 《农产品贮藏与加工》课件第三章稻谷精深加工
- 外研版五年级上册英语(全册)单元教材分析
- 【课件】音响的感知课件-高中音乐湘教版(2019)音乐鉴赏
- 华为-计划、预算和核算
- 膝关节置换术的护理
评论
0/150
提交评论