现在JAVA开发都流行SSH.而很大部分公司也使用了struts2进行开发..因为struts2提供了很多插件和标签方便使用..在之前开发过程中总发现使用了struts2会出现很多相应的配合文件.如果对配置文件的管理感觉比较麻烦..可以考虑使用COnvention插件可以进行零配置而且插件进行很多规范的约定也可以对开发合作当中按着它相应的规律开发..感觉也挺方便管理的.下面简单介绍它的使用. 
首先我们需要使用到的jar包:

  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的配置

  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. <filter-mapping>
  7. <filter-name>struts</filter-name>
  8. <url-pattern>*.action</url-pattern>
  9. </filter-mapping>

struts.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. <constant name="struts.multipart.maxSize" value="-1" />
  18. <!-- 主题 -->
  19. <constant name="struts.ui.theme" value="simple" />
  20. <!-- 编码 -->
  21. <constant name="struts.i18n.encoding" value="UTF-8" />
  22. <!-- 后缀 -->
  23. <constant name="struts.action.extension" value="action" />
  24. <!-- 结果资源的路径 -->
  25. <constant name="struts.convention.result.path" value="/WEB-INF/template/" />
  26. <!-- URL资源分隔符 -->
  27. <constant name="struts.convention.action.name.separator" value="_" />
  28. <package name="basePackage" extends="struts-default">
  29. <interceptors>
  30. <interceptor-stack name="baseStack">
  31. <interceptor-ref name="exception" />
  32. <interceptor-ref name="alias" />
  33. <interceptor-ref name="servletConfig" />
  34. <interceptor-ref name="i18n" />
  35. <interceptor-ref name="prepare" />
  36. <interceptor-ref name="chain" />
  37. <interceptor-ref name="debugging" />
  38. <interceptor-ref name="scopedModelDriven" />
  39. <interceptor-ref name="modelDriven" />
  40. <interceptor-ref name="fileUpload" />
  41. <interceptor-ref name="checkbox" />
  42. <interceptor-ref name="multiselect" />
  43. <interceptor-ref name="staticParams" />
  44. <interceptor-ref name="actionMappingParams" />
  45. <interceptor-ref name="params">
  46. <param name="excludeParams">dojo\..*,^struts\..*</param>
  47. </interceptor-ref>
  48. <interceptor-ref name="conversionError"/>
  49. <!-- 配置方法级别的校验 -->
  50. <interceptor-ref name="validation">
  51. <param name="excludeMethods">input,back,cancel,browse</param>
  52. <param name="validateAnnotatedMethodOnly">true</param>
  53. </interceptor-ref>
  54. <interceptor-ref name="workflow">
  55. <param name="excludeMethods">input,back,cancel,browse</param>
  56. </interceptor-ref>
  57. </interceptor-stack>
  58. </interceptors>
  59. <!-- 配置默认拦截器栈 -->
  60. <default-interceptor-ref name="baseStack" />
  61. <!-- 未到找Action指向页面 -->
  62. <default-action-ref name="errorPage" />
  63. <action name="errorPage">
  64. <result type="redirect">/html/error_page_404.html</result>
  65. </action>
  66. </package>
  67. <package name="shop" extends="basePackage" namespace="/shop/">
  68. <global-results>
  69. <result name="error" type="freemarker">/WEB-INF/template/shop/error.ftl</result>
  70. </global-results>
  71. </package>
  72. </struts>

action的代码 
我在Myeclipse新建的web项目中建立action.shop.HelloAction

  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这个属性的值来改变到其他路径。如:

  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属性来修改这个配置。如:

  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)

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 如

  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

struts2 Convention插件好处及使用的更多相关文章

  1. Struts2 Convention插件的使用(4)使用@Action注解返回json数据

    package com.hyy.action; import java.util.HashMap; import java.util.Map; import org.apache.struts2.co ...

  2. Struts2 Convention插件的使用(1)

    刚刚查阅官方文档(convention-plugin.html)并学习了Struts2的Convention插件,文章这里只作为一个笔记,建议大家去看官方文档比较清晰和全面. 需要在项目添加这些包 c ...

  3. struts2 Convention插件零配置,使用注解开发

    从struts21开始,struts2不再推荐使用codebehind作为零配置插件,而是改用Convention插件来支持零配置.与以前相比较,Convention插件更彻底. 使用Conventi ...

  4. struts2 convention插件

    1.struts2自2.1以后推荐使用Convention Plugin支持struts零配置支持(引入jar:struts2-convention-plugin-2.x.x.jar)①convent ...

  5. Struts2 Convention插件的使用(3)方法前的@Action注解

    package com.hyy.action; import org.apache.struts2.convention.annotation.Action; import com.opensymph ...

  6. Struts2 convention插件试用+ Spring+Hibernate SSH整合

    第一步,引入struts2-convention-plugin-2.2.1.jar 然后,改动配置文件. 我是在struts.properties文件里改动的: struts.objectFactor ...

  7. Struts2 Convention插件的使用

    转自:http://chenjumin.iteye.com/blog/668389 1.常量说明 struts.convention.result.path="/WEB-INF/conten ...

  8. Struts2 Convention插件的使用(2)return视图以及jsp的关系

    package com.hyy.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extend ...

  9. struts2基于Convention插件的约定映射使用

    一.首先说明一点:所谓的基于Convention插件的约定优于配置的使用,并不是严格意义上的零配置,struts.xml文件并不能完全舍弃. 获得Convention插件功能,所必需的jar包有:|a ...

随机推荐

  1. Centos 7.2基础安装和配置(含分区方案建议)

    景:windows桌面运维为主的我,前几天接到一个去某客户上架安装服务器的工作任务,含糊的说要上架几台服务器顺便安装Centos系统,于是我便下载了一个Centos7.2版本的镜像,顺利的用USBwr ...

  2. AWS安装

    curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" sudo python get-pip. ...

  3. Windows10 IME占用过高临时解决办法

    解决方案: 在管理员模式下运行命令提示符,并输入如下命令 if exist "%SystemRoot%\System32\InputMethod\CHS\ChsIME.exe" ( ...

  4. xdebug php

    sudo apt-get install php5-dev php5-cli #其中php5-dev为了安装xdebug所以必须安装. sudo apt-get install php5-xsl #X ...

  5. cluster DNS

    [root@mhc1 dns]# pwd/root/test/k8s/kubernetes/cluster/addons/dns [root@mhc1 dns]# export DNS_SERVER_ ...

  6. The Process of Google Hiring

    [The Process of Google Hiring] 1.keynote 1: The Google hiring process is designed to hire the most t ...

  7. gd库已打开,验证码不显示

    ob_start(); ob_clean();

  8. 12个优秀的国外Material Design网站案例

    眼看2017年就快完了,你是不是还没完全搞懂Material Design呢?是嫌说明文档太长,还是觉得自己英文不好?都没关系,小编今天给大家整理了一份干货满满的学习笔记,并列举了一些国外的Mater ...

  9. 解决win7和2008连接windows 2003远程桌面很卡的问题

    解决win7和2008连接windows 2003远程桌面很卡的问题 来源:http://www.hack1990.com/ 作者:佚名 时间:2013-04-12 TAG: 我要投稿 原因在于从vi ...

  10. 【译】微型ORM:PetaPoco【不完整的翻译】(转)

    出处:http://www.cnblogs.com/youring2/archive/2012/06/04/2532130.html PetaPoco是一款适用于.Net 和Mono的微小.快速.单文 ...