在WebWork中實(shí)現(xiàn)自己的Result Type

          除經(jīng)特別注明外,本站文章版權(quán)歸JScud Develop團(tuán)隊(duì)或其原作者所有.
          轉(zhuǎn)載請(qǐng)注明作者和來源.  scud(飛云小俠)    歡迎訪問 JScud Develop



          我在項(xiàng)目中經(jīng)常用到這樣的功能:例如添加完一個(gè)文章,我通常會(huì)提示用戶"操作完成",然后轉(zhuǎn)向到文章列表頁面或者文章詳情頁面.以前的做法是使用WebWork本身的dispatcher結(jié)果類型,設(shè)置結(jié)果頁面為信息提示頁面,在程序中設(shè)置目標(biāo)頁面和提示信息.這樣雖然也可以完成我的目標(biāo),但是目標(biāo)頁面是在程序里面寫死的,無法放在配置里.

          這幾天研究了一下WebWork的Result Type,發(fā)現(xiàn)寫一個(gè)新的Result Type很容易,于是誕生了我的一個(gè)新的Result Type:

          功能:信息提示并轉(zhuǎn)向到目標(biāo)頁面
          流程:在Action中設(shè)置提示信息(當(dāng)然也可以改到配置文件里,但是配置起來略麻煩,所以還是寫在程序里了),然后使用我的結(jié)果類型msgto,在配置文件里面配置目標(biāo)頁面.


          代碼如下

          1.頁面信息提示類(包含頁面提示信息和目標(biāo)頁面,停留時(shí)間)

           
           
           package com.jscud.www.support.web;
           
           
           /*
            * Created Date  2004-11-23
            *
            */
           
           /**
            * 用于頁面提示的信息類.
            *
            * @author scud
            * 
            */
           public class PageMessage
           {
               /**標(biāo)題*/
               private String msgTitle;
           
               /**內(nèi)容體*/
               private String msgContent;
           
               /**要轉(zhuǎn)向到的網(wǎng)址*/
               private String msgToURL;
           
               /**停留時(shí)間*/
               private int msgToTime = 1 ;
           
              
               public String getMsgContent()
               {
                   return msgContent;
               }
           
               public void setMsgContent(String msgContent)
               {
                   this.msgContent = msgContent;
               }
           
               public String getMsgTitle()
               {
                   return msgTitle;
               }
           
               public void setMsgTitle(String msgTitle)
               {
                   this.msgTitle = msgTitle;
               }
           
               public int getMsgToTime()
               {
                  
                   return msgToTime;
               }
           
               public void setMsgToTime(int msgToTime)
               {
                   this.msgToTime = msgToTime;
               }
           
               public String getMsgToURL()
               {
                   return msgToURL;
               }
           
               public void setMsgToURL(String msgToURL)
               {
                   this.msgToURL = msgToURL;
               }
           }

           
           
           
          2."msgto" Result Type ,借鑒了WebWork本身的幾個(gè)ResultType,部分代碼是copy過來的

           
           package com.jscud.www.support.web;
           
           //import com.jscud.util.LogMan;
           import com.opensymphony.webwork.config.Configuration;
           import com.opensymphony.webwork.dispatcher.ServletDispatcher;
           import com.opensymphony.webwork.dispatcher.WebWorkResultSupport;
           
           import com.opensymphony.webwork.ServletActionContext;
           import com.opensymphony.xwork.ActionContext;
           import com.opensymphony.xwork.ActionInvocation;
           import com.opensymphony.xwork.util.OgnlValueStack;
           
           import javax.servlet.RequestDispatcher;
           import javax.servlet.http.HttpServletRequest;
           import javax.servlet.http.HttpServletResponse;
           
           /*
            * Created on 2005-7-26
            *
            */
           
           /**
            * WebWork Result Type for "msgto".
            *
            * Message Hint to User then Refresh to another Page.
            *
            * @author scud.
            *
            */
           public class ServletMessageToResult extends WebWorkResultSupport
           {
           
               //~ Static fields/initializers /////////////////////////////////////////////
           
               private static final String DefaultMessagePage = "/WEB-INF/public/page/msgto.jsp";
           
               protected String messagePage;
              
               protected boolean prependServletContext = true;
           
           
               //~ Methods ////////////////////////////////////////////////////////////////
           
               public void setMessagePage(String sPagePath)
               {
                   this.messagePage = sPagePath;
               }
           
               public String getMessagePage()
               {
                   if (null == messagePage)
                   {
                       if (Configuration.isSet("jscud.page.messageto"))
                       {
                           this.messagePage = Configuration.getString("jscud.page.messageto");
                       }
                       else
                       {
                           this.messagePage = DefaultMessagePage;
                       }
                   }
           
                   return messagePage;
               }
           
              
               /**
                * Sets whether or not to prepend the servlet context path to the redirected URL.
                *
                * @param prependServletContext <tt>true</tt> to prepend the location with the servlet context path,
                *                              <tt>false</tt> otherwise.
                */
               public void setPrependServletContext(boolean prependServletContext) {
                   this.prependServletContext = prependServletContext;
               }
              
               /**
                * Dispatches to the Message Page. Does its forward via a RequestDispatcher. If the
                * dispatch fails a 404 error will be sent back in the http response.
                *
                * @param finalLocation the location to dispatch to.
                * @param invocation    the execution state of the action
                * @throws Exception if an error occurs. If the dispatch fails the error will go back via the
                *                   HTTP request.
                */
               public void doExecute(String finalLocation, ActionInvocation invocation)
                       throws Exception
               {
                   String messageLocation = getMessagePage();
           
                   HttpServletRequest request = ServletActionContext.getRequest();
                   HttpServletResponse response = ServletActionContext.getResponse();
           
                   RequestDispatcher dispatcher = request.getRequestDispatcher(messageLocation);
           
                   // if the view doesn’t exist, let’s do a 404
                   if (dispatcher == null)
                   {
                       response.sendError(404, "message page ’" + messageLocation + "’ not found");
           
                       return;
                   }
           
                   request.setAttribute("webwork.view_uri", messageLocation);
                   request.setAttribute("webwork.request_uri", request.getRequestURI());
           
                   //set refresh target url
                   if (isPathUrl(finalLocation))
                   {
                       if (!finalLocation.startsWith("/"))
                       {
                           String actionPath = request.getServletPath();
                           String namespace = ServletDispatcher
                                   .getNamespaceFromServletPath(actionPath);
           
                           if ((namespace != null) && (namespace.length() > 0))
                           {
                               finalLocation = namespace + "/" + finalLocation;
                           }
                           else
                           {
                               finalLocation = "/" + finalLocation;
                           }
                       }
           
                       // if the URL’s are relative to the servlet context, append the servlet context path
                       if (prependServletContext && (request.getContextPath() != null)
                               && (request.getContextPath().length() > 0))
                       {
                           finalLocation = request.getContextPath() + finalLocation;
                       }
           
                       finalLocation = response.encodeRedirectURL(finalLocation);
                   }
           
                   //LogMan.debug("Refreshing to finalLocation " + finalLocation);
                  
                   //設(shè)置屬性
                   OgnlValueStack stack = ActionContext.getContext().getValueStack();
                  
                   PageMessage aPageMessage = (PageMessage)stack.findValue("pagemessage",PageMessage.class);
                  
                   if(null!=aPageMessage)
                   {
                       aPageMessage.setMsgToURL(finalLocation);
                   }
                  
                   dispatcher.forward(request, response);
               }
           
               //copy from ServletRedirectResult
               private static boolean isPathUrl(String url)
               {
                   // filter out "http:", "https:", "mailto:", "file:", "ftp:"
                   // since the only valid places for : in URL’s is before the path specification
                   // either before the port, or after the protocol
                   return (url.indexOf(’:’) == -1);
               } 
           }
           


           
          3.信息提示頁面

           缺省為/WEB-INF/public/page/msgto.jsp,可以在webwork.properties里面配置"jscud.page.messageto",還可以通過配置文件配置.
           
           可以根據(jù)自己需要更改頁面內(nèi)容.
           
           
           <%@ page contentType="text/html; charset=GBK" %>
           <%@ taglib uri="webwork" prefix="ww" %>
           
           <html>
           <head>
           <meta http-equiv="pragma" content="no-cache">
           <meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
           <meta http-equiv="expires" content="0">
           
           <meta http-equiv="Content-Type" content="text/html; charset=GBK">
           
           <meta http-equiv="refresh" content="<ww:property value="pagemessage.msgToTime" />;URL=<ww:property value="pagemessage.msgToURL" />">
           
           <title><ww:property value="pagemessage.msgTitle" /></title>
           </head>
           <body >
           <div align=center>
           <table width=90% border="0" cellspacing="0" cellpadding="0" align="center" bgcolor=#ffffff>
             <tr valign="top">
               <td width="2">        
               </td>
               <td width="5"></td>
               <td colspan="3">
           <center><br><br>
           <table align="center" border="0" cellpadding="5" cellspacing="0" width="100%">
           <tbody>
           <tr>
           <td><ww:property value="pagemessage.msgTitle" />
           <hr noshade size="1" color="#808000">
           </td>
           </tr>
           <tr>
           <td><ww:property value="pagemessage.msgContent" /><br><br><br>
           請(qǐng)稍后......<br>
           </td>
           </tr>
           </tbody>
           </table>
           </center>
           
               </td>
             </tr>
           </table>
           
           </div>
           </body>
           </html>


           
           
           
          4.配置示例

            

            <result-types>
             <result-type name="msgto" class="com.jscud.www.support.web.ServletMessageToResult" />
            </result-types> 

           

           <action name="doAddCata" class="catagoryAction" method="doSet">
             <result name="input" type="dispatcher">
              <param name="location">/WEB-INF/jsp/news/catagory_add.jsp</param>
             </result>
             <result name="success" type="msgto">
              <param name="location">/news/admin/admin.jspa</param>
             </result>
            </action> 



             

          5.Action中的代碼示例

           

               /**
               * 增加/編輯類別
               *
               * @return 結(jié)果
               */
              public String doSet()
              {
                  //輸入界面
                  if (null == catagory)
                  {
                      return INPUT;
                  }

                  catagoryManager.updateCatagory(getCatagory(), getWork());
                      //return goException("設(shè)置分類時(shí)發(fā)生了錯(cuò)誤",ex);

                  setPageMessageProp("操作成功", "操作成功");

                  return SUCCESS;
              }




           
           
           
           以下代碼放在基礎(chǔ)Action里:(自己要做適當(dāng)修改)
           

              /**
               * 設(shè)置信息提示的內(nèi)容.
               *
               * @param sTitle
               * @param sHintMsg
               */
              protected void setPageMessageProp(String sTitle, String sHintMsg)
              {
                  PageMessage apm = new PageMessage();

                  if ((null == sTitle) || (sTitle.equals("")))
                  {
                      sTitle = getText("core.errorhint.title.default");
                  }

                  apm.setMsgTitle(sTitle);
                  apm.setMsgContent(sHintMsg);

                  setPagemessage(apm);
              }

              /**
               * 設(shè)置信息提示的參數(shù).(轉(zhuǎn)向)
               *
               * @param sTitle
               * @param sHintMsg
               * @param toURL
               * @param nToTime
               */
              protected void setPageMessageProp(String sTitle, String sHintMsg,int nToTime)
              {
                  PageMessage apm = new PageMessage();

                  if ((null == sTitle) || (sTitle.equals("")))
                  {
                      sTitle = getText("core.errorhint.title.default");
                  }

                  apm.setMsgTitle(sTitle);
                  apm.setMsgContent(sHintMsg);
                  //apm.setMsgToURL(toURL);
                  apm.setMsgToTime(nToTime);

                  setPagemessage(apm);
              }


              public PageMessage getPagemessage()
              {
                  return pagemessage;
              }

              public void setPagemessage(PageMessage pagemessage)
              {
                  this.pagemessage = pagemessage;
              }
           
            



           

           
           
          ok,很簡(jiǎn)單,不是嗎?
           

          posted on 2005-08-22 13:13 Scud(飛云小俠) 閱讀(873) 評(píng)論(0)  編輯  收藏 所屬分類: WEB

          <2005年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          文章發(fā)布許可
          創(chuàng)造共用協(xié)議:署名,非商業(yè),保持一致

          我的郵件
          cnscud # gmail


          常用鏈接

          留言簿(15)

          隨筆分類(113)

          隨筆檔案(103)

          相冊(cè)

          友情鏈接

          技術(shù)網(wǎng)站

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 松溪县| 海丰县| 泽州县| 简阳市| 瓮安县| 巴林左旗| 石城县| 威宁| 江都市| 伊川县| 伊宁市| 银川市| 曲水县| 巩留县| 吉木萨尔县| 肥东县| 原阳县| 郑州市| 寿阳县| 湖州市| 阜新| 新化县| 天台县| 吐鲁番市| 宁阳县| 铜陵市| 腾冲县| 封丘县| 石泉县| 武义县| 耒阳市| 昌吉市| 乐清市| 什邡市| 河北区| 讷河市| 天等县| 瑞丽市| 西乡县| 宣武区| 安丘市|