一、约定大于配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定自动配置。
使用Convention:7.2、 第二个Action与第一个Action处在同一个包下
7.3、 第二个Action影射的URL为:firstActionURL+“-”+resultCODE
如:package 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; } }
struts.convention.result.flatLayout—如果是为false则可以将试图放置Action对应的目录下,无需放入WEB-INF/content
二、Convention注解
1. Convention的Annotationprivate String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Actions({ @Action(value = "login1", params = { "message", "指定Result!" }), @Action(value = "login2") }) public String login() { return "str"; } @Action(value = "other") public String abc() { return "abc"; }我们可以通过/login1.action访问,而在访问时,message这个属性已经有值,为message=”指定Result!” 返回的视图是login1-str.jsp;
2.1.2、@Result相当于struts.xml文件的<result/>元素。使用@Result必须指定一个name属性,相当于<result name=””/>。
2.1.3、@Result可选的属性:
① type 相当于<result type=””/>指定返回视图资源的类型
② location 相当于<result>…..</result>中间部分,用于指定实际视图位置@Results({ @Result(name = "failure", location = "fail.jsp") }) public class HelloWorld extends ActionSupport { @Action(value = "/different/url", results = { @Result(name = "success", location = "http://struts.apache.org", type = "redirect") }) public String execute() { return SUCCESS; } @Action("/another/url") public String doSomething() { return SUCCESS; } }2.1、@ResultPath则用于修饰包和Action类,用于改变被修饰Action所对应的物理视图资源的根路径。例如:默认情况下,Convention都会到WEB-INF/content路径下找物理视图资源,一旦我们使用@ResultPath(“/WEB-INF/jsps”)修饰该Action,系统将回到/WEB-INF/jsps目录下寻找物理视图资源。
@ResultPath("/WEB-INF/jsps") public class HelloWorld extends ActionSupport { public String execute() { return SUCCESS; } }3、包和命名空间相关的Annotation:
@ExceptionMappings({ @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success", params = { "param1", "val1" }) }) public class ExceptionsActionLevelAction { public String execute() throws Exception { return null; } }② 方法级的异常定义: 将多个@ExceptionMapping组成数组后作为@Action的exceptionMappings属性值,这种异常定义仅对被修饰的方法有效。
public class ExceptionsMethodLevelAction { @Action(value = "exception1", exceptionMappings = { @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success", params = { "param1", "val1" }) }) public String run1() throws Exception { return null; } }5、拦截器配置相关的Annotation
@InterceptorRef用于为指定Action引用lanjieq或者是拦截器栈。也就相当于strut.xml中位于<action../>元素内部的<interceptor-ref../>子元素的作用。使用@InterceptorRefAnnotation时,必须制定一个value属性,用于指定所引用的拦截器或拦截器栈的名字。相当于<interceptor-ref../>子元素里name属性的作用。
@InterceptorRefs({ @InterceptorRef("interceptor-1"),@InterceptorRef("defaultStack") }) public class HelloWorld extends ActionSupport { @Action(value = "action1", interceptorRefs = @InterceptorRef("validation")) public String execute() { return SUCCESS; } @Action(value = "action2") public String doSomething() { return SUCCESS; } }
三、struts-plugin.xml 常量
默认常量:
<constant name="struts.convention.actionConfigBuilder" value="convention"/> <constant name="struts.convention.actionNameBuilder" value="convention"/> <constant name="struts.convention.resultMapBuilder" value="convention"/> <constant name="struts.convention.interceptorMapBuilder" value="convention"/> <constant name="struts.convention.conventionsService" value="convention"/> <constant name="struts.convention.result.path" value="/WEB-INF/content/"/> <constant name="struts.convention.result.flatLayout" value="true"/> <constant name="struts.convention.action.suffix" value="Action"/> <constant name="struts.convention.action.disableScanning" value="false"/> <constant name="struts.convention.action.mapAllMatches" value="false"/> <constant name="struts.convention.action.checkImplementsAction" value="true"/> <constant name="struts.convention.default.parent.package" value="convention-default"/> <constant name="struts.convention.action.name.lowercase" value="true"/> <constant name="struts.convention.action.name.separator" value="-"/> <constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/> <constant name="struts.convention.package.locators.disable" value="false"/> <constant name="struts.convention.package.locators.basePackage" value=""/> <constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*"/> <constant name="struts.convention.relative.result.types" value="dispatcher,velocity,freemarker"/> <constant name="struts.convention.redirect.to.slash" value="true"/> <constant name="struts.convention.action.alwaysMapExecute" value="true"/> <constant name="struts.mapper.alwaysSelectFullNamespace" value="true"/> <!-- <constant name="struts.convention.action.includeJars" /> --> <constant name="struts.convention.action.fileProtocols" value="jar" /> <constant name="struts.convention.classes.reload" value="false" /> <constant name="struts.convention.exclude.parentClassLoader" value="true" />
Add a constant element to your struts config file to change the value of a configuration setting, like:
<constant name="struts.convention.result.path" value="/WEB-INF/mytemplates/"/>
Name | Default Value | Description |
---|---|---|
struts.convention.action.alwaysMapExecute | true | Set to false, to prevent Convention from creating a default mapping to "execute" when there are other methods annotated as actions in the class |
struts.convention.action.includeJars | Comma separated list of regular expressions of jar URLs to be scanned. eg. ".myJar-0\.2.,.thirdparty-0\.1." | |
struts.convention.action.packages | An optional list of action packages that this should create configuration for (they don't need to match a locator pattern) | |
struts.convention.result.path | /WEB-INF/content/ | Directory where templates are located |
struts.convention.result.flatLayout | true | If set to false, the result can be put in its own directory: resultsRoot/namespace/actionName/result.extension |
struts.convention.action.suffix | Action | Suffix used to find actions based on class names |
struts.convention.action.disableScanning | false | Scan packages for actions |
struts.convention.action.mapAllMatches | false | Create action mappings, even if no @Action is found |
struts.convention.action.checkImplementsAction | true | Check if an action implements com.opensymphony.xwork2.Action to create an action mapping |
struts.convention.default.parent.package | convention-default | Default parent package for action mappins |
struts.convention.action.name.lowercase | true | Convert action name to lowercase |
struts.convention.action.name.separator | - | Separator used to build the action name, MyAction -> my-action. This character is also used as the separator between the action name and the result in templates, like action-result.jsp |
struts.convention.package.locators | action,actions,struts,struts2 | Packages whose name end with one of these strings will be scanned for actions |
struts.convention.package.locators.disable | false | Disable the scanning of packages based on package locators |
struts.convention.exclude.packages | org.apache.struts.*, org.apache.struts2.*, org.springframework.web.struts.*, org.springframework.web.struts2.*, org.hibernate.* |
Packages excluded from the action scanning |
struts.convention.package.locators.basePackage | If set, only packages that start with its value will be scanned for actions | |
struts.convention.relative.result.types | dispatcher,velocity,freemarker | The list of result types that can have locations that are relative and the result location (which is the resultPath plus the namespace) prepended to them |
struts.convention.redirect.to.slash | true | A boolean parameter that controls whether or not this will handle unknown actions in the same manner as Apache, Tomcat and other web servers. This handling will send back a redirect for URLs such as /foo to /foo/ if there doesn't exist an action that responds to /foo |
struts.convention.classLoader.excludeParent | true | Exclude URLs found by the parent class loader from the list of URLs scanned to find actions (needs to be set to false for JBoss 5) |
struts.convention.action.eagerLoading | false | If set, found action classes will be instantiated by the ObjectFactory to accelerate future use, setting it up can clash with Spring managed beans |
1、当使用@Namespace将会覆盖类包定义的Namespace,其他定义的有效Namespace将不通过Action直接找该命名空间下的对应的视图。
2、当使用@Action(value = "/url")时,使用@Namespace定义的命名空间无效,该命名空间为content。
3、使用@Action(value = "url")时,同时使用了@Namespace,完整路径就是@Namespace+url