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

          DispatchAction的使用

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

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

          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跳轉(zhuǎn)的do的參數(shù)就可以了。基本上配置方面 和一般<action>區(qū)別就是含有同一個type地址和parameter

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

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


           execute就不用在寫了。雖然不知道寫了會怎么樣。 
          下面是我自己的struts-config.xml配置: 
                      實現(xiàn)offer的增,刪,改,查。其中查和刪的配置寫在了一起。所以是 3個Action,注意其 path,type ,和parameter屬性。調(diào)用時用parameter的值區(qū)分。這里還可以看到定向到offerSearch的URL的使用。
          這里的name雖然相同,但是分開的原因是 我需要input這個屬性作為異常的轉(zhuǎn)回頁面。查詢和刪除都不用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>

          對應(yīng)的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");
              }
          }



          主站蜘蛛池模板: 益阳市| 淮南市| 锦州市| 赤水市| 平远县| 肥西县| 眉山市| 阜宁县| 合川市| 溧阳市| 开封市| 临夏市| 绍兴县| 河源市| 平原县| 思茅市| 水城县| 沂源县| 台前县| 琼海市| 贡嘎县| 疏附县| 铜梁县| 杭锦旗| 延庆县| 琼中| 张家川| 屏南县| 天津市| 栖霞市| 特克斯县| 崇左市| 滦南县| 沾化县| 唐山市| 濉溪县| 伊川县| 无棣县| 花垣县| 图片| 东港市|