1. 程式人生 > >struts2 Convention插件好處及使用

struts2 Convention插件好處及使用

tom ring 文件 rec annotate ted put ida 分隔符

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

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)

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 如

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

struts2 Convention插件好處及使用