struts2 Convention插件好处及使用
现在JAVA开发都流行SSH.而很大部分公司也使用了struts2进行开发..因为struts2提供了很多插件和标签方便使用..在之前开发过程中总发现使用了struts2会出现很多相应的配合文件.如果对配置文件的管理感觉比较麻烦..可以考虑使用COnvention插件可以进行零配置而且插件进行很多规范的约定也可以对开发合作当中按着它相应的规律开发..感觉也挺方便管理的.下面简单介绍它的使用.
首先我们需要使用到的jar包:
- struts2-convention-plugin-2.1.8.jar
- struts2-core-2.1.8.jar
- xwork-core-2.1.6.jar
- commons-fileupload-1.2.1.jar
- freemarker2.3.16.jar
web.xml的配置
- <!-- Struts2过滤器 -->
- <filter>
- <filter-name>struts</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
struts.xml的配置
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!--++++++++++++++++++++++++++++++++++++++++++++++++开发状态 -->
- <!-- 是否显示详细错误信息 -->
- <constant name="struts.devMode" value="true" />
- <!-- 国际化资源文件名称 -->
- <constant name="struts.custom.i18n.resources" value="i18n" />
- <!-- 是否自动加载国际化资源文件 -->
- <constant name="struts.i18n.reload" value="false" />
- <!-- convention类重新加载 -->
- <constant name="struts.convention.classes.reload" value="true" />
- <!--++++++++++++++++++++++++++++++++++++++++++++++++开发状态 -->
- <!-- 浏览器是否缓存静态内容 -->
- <constant name="struts.serve.static.browserCache" value="true" />
- <!-- 上传文件大小限制设置 -->
- <constant name="struts.multipart.maxSize" value="-1" />
- <!-- 主题 -->
- <constant name="struts.ui.theme" value="simple" />
- <!-- 编码 -->
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <!-- 后缀 -->
- <constant name="struts.action.extension" value="action" />
- <!-- 结果资源的路径 -->
- <constant name="struts.convention.result.path" value="/WEB-INF/template/" />
- <!-- URL资源分隔符 -->
- <constant name="struts.convention.action.name.separator" value="_" />
- <package name="basePackage" extends="struts-default">
- <interceptors>
- <interceptor-stack name="baseStack">
- <interceptor-ref name="exception" />
- <interceptor-ref name="alias" />
- <interceptor-ref name="servletConfig" />
- <interceptor-ref name="i18n" />
- <interceptor-ref name="prepare" />
- <interceptor-ref name="chain" />
- <interceptor-ref name="debugging" />
- <interceptor-ref name="scopedModelDriven" />
- <interceptor-ref name="modelDriven" />
- <interceptor-ref name="fileUpload" />
- <interceptor-ref name="checkbox" />
- <interceptor-ref name="multiselect" />
- <interceptor-ref name="staticParams" />
- <interceptor-ref name="actionMappingParams" />
- <interceptor-ref name="params">
- <param name="excludeParams">dojo\..*,^struts\..*</param>
- </interceptor-ref>
- <interceptor-ref name="conversionError"/>
- <!-- 配置方法级别的校验 -->
- <interceptor-ref name="validation">
- <param name="excludeMethods">input,back,cancel,browse</param>
- <param name="validateAnnotatedMethodOnly">true</param>
- </interceptor-ref>
- <interceptor-ref name="workflow">
- <param name="excludeMethods">input,back,cancel,browse</param>
- </interceptor-ref>
- </interceptor-stack>
- </interceptors>
- <!-- 配置默认拦截器栈 -->
- <default-interceptor-ref name="baseStack" />
- <!-- 未到找Action指向页面 -->
- <default-action-ref name="errorPage" />
- <action name="errorPage">
- <result type="redirect">/html/error_page_404.html</result>
- </action>
- </package>
- <package name="shop" extends="basePackage" namespace="/shop/">
- <global-results>
- <result name="error" type="freemarker">/WEB-INF/template/shop/error.ftl</result>
- </global-results>
- </package>
- </struts>
action的代码
我在Myeclipse新建的web项目中建立action.shop.HelloAction
- public class HelloAction extends ActionSupport {
- public String execute() {
- return "test";
- }
- public String mysql() {
- return "test";
- }
- }
然后你在WEB-INF/template新建文件夹shop(这里新建的文件夹名字要和你建立的Action对应的存放文件夹名字符合)
然后建立文件一个jsp或者tld或者jsf文件名字为hello_test使用http://localhost:8080/web/shop/hello.action时将会直接跳到该页面进行显示
加了方法的名的话访问就使用http://localhost:8080/web/shop/hello!mysql.action
下面详细解析下struts.xml中Convention配置
1.1. 设置结果页面路径
默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如:
- <constant name="struts.convention.result.path" value="="/WEB-INF/template/" />
这样你在就必须将你需要跳转的页面放在template下面
struts2支持.jsp .html .htm .vm格式的文件。
下面是actiong和结果模版的映射关系:
URL Result File that could match Result Type
/hello success /WEB-INF/content/hello.jsp Dispatcher
/hello success /WEB-INF/content/hello-success.htm Dispatcher
/hello success /WEB-INF/content/hello.ftl FreeMarker
/hello input /WEB-INF/content/hello-world-input.vm Velocity
/hello error /WEB-INF/content/test/test2/hello-error.html Dispatcher
以上的内容来自struts2的文档
[url]http://struts.apache.org/2.1.6/docs/convention-plugin.html [/url]
当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。
1.2. 设置Convention搜索包
默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如:
- <constant name="struts.convention.package.locators" value="web,action" />
则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。
Com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。
接着,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:
com.example.actions.MainAction
com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
com.example.struts.company.details.ShowCompanyDetailsAction
1.3. 命名空间
命名空间。从定义的.package.locators标示开始到包结束的部分,就是命名空间。举个例子:
xxx.xxx.action.shop.helloAction的命名空间是:”/shop”。
xxx.xxx.action.shop.detail.UserAction的命名空间是:”/shop/detail”
1.4. Actin类名路径分割
Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割,你可以设置struts.convention.action.name.separator 如
- <contant name="struts.convention.action.name.separator" value="_" />
还是举个例子:
HelloAction->hello HelloWorldAction ->hello-world。
结合上面的。
对于action.shop.HelloAction,映射的url就是/WEB-INF/template/shop/hello_test.jsp
struts2 Convention插件好处及使用的更多相关文章
- Struts2 Convention插件的使用(4)使用@Action注解返回json数据
package com.hyy.action; import java.util.HashMap; import java.util.Map; import org.apache.struts2.co ...
- Struts2 Convention插件的使用(1)
刚刚查阅官方文档(convention-plugin.html)并学习了Struts2的Convention插件,文章这里只作为一个笔记,建议大家去看官方文档比较清晰和全面. 需要在项目添加这些包 c ...
- struts2 Convention插件零配置,使用注解开发
从struts21开始,struts2不再推荐使用codebehind作为零配置插件,而是改用Convention插件来支持零配置.与以前相比较,Convention插件更彻底. 使用Conventi ...
- struts2 convention插件
1.struts2自2.1以后推荐使用Convention Plugin支持struts零配置支持(引入jar:struts2-convention-plugin-2.x.x.jar)①convent ...
- Struts2 Convention插件的使用(3)方法前的@Action注解
package com.hyy.action; import org.apache.struts2.convention.annotation.Action; import com.opensymph ...
- Struts2 convention插件试用+ Spring+Hibernate SSH整合
第一步,引入struts2-convention-plugin-2.2.1.jar 然后,改动配置文件. 我是在struts.properties文件里改动的: struts.objectFactor ...
- Struts2 Convention插件的使用
转自:http://chenjumin.iteye.com/blog/668389 1.常量说明 struts.convention.result.path="/WEB-INF/conten ...
- Struts2 Convention插件的使用(2)return视图以及jsp的关系
package com.hyy.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extend ...
- struts2基于Convention插件的约定映射使用
一.首先说明一点:所谓的基于Convention插件的约定优于配置的使用,并不是严格意义上的零配置,struts.xml文件并不能完全舍弃. 获得Convention插件功能,所必需的jar包有:|a ...
随机推荐
- ModuleNotFoundError: No module named '_sqlite3'
ModuleNotFoundError: No module named '_sqlite3' 解决: 1,首先安装 sqlite-devel yum install sqlite-devel 2,重 ...
- VisualStudio2012轻松把JSON数据转换到POCO的代码(转)
VisualStudio2012轻松把JSON数据转换到POCO的代码 在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Essentials 20 ...
- Java里的堆(heap)栈(stack)和方法区(method)
基础数据类型直接在栈空间分配, 方法的形式参数,直接在栈空间分配,当方法调用完成后从栈空间回收. 引用数据类型,需要用new来创建,既在栈空间分配一个地址空间,又在堆空间分配对象的类变量 . 方法 ...
- c# 数据拼接成键值对格式
public static object FindLayoutTypes() { //地鼓.地裂.墙裂.井水.泉水 var sb = new StringBuilder(); sb.Append(ge ...
- nginx 配置ajax跨域访问php接口
在nginx.conf里面,找到server项,并在里面添加如下配置 location ~ \.php?($|/) { #try_files $uri =; #handel cosr by mao a ...
- c# class struct区别
一句话,前者引用类型,后者值类型,适合高性能的情况,但不可存储大数据.
- ColorMask
[ColorMask] When using multiple render target (MRT) rendering, it is possible to set up different co ...
- unity3d开发实战《啪啪三国》技术详解!
去年11月,上海火溶网络CEO王伟峰以其第一款3d手游产品<啪啪三国>为例,着重讲解了unity3D手机网游开发的经验,其中涉及了团队组成.人员要求.常见的unity3d开发遇到的坑及解 ...
- 从值栈获取List集合
-------------------siwuxie095 从值栈获取 List 集合 1.具体步骤 (1)在 Action 中向值栈放 List 集合 (2)在 JSP 页面中从值栈获取 List ...
- Ckeditor 中粘贴图片
我们在ckeditor 中有上传图片,但是实际使用中这种手动上传图片方式并不是很方便,而是复制或者截图粘贴图片. 这里我们实现主要是获取对应的粘贴事件. CKEDITOR.instances[&quo ...