Struct2常量(Constants)
struts.properties 常用配置文件
<constant name="struts.i18n.encoding" value="UTF-8" />字符集
<constant name="struts.convention.action.packages" value="cn.jingmin.web" /> 只搜索特定package下的Action
<constant name="struts.convention.result.path" value="/WEB-INF/context" /> 指定加载页面的目录
<constant name="struts.convention.relative.result.types" value="dispatcher,velocity,freemarker" />返回的页面类型
<constant name="struts.convention.exclude.packages" value="" />忽略某些包
<constant name="struts.convention.package.locators" value="" /> Convention默认的根packages
<constant name="struts.convention.classes.reload" value="true" />
<constant name="struts.objectFactory" value="spring" /> 指定spring自动装载
<constant name="struts.action.extension" value="action" /> 指定struts的扩展名
<constant name="struts.devMode" value="true" /> 指定struts的开发模式
<constant name="struts.i18n.reload" value="action" /> 指定struts的资源文件自动重载
<constant name="struts.custom.i18n.resource" value="" /> 指定struts的国际化资源文件
<constant name="struts.configuration.xml.reload" value="" /> 指定struts的配置文件是否自动重载
<constant name="struts.ul.theme" value="xthml" /> 指定struts的模板
Struts 2的convention-plugin插件
Maven下对应依赖包配置:
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.1.6</version></dependency>
<constant name="struts.convention.result.path" value="/WEB-INF/page" />
com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:com.example.actions.MainActioncom.example.actions.products.Display (implements com.opensymphony.xwork2.Action)com.example.struts.company.details.ShowCompanyDetailsAction
<constant name="struts.convention.action.name.separator" value="-" />
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; } }
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”; }
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 { @Action("/another/url") public String method1() { return “error”; }
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”; } }
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”; } }
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; public class HelloWorld extends ActionSupport { @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")}) public String method1() { return “error”; } }