收藏 分销(赏)

Struts2的Convention插件的好处与使用.docx

上传人:pc****0 文档编号:8727626 上传时间:2025-02-27 格式:DOCX 页数:6 大小:37.61KB 下载积分:10 金币
下载 相关 举报
Struts2的Convention插件的好处与使用.docx_第1页
第1页 / 共6页
Struts2的Convention插件的好处与使用.docx_第2页
第2页 / 共6页


点击查看更多>>
资源描述
  Struts2的Convention插件的好处与使用 分类: struts2013-09-01 14:18 118人阅读 评论(0) 收藏 举报 Struts   现在JAVA开发都流行SSH.而很大部分公司也使用了struts2进行开发..因为struts2提供了很多插件和标签方便使用..在之前开发过程中总发现使用了struts2会出现很多相应的配合文件.如果对配置文件的管理感觉比较麻烦..可以考虑使用COnvention插件可以进行零配置而且插件进行很多规范的约定也可以对开发合作当中按着它相应的规律开发..感觉也挺方便管理的.下面简单介绍它的使用.  首先我们需要使用到的jar包:  Java代码   1. struts2-convention-plugin-2.1.8.jar   2. struts2-core-2.1.8.jar   3. xwork-core-2.1.6.jar   4. commons-fileupload-1.2.1.jar   5. freemarker2.3.16.jar   web.xml的配置  Xml代码   1.  <!-- Struts2过滤器 -->   2.     <filter>   3.         <filter-name>struts</filter-name>   4.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>   5.     </filter>   6.        7.     <filter-mapping>   8.         <filter-name>struts</filter-name>   9.         <url-pattern>*.action</url-pattern>   10. </filter-mapping>   struts.xml的配置  Xml代码   1. <?xml version="1.0" encoding="UTF-8"?>   2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">   3. <struts>   4.  <!--++++++++++++++++++++++++++++++++++++++++++++++++开发状态 -->    5.     <!-- 是否显示详细错误信息 -->   6.     <constant name="struts.devMode" value="true" />   7.     <!-- 国际化资源文件名称 -->   8.     <constant name="struts.custom.i18n.resources" value="i18n" />   9.     <!-- 是否自动加载国际化资源文件  -->   10.     <constant name="struts.i18n.reload" value="false" />   11.     <!-- convention类重新加载 -->   12.     <constant name="struts.convention.classes.reload" value="true" />   13.  <!--++++++++++++++++++++++++++++++++++++++++++++++++开发状态 -->    14.     <!-- 浏览器是否缓存静态内容 -->   15.     <constant name="struts.serve.static.browserCache" value="true" />   16.    17.     <!-- 上传文件大小限制设置 -->   18.     <constant name="struts.multipart.maxSize" value="-1" />   19.    20.     <!-- 主题 -->   21.     <constant name="struts.ui.theme" value="simple" />   22.     <!-- 编码 -->   23.     <constant name="struts.i18n.encoding" value="UTF-8" />   24.     <!-- 后缀 -->   25.     <constant name="struts.action.extension" value="action" />   26.    27.     <!-- 结果资源的路径 -->   28.     <constant name="struts.convention.result.path" value="/WEB-INF/template/" />   29.     <!-- URL资源分隔符 -->   30.     <constant name="struts.convention.action.name.separator" value="_" />   31.        32.     <package name="basePackage" extends="struts-default">   33.         <interceptors>   34.             <interceptor-stack name="baseStack">   35.                 <interceptor-ref name="exception" />   36.                 <interceptor-ref name="alias" />   37.                 <interceptor-ref name="servletConfig" />   38.                 <interceptor-ref name="i18n" />   39.                 <interceptor-ref name="prepare" />   40.                 <interceptor-ref name="chain" />   41.                 <interceptor-ref name="debugging" />   42.                 <interceptor-ref name="scopedModelDriven" />   43.                 <interceptor-ref name="modelDriven" />   44.                 <interceptor-ref name="fileUpload" />   45.                 <interceptor-ref name="checkbox" />   46.                 <interceptor-ref name="multiselect" />   47.                 <interceptor-ref name="staticParams" />   48.                 <interceptor-ref name="actionMappingParams" />   49.                 <interceptor-ref name="params">   50.                     <param name="excludeParams">dojo\..*,^struts\..*</param>   51.                 </interceptor-ref>   52.                 <interceptor-ref name="conversionError"/>   53.                 <!-- 配置方法级别的校验 -->   54.                 <interceptor-ref name="validation">   55.                     <param name="excludeMethods">input,back,cancel,browse</param>   56.                     <param name="validateAnnotatedMethodOnly">true</param>   57.                 </interceptor-ref>   58.                 <interceptor-ref name="workflow">   59.                     <param name="excludeMethods">input,back,cancel,browse</param>   60.                 </interceptor-ref>   61.             </interceptor-stack>   62.         </interceptors>   63.    64.         <!-- 配置默认拦截器栈 -->   65.         <default-interceptor-ref name="baseStack" />   66.    67.         <!-- 未到找Action指向页面 -->   68.         <default-action-ref name="errorPage" />   69.    70.         <action name="errorPage">   71.             <result type="redirect">/html/error_page_404.html</result>   72.         </action>   73.    74.     </package>   75.    76.    77.    78.     <package name="shop" extends="basePackage" namespace="/shop/">   79.    80.         <global-results>   81.             <result name="error" type="freemarker">/WEB-INF/template/shop/error.ftl</result>   82.         </global-results>   83.    84.     </package>   85.        86. </struts>   action的代码  我在Myeclipse新建的web项目中建立action.shop.HelloAction  Java代码   1. public class HelloAction  extends ActionSupport {   2.     public String execute() {    3.         return "test";    4.     }    5. public String mysql() {   6.         return "test";    7.     }   8. }   然后你在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这个属性的值来改变到其他路径。如:  Xml代码   1. <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属性来修改这个配置。如:  Xml代码   1. <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)  pany.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 如  Xml代码   1. <contant name="struts.convention.action.name.separator" value="_" />   还是举个例子:  HelloAction->hello HelloWorldAction ->hello-world。  结合上面的。  对于action.shop.HelloAction,映射的url就是/WEB-INF/template/shop/hello_test.jsp  以上是它简单而常使用的方式和配置 
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服