張昊

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

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

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

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

          4、在某個(gè)Jsp頁(yè)面中調(diào)于這個(gè)無配置actoin的寫法
              actionName!a.action

          分析
             ActionProxy類是struts2或webwork提供的一個(gè)action代理類,它的作用是它的作用是記錄當(dāng)前這個(gè)action的對(duì)象、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借用了這個(gè)代理類,在action的基類也就是BaseAction中添加了對(duì)該類實(shí)例的引用,從而實(shí)體全局配置
                 <result name="auto">/${proxy.config.packageName}/${proxy.method}.jsp</result> 
             其中${proxy.config.packageName}用來指定當(dāng)前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}是指調(diào)用該action的方法名
            name="auto" 是我們特意為這樣無配置的actoin起了一個(gè)特定的名字,也就是說 
                

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

          技巧
             為了適應(yīng)不同人對(duì)action的開發(fā)習(xí)慣,J-Hi對(duì)struts2與webwork的生成方式是不同的。struts2是所有的操作都放在一個(gè)Action類中通過方法調(diào)用,而webwork是每個(gè)一操作一個(gè)Action類。兩種方式均有優(yōu)勢(shì)也優(yōu)有不足之處,大家在使用時(shí)全憑自己的習(xí)慣就好。我們之所以實(shí)現(xiàn)無配置,主要是考慮到J-Hi它不只是一個(gè)開發(fā)管理系統(tǒng)的平臺(tái),也應(yīng)該可以做網(wǎng)站或電子商務(wù)前端的開發(fā)。我們知道對(duì)于后臺(tái)管理系統(tǒng)主要考慮的是系統(tǒng)安全性(頁(yè)面的布局與樣式風(fēng)格要統(tǒng)一),而網(wǎng)站或電子商務(wù)前端恰好相反,它追求的是安全不是問題因?yàn)樗鼩g迎更多的瀏覽者不需要對(duì)每個(gè)操作都做權(quán)限控制(頁(yè)面的風(fēng)格也五花八門,炫、酷不規(guī)則是這類系統(tǒng)的特點(diǎn))。因此提供了無配置文件的方式,以滿足這類需求(當(dāng)然純頁(yè)面還是要由美工來完成,無規(guī)則平臺(tái)的生成器是無法勝任該工作的)。由此而帶來的另一個(gè)問題是,平臺(tái)已經(jīng)生成了很多aciton的功能,如何讓前臺(tái)與后臺(tái)共用這些已生成的action類呢?下面我們以struts2為例
             在BaseAction中有一個(gè)protected String returnCommand()方法,該方法是確定返回的結(jié)果的名字
              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版就返回一個(gè)json對(duì)象的字符串
                      
          else
                          
          return ajaxForwardError(message);
                  }
                  
                  
          return SUCCESS;  //如果是經(jīng)典版就返回success字符串
              }
            如果你想在前臺(tái)調(diào)用平臺(tái)已生成的action,而跳過權(quán)限控制,就可以通過無配置文件這種方式來實(shí)現(xiàn),解決方案為,你在要做無配置的action類中覆蓋BaseAction的retunCommand()方法,覆蓋的實(shí)現(xiàn)方法如下:
              protected String returnCommand() {
                  
                  
          if(this.getRequest().getRequestURI().indexOf("!">0)   //如果在URL中包含!就說明是無配置的,它就會(huì)返回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>
            平臺(tái)生成的MaterialAction類會(huì)有一個(gè)materialList(),你想在前臺(tái)調(diào)用而忽略權(quán)限,就可以寫成material!materialList.action,就可以了


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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 章丘市| 亳州市| 湘潭县| 桂平市| 黎城县| 吉隆县| 汕头市| 大安市| 恭城| 靖江市| 莱州市| 平潭县| 长汀县| 玉山县| 吉木乃县| 安阳县| 泉州市| 沽源县| 临江市| 繁昌县| 开封市| 宁安市| 克拉玛依市| 鲁山县| 盘锦市| 汉中市| 陈巴尔虎旗| 伊川县| 榆中县| 宝清县| 正定县| 潍坊市| 凤翔县| 乐清市| 孝昌县| 卓尼县| 新竹县| 石城县| 靖西县| 洪雅县| 错那县|