JAVA—咖啡館

          ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術,交流工作經驗,分享JAVA帶來的快樂!本網站部分轉載文章,如果有版權問題請與我聯系。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

          公告

           

          Locations of visitors to this page
          點擊這里給我發消息 點擊這里給我發消息

          常用鏈接

          留言簿(17)

          隨筆分類(542)

          隨筆檔案(438)

          文章分類(182)

          文章檔案(142)

          新聞分類

          ※→ 【JAVA文檔】

          ※→ 【親人博客】

          ※→ 【休閑娛樂】

          ※→ 【友情鏈接】

          ※→ 【學習網站】

          ※→ 【服務網站】

          ※→ 【著名網站】

          ※→ 【阿里博客】

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          1.1. 設置結果頁面路徑
          默認所有的結果頁面都存儲在WEB-INF/content下,你可以通過設置struts.convention.result.path這個屬性的值來改變到其他路徑。如:

          Xml代碼:

          <constant name="struts.convention.result.path" value="/WEB-INF/page" />
           

           

          則將路徑配置到了WEB-INF/page 下。

          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標示開始到包結束的部分,就是命名空間。舉個例子:

          Com.ustb.web.user.userAction的命名空間是:”/user”。

          Com.ustb.web.user.detail.UserAction的命名空間是:”/user/detail”
           

           

          1.4. Actin類名路徑分割
          Convention通過如下規則確定URL的具體資源部分:去掉類名的Action部分。然后將將每個分部的首字母轉為小寫,用’-’分割,你可以設置struts.convention.action.name.separator 如

          <constant name="struts.convention.action.name.separator" value="-" />

          還是舉個例子:

          UserAction->user  UserDetailAction ->user-detail。

          結合上面的。

          對于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp

          1.5. 支持jsp、html、htm、vm等格式
          struts支持.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的文檔http://struts.apache.org/2.1.6/docs/convention-plugin.html

          當然,簡單的通過默認的方式來進行配置不能完全滿足實際項目的需要。所幸,convention的零配置是非常靈活的。

           

          1.6. @Action注解
           

          通過@Action注釋

          對如下例子:

          Java代碼

          package com.example.web;

           

          import com.opensymphony.xwork2.Action;

          import com.opensymphony.xwork2.ActionSupport;

           

          public class HelloAction extends ActionSupport {

              @Action("action1")

              public String method1() {

                  return SUCCESS;

              }

           

              @Action("/user/action2")

              public String method2() {

                  return SUCCESS;

          }

          }

          方法名    默認調用路徑       默認映射路徑

          method1  /hello!method1.action .   /WEB-INF/content/hello.jsp

          method2  /hello!method2.action.    /WEB-INF/content/hello.jsp

          通過@Action注釋后

          方法名    @Action注釋后調用路徑    @Action注釋 后映射路徑

          method1  /action1!method1.action.       /WEB-INF/content/action1.jsp

          method1  /user/action2!method2.action /WEB-INF/content/user/action2.jsp
           

           

          1.7. @Actions注解
          通過@Actions注釋,例子:

          Java代碼

          package com.example.web;

           

          import com.opensymphony.xwork2.ActionSupport;

          import org.apache.struts2.convention.annotation.Action;

          import org.apache.struts2.convention.annotation.Actions;

           

          public class HelloAction extends ActionSupport {

            @Actions({

              @Action("/different/url"),

              @Action("/another/url")

            })

            public String method1() {

              return “error”;

            }
           

           

          我們可以通過:/different/url!method1.action 或 /another/url!method1.action 來調用method1 方法。

          對應的映射路徑分別是

          /WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp

          可能誤導了大家,一個方法被@Action注釋后,只是多了一種調用方式,而不是說覆蓋了原來的調用方式。比如對于如下例子:

          Java代碼

          com.example.web;

           

          import com.opensymphony.xwork2.ActionSupport;

          import org.apache.convention.annotation.Action;

          import org.apache.convention.annotation.Actions;

           

          public class HelloAction extends ActionSupport {

            @Action("/another/url")

            public String method1() {

              return “error”;

            }

           
           

           

          我們調用method1方法可以通過兩種方式:

          1  /hello!method1.action 映射 url:/WEB-INF/content/hello-error.jsp

          2 /another/url!method1.action 映射 url:/WEB-INF/content/another/url-error.jsp

          可見,兩種方式均可對method1方法進行調用,唯一的區別就是,兩種調用的映射是不一樣的,所以,想跳轉到不同的界面,這是一個非常好的選擇。

          1.8. @Namespace注解
          通過@Namespace 注釋

          package com.example.web;

           

          import com.opensymphony.xwork2.ActionSupport;

          import org.apache.struts2.convention.annotation.Action;

          import org.apache.struts2.convention.annotation.Actions;

          @Namespace("/other")

          public class HelloWorld extends ActionSupport {

           

            public String method1() {

              return “error”;

            }

              @Action("url")

            public String method2() {

          return “error”;

            }

           

              @Action("/different/url")

            public String method3() {

          return “error”;

            }

          }

           
           

           

          通過 /other/hello-world!method1.action 訪問method1 方法。

          通過 /other/url!method2.action 訪問method2 方法

          通過 /different /url!method3.action 訪問method3 方法

          與@Action 注釋不同的是,該注釋覆蓋了默認的namespace(這里是’/’),此時再用hello!method1.action 已經不能訪問method1 了.

          1.9. @Results和@Result注解
          @Results和@Result

          1.9.1.全局的(global)。
          全局results可以被action類中所有的action分享,這種results在action類上使用注解進行聲明。

          package com.example.actions;

           

          import com.opensymphony.xwork2.ActionSupport;

          import org.apache.struts2.convention.annotation.Action;

          import org.apache.struts2.convention.annotation.Actions;

          import org.apache.struts2.convention.annotation.Result;

          import org.apache.struts2.convention.annotation.Results;

           

          @Results({

            @Result(name="failure", location="/WEB-INF/fail.jsp")

          })

          public class HelloWorld extends ActionSupport {

            public String method1() {

              return “failure”;

            }

              @Action("/different/url")

            public String method2() {

              return “failure”;

            }

          }
           

           

          當我們訪問 /hello -world !method1.action 時,返回 /WEB-INF/fail.jsp

          當我們訪問 /hello -world !method2.action 時,返回 /WEB-INF/fail.jsp

          當我們訪問 /different/url!method2.action 時,返回 /WEB-INF/fail.jsp

          1.9.2.本地的(local)。
          本地results只能在action方法上進行聲明。

          Java代碼

          package com.example.actions;

           

          import com.opensymphony.xwork2.ActionSupport;

          import org.apache.struts2.convention.annotation.Action;

          import org.apache.struts2.convention.annotation.Actions;

          import org.apache.convention.annotation.Result;

          import org.apache.convention.annotation.Results;

           

          public class HelloWorld extends ActionSupport {

              @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})

            public String method1() {

              return “error”;

            }

          }

           
           

           

          當我們調用 /hello -world !method1.action 時,返回 /WEB-INF/content/hello-error.jsp

          當我們調用 /other/bar!method1.action 時,返回 www.baidu.com

           

          1.10.            @ParentPackage 注解
          ParentPackage注解用來定義具體action類的父XWork包或java包,下面例子演示了在action類上使用本注解:

           

          package com.example.actions;

          import com.opensymphony.xwork2.ActionSupport; 

          import org.apache.struts2.convention.annotation.Action;

          import org.apache.struts2.convention.annotation.ParentPackage;

          @ParentPackage("customXWorkPackage")

          public class HelloWorld extends ActionSupport {

              public String execute() {

                  return SUCCESS;

              }

          }
           

           

          1.11.            異常注解配置
          ExceptionMapping 注解用來影射action拋出的異常。可以參考exception mapping documentation 獲得詳細信息。注解用類級別,在這種情況下,注解會應用到類里面的所有action

          @ExceptionMappings({

          @ExceptionMapping(exception = "java.lang.NullPointerException",

          result = "success", params = {"param1", "val1"})

          })

          public class ExceptionsActionLevelAction {

           

              public String execute() throws Exception {

                  return null;

              }

          }
           

           

          可以在ExceptionMapping注解中使用params 屬性來傳遞具體值給結果渲染頁。ExceptionMapping注解同樣可以在action級別進行設置:

           

          public class ExceptionsMethodLevelAction {

             @Action(value = "exception1", exceptionMappings = {

                         @ExceptionMapping(exception = "java.lang.NullPointerException",

          result = "success", params = {"param1", "val1"})

              })

              public String run1() throws Exception {

                  return null;

              }

          }
           

           

           

          1.12.            自動加載無需啟動服務
          Convention插件可以自動重新加載配置的功能,無需重啟容器,就可以刷新類中包含的action。這自動加載automatic xml 配置文件類似。你必須在struts.xml 中添加以下代碼來啟用本功能:

           

          <constant name="struts.devMode" value="true"/>

          <constant name="struts.convention.classes.reload" value="true" />
           

           

          此功能沒有在所有容器中進行過測試,強力建議不要在生產環境中使用。

          1.13.            掃描Action的Jar包
          默認情況下,Convention 插件不會從jar文件中尋找action。如果想實現這一功能,jar文件必須被struts.convention.action.includeJars 所定義的正則 匹配到。在例子中 myjar1.jar和 myjar2.jar 將被插件檢測到:

          <constant

          name="struts.convention.action.includeJars"

          value=".*/myjar1.*?jar(!/)?,.*/myjar2*?jar(!/)?" />
           

           

          提示:正則表達式只針對jar文件的路徑進行匹配,而不是文件名。jar的URL應該包含jar文件的路徑并以"!/"結尾。

           

          本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/xiaoping8411/archive/2010/06/02/5641575.aspx

          posted on 2010-06-21 10:16 rogerfan 閱讀(657) 評論(0)  編輯  收藏 所屬分類: 【Java知識】
          主站蜘蛛池模板: 怀安县| 乐至县| 桂林市| 射阳县| 赤城县| 孝感市| 高淳县| 德保县| 特克斯县| 莱阳市| 濮阳市| 论坛| 鄂托克旗| 华池县| 临清市| 灵武市| 抚州市| 崇仁县| 白城市| 石渠县| 石门县| 南召县| 金山区| 萝北县| 阿图什市| 德庆县| 望江县| 宁晋县| 石阡县| 宝兴县| 盐城市| 九龙坡区| 城市| 淄博市| 宣汉县| 襄汾县| 苍南县| 宁津县| 大厂| 嘉荫县| 英山县|