posts - 10, comments - 9, trackbacks - 0, articles - 17

          DispatchAction的使用

          Posted on 2008-12-04 17:12 wesley1987 閱讀(414) 評論(0)  編輯  收藏 所屬分類: struts學習項目

          模塊的實現寫的差不多了,可是 每個頁面的活動都要一個Action,3個對象的增刪改查 就有14個Action。終于今天組長發飆了,要改成DispatchAction。現上網查查怎么用。。找到個本博客中的文章放入收藏了。

          http://www.aygfsteel.com/jxhkwhy/archive/2007/01/21/95177.html

          把增刪改查都放到一個Action中,然后在XML中為每個方法配一個
          < action

                attribute = "addUserForm"

                input = "/addUser.jsp"

                name = "addUserForm"

                parameter="method"

                path = "/addUser"

                scope = "request"

                type="com.why.struts.action.UserAction" >

              </ action >
          其實如果使用的Form(name=“xxxForm”)是一樣的話,幾個方法配一個<action>也行。用多個的時候,注意path不要寫成一樣的了。
          注意parameter的值,作為JSP跳轉的do的參數就可以了?;旧吓渲梅矫?和一般<action>區別就是含有同一個type地址和parameter

          <html:link href="addUser.do?method=addUser">Add User</html:link>

          參數值為Action里的方法名,如這個 對應為 Action中的
          addUser (ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response)


           execute就不用在寫了。雖然不知道寫了會怎么樣。 
          下面是我自己的struts-config.xml配置: 
                      實現offer的增,刪,改,查。其中查和刪的配置寫在了一起。所以是 3個Action,注意其 path,type ,和parameter屬性。調用時用parameter的值區分。這里還可以看到定向到offerSearch的URL的使用。
          這里的name雖然相同,但是分開的原因是 我需要input這個屬性作為異常的轉回頁面。查詢和刪除都不用name 而且異常提示頁面相同,所以合并。
              <action
                
          input="/service/offerSearch.jsp"
                parameter
          ="operation"
                path
          ="/offer"
                scope
          ="request"
                type
          ="com.neusoft.struts.service.action.OfferAction">
                
          <forward name="success" path="/offer.do?operation=offerSearch&amp;offername="/>
                
          <forward name="show" path="/service/offerSearch.jsp" />
              
          </action>
              
          <action
                
          attribute="OfferForm"
                input
          ="/service/offerNew.jsp"
                parameter
          ="operation"
                name
          ="OfferForm"
                path
          ="/offerAdd"
                scope
          ="request"
                type
          ="com.neusoft.struts.service.action.OfferAction">
                
          <forward name="success" path="/offer.do?operation=offerSearch&amp;offername="/>
              
          </action>
              
          <action 
               
          attribute="OfferForm"
              name
          ="OfferForm"
              parameter
          ="operation"
              path
          ="/offerUpdate" 
              input
          ="/service/offerUpdate.jsp"
              scope
          ="request"
              type 
          ="com.neusoft.struts.service.action.OfferAction" >
               
          <forward name="success" path="/offer.do?operation=offerSearch&amp;offername="/>
              
          </action>

          對應的DispatchAction    :

          public class OfferAction extends DispatchAction {
              
              
              
          public ActionForward offerSearch(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response) 
          throws Exception {
                  
                  String offername 
          = changeStr(request.getParameter("offername"));
                  request.setAttribute(
          "offername", offername);
                  
                  HttpSession session 
          = request.getSession();
                  IPageList pageList;

                  
          if(request.getParameter("page")==null){            //來自查詢表單的請求
                      pageList = new PageList(1,OfferManager.getInstance().getOfferList(offername));
                      session.setAttribute(
          "list", pageList.getResultList());
                      request.setAttribute(
          "pageList", pageList);
                  }
                  
          else{                                            //來自分頁鏈接的請求
                      int page = Integer.parseInt(request.getParameter("page"));
                      pageList 
          = new PageList(page,OfferManager.getInstance().getOfferList(offername));
                      session.setAttribute(
          "list", pageList.getResultList());
                      request.setAttribute(
          "pageList", pageList);
                  }        
                  
          return mapping.findForward("show");
              }

              
          public ActionForward offerAdd(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response) 
          throws Exception {

                  OfferForm offer 
          = (OfferForm) form;
                  
                  OfferManager.getInstance().addOffer(offer);
                  
                  
          return mapping.findForward("success");
              }

              
          public ActionForward offerUpdate(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response) 
          throws Exception {
                  
                  OfferForm offer 
          = (OfferForm) form;

                  OfferManager.getInstance().offerUpdate(offer);
                  
          return mapping.findForward("success");
                  
              }

              
          public ActionForward offerDelete(ActionMapping mapping,ActionForm Form,
                              HttpServletRequest request,HttpServletResponse response)
          throws Exception{
                  
                  String id 
          = request.getParameter("offerId");
                  
          if(id!=null){
                      OfferManager.getInstance().offerDelete(Integer.parseInt(id));
                  }
                  
          return mapping.findForward("success");
              }
          }



          主站蜘蛛池模板: 天津市| 改则县| 濮阳县| 那坡县| 兖州市| 禹城市| 商洛市| 枣强县| 喀喇沁旗| 芒康县| 萨迦县| 探索| 房产| 道真| 牙克石市| 昌江| 观塘区| 永仁县| 乾安县| 大名县| 景宁| 博罗县| 桃江县| 义乌市| 鲜城| 樟树市| 乌兰浩特市| 沁水县| 盐山县| 闽侯县| 金昌市| 浑源县| 江陵县| 千阳县| 丰镇市| 高淳县| 定陶县| 晋江市| 松原市| 巴林左旗| 金堂县|