張昊

          J-Hi(http://www.j-hi.net)

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            45 Posts :: 1 Stories :: 110 Comments :: 0 Trackbacks
          實現方式
          1、在struts.xml或xwork.xml加如下配置信息
                  <global-results>
                      
                      
          <result name="auto">/${proxy.config.packageName}/${proxy.method}.jsp</result>           
                  
          </global-results>

          2、在BaseAction類中加入proxy的方法實現
                private ActionProxy proxy;
             
              public ActionProxy getProxy(){
                  if(proxy  == null)
                      proxy = ActionContext.getContext().getActionInvocation().getProxy();
                  return proxy;
              }

          3、做一個JSP文件,文件名一定要與action的方法名相同,列如:a.jap那么action的方法的寫法
              public String a() throws Exception{
                  
                  
          return AUTO;
              }

          4、在某個Jsp頁面中調于這個無配置actoin的寫法
              actionName!a.action

          分析
             ActionProxy類是struts2或webwork提供的一個action代理類,它的作用是它的作用是記錄當前這個action的對象、action的名稱、配置信息及該action所屬的包名等信息。該接口的聲明如下
          public interface ActionProxy {

              
          /**
               * 
          @return the Action instance for this Proxy
               
          */
              Object getAction();

              
          /**
               * 
          @return the alias name this ActionProxy is mapped to
               
          */
              String getActionName();

              
          /**
               * 
          @return the ActionConfig this ActionProxy is built from
               
          */
              ActionConfig getConfig();

              
          /**
               * Sets whether this ActionProxy should also execute the Result after executing the Action
               *
               * 
          @param executeResult
               
          */
              
          void setExecuteResult(boolean executeResult);

              
          /**
               * 
          @return the status of whether the ActionProxy is set to execute the Result after the Action is executed
               
          */
              
          boolean getExecuteResult();

              
          /**
               * 
          @return the ActionInvocation associated with this ActionProxy
               
          */
              ActionInvocation getInvocation();

              
          /**
               * 
          @return the namespace the ActionConfig for this ActionProxy is mapped to
               
          */
              String getNamespace();

              
          /**
               * Execute this ActionProxy. This will set the ActionContext from the ActionInvocation into the ActionContext
               * ThreadLocal before invoking the ActionInvocation, then set the old ActionContext back into the ThreadLocal.
               *
               * 
          @return the result code returned from executing the ActionInvocation
               * 
          @throws Exception
               * 
          @see ActionInvocation
               
          */
              String execute() 
          throws Exception;

              
          /**
               * Sets the method to execute for the action invocation. If no method is specified, the method provided by
               * in the action's configuration will be used.
               *
               * 
          @param method the string name of the method to invoke
               
          */
              
          void setMethod(String method);

              
          /**
               * Returns the method to execute, or null if no method has been specified (meaning "execute" will be invoked)
               
          */
              String getMethod();
          }
           
             J-Hi借用了這個代理類,在action的基類也就是BaseAction中添加了對該類實例的引用,從而實體全局配置
                 <result name="auto">/${proxy.config.packageName}/${proxy.method}.jsp</result> 
             其中${proxy.config.packageName}用來指定當前action所屬的包名,例如,"testjs"就是配置文件的包名
          <xwork>
              
          <package name="testjs" extends="hi" >
                
          <action name="materialList"
                      class
          ="org.hi.testjs.action.webwork.MaterialListAction">
                      
          <result name="success">/testjs/MaterialList.jsp</result>
                      
          <interceptor-ref name="modelParamsStack" />
                  
          </action>
          .
          </xwork>
            ${proxy.method}是指調用該action的方法名
            name="auto" 是我們特意為這樣無配置的actoin起了一個特定的名字,也就是說 
                

              public String a() throws Exception{
                 
          return "auto";     
                  或
                  
          return AUTO;
              }
            效果是一樣的
           
            我們特意將這段result的配置放在了
          <global-results>中原因是省去寫配置文件,只要是return "auto";就會調用這個結果。那么它的結果是什么呢?對,是一個JSP,也就是說你通過actionName!method.action后,系統會自動執行這個方法,并自動調用這個aciton所屬包名下的與方法名相同的jsp文件。例如配置文件的包名為"testjs",actionName為"materialList",對應的class為"org.hi.testjs.action.webwork.MaterialListAction",你在這個action類中增加了一個a(),想通過調用該方法實現無配置調用jsp,那么你就應該將這個jsp文件放到web/testjs(與包名相同)目錄下,并且該jsp的文件名為a.jsp(與方法名相同)。調用這個action方法的寫法如下:materialList!a.action。OK,大工告成!!

          技巧
             為了適應不同人對action的開發習慣,J-Hi對struts2與webwork的生成方式是不同的。struts2是所有的操作都放在一個Action類中通過方法調用,而webwork是每個一操作一個Action類。兩種方式均有優勢也優有不足之處,大家在使用時全憑自己的習慣就好。我們之所以實現無配置,主要是考慮到J-Hi它不只是一個開發管理系統的平臺,也應該可以做網站或電子商務前端的開發。我們知道對于后臺管理系統主要考慮的是系統安全性(頁面的布局與樣式風格要統一),而網站或電子商務前端恰好相反,它追求的是安全不是問題因為它歡迎更多的瀏覽者不需要對每個操作都做權限控制(頁面的風格也五花八門,炫、酷不規則是這類系統的特點)。因此提供了無配置文件的方式,以滿足這類需求(當然純頁面還是要由美工來完成,無規則平臺的生成器是無法勝任該工作的)。由此而帶來的另一個問題是,平臺已經生成了很多aciton的功能,如何讓前臺與后臺共用這些已生成的action類呢?下面我們以struts2為例
             在BaseAction中有一個protected String returnCommand()方法,該方法是確定返回的結果的名字
              protected String returnCommand(String message){
                  String viewMode 
          = HiConfigHolder.getViewMode();
                  
                  
          if(viewMode.equals("dwz")){                   
                      
          if ((ajax == null || !ajax.trim().equals("1")) && message == null)
                          
          return SUCCESS;
                      
          if(message == null)
                          
          return ajaxForwardSuccess(I18NUtil.getString("操作成功"));  //如果是dwz版就返回一個json對象的字符串
                      
          else
                          
          return ajaxForwardError(message);
                  }
                  
                  
          return SUCCESS;  //如果是經典版就返回success字符串
              }
            如果你想在前臺調用平臺已生成的action,而跳過權限控制,就可以通過無配置文件這種方式來實現,解決方案為,你在要做無配置的action類中覆蓋BaseAction的retunCommand()方法,覆蓋的實現方法如下:
              protected String returnCommand() {
                  
                  
          if(this.getRequest().getRequestURI().indexOf("!">0)   //如果在URL中包含!就說明是無配置的,它就會返回auto
                      
          return "auto";
                  
                  
          return super.returnCommand();              //否則就走BaseAction也就是父類的retunCommand()方法
                  }

            例如struts的action配置文件如下
          <struts>
              
          <package name="testjs" extends="hi" >
                
          <action name="material"
                      
          class="org.hi.testjs.action.struts.MaterialAction">
                      
          <interceptor-ref name="modelParamsStack" />
                  
          </action>
          .
          </struts>
            平臺生成的MaterialAction類會有一個materialList(),你想在前臺調用而忽略權限,就可以寫成material!materialList.action,就可以了


          posted on 2011-04-28 23:03 張昊 閱讀(1928) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 英山县| 同江市| 商河县| 于田县| 兴宁市| 绥阳县| 迁安市| 无锡市| 海阳市| 罗江县| 文山县| 温泉县| 河西区| 民乐县| 西青区| 抚顺市| 汤原县| 尉犁县| 和田市| 绥江县| 洪雅县| 乐清市| 延长县| 隆昌县| 安达市| 奇台县| 永兴县| 孝义市| 海原县| 牡丹江市| 旌德县| 楚雄市| 历史| 海门市| 清新县| 桑日县| 武山县| 盘锦市| 洮南市| 青田县| 正蓝旗|