您的位置:首页 > 编程语言 > Java开发

Struts2 Convention 系列 1

2016-03-17 16:18 429 查看

struts2中的convention插件,可以大大地减少开发过程中的配置量。更多更具体配置信息,请文档:点击打开链接以下以maven开发为例。

流程

1、工具包a) struts2-core.jar;struts2的核心包。b) struts2-convention-plugin;convention插件。c) struts2-config-browser-plugin;查看映射情况。2、web.xml配置,略。3、struts.xml配置a) 设置为开发模式。<constant name="struts.devMode" value="true" />b) 自动加载。(或许会出现NullPointerException异常)<constant name="struts.convention.classes.reload" value="true"></constant>c) 结果映射路径。<constant name="struts.convention.result.path" value="/WEB-INF" /> 具体配置如下:
<struts><!-- 默认编码 --><constant name="struts.i18n.encoding" value="utf8" /><!-- 上传文件大小限制为10M --><constant name="struts.multipart.maxSize" value="20971520" /><!-- 是否显示详细错误信息 --><constant name="struts.devMode" value="true" /><!-- 是否在struts.xml修改后重新加载 ,默认false --><constant name="struts.configuration.xml.reload" value="true" /><!-- 是否每次HTTP请求到达时,系统都重新加载资源文件,默认false --><constant name="struts.i18n.reload" value="false" /><!-- 国际化资源文件名称 --><constant name="struts.custom.i18n.resources" value="i18n" /><!-- 自动加载 --><constant name="struts.convention.classes.reload" value="true"></constant><!-- 扫描的包 --><constant name="struts.convention.package.locators" value="struts2,action" /><!-- :去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割 --><constant name="struts.convention.action.name.separator" value="-" /><constant name="struts.convention.result.path" value="/WEB-INF" /></struts>
4、请求页面 a) /WEB-INF/index.jsp
<form action="user/test"><input type="submit" value="test"/></form>
5、action a) src/com/action/user/TestAcion.javapackage com.action.user;import com.opensymphony.xwork2.Action;public class TestAction implements Action{<span> </span>@Override<span> </span>public String execute() throws Exception {<span> </span>System.out.println("convertion test result is true");<span> </span>return SUCCESS;<span> </span>}}6、结果页面 a) /WEB-INF/user/test.jsp;b) /WEB-INF/user/test-error.jsp;注:用的版本是2.3.24,虽然上面配置去掉“-”,但如果页面是“testerror.jsp”,会把中间的一个”e”冲掉,变成”testrror.jsp”。7、查看配置信息a) 地址栏输入:localhost:....../项目/config-browser/acionName.action;可以查看到映射等配置的结果。如果没有结果,那就是映射失败啰。

附图:

maven配置:<dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.24</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-convention-plugin</artifactId><version>2.3.24</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-config-browser-plugin</artifactId><version>2.3.24</version></dependency>结构如下:配置结果如下:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息