寶貝小豬嘜

          常用鏈接

          統計

          最新評論

          struts-處理標單跨頁

          有的時候由于表單數據太多,無法在同一個頁面顯示,可以把它拆分成多個表單,
          分多個頁面顯示,這種情況下,既可以為每個表單創建單獨的ActionForm,
          也可以只創建一個ActionForm和多個表單對應

          例:
          insertForm表單包含三個字段:name,phone,address,把它分成兩個表單。
          第一個表單在insertContent.jsp中,包含name和phone字段。
          第二個表單在insertContent_next.jsp中,包含address字段,
          它們分別對應不同的Action:"/insert1"和"/insert2",但是這兩個Action與同一個Actionform映射
          1.把html表單拆分到多個jsp頁面中
          ???可以在每個表單中定義一個隱含字段<html:hidden property="page"/>代表當前頁面編號

          ???insertContent.jsp中定義html表單的代碼
          ???<html:form action="/insert1.do" focus="title">
          ??????<html:hidden property="page" value="1"/>
          ??????...........................
          ???</html:form>

          ???insertContent_next.jsp中定義html表單的代碼
          ???<html:form action="/insert1.do" focus="title">
          ??????<html:hidden property="page" value="2"/>
          ??????...........................
          ???</html:form>

          2.創建和多個html表單對應的ActionForm
          ???以上兩個html表單都對應InsertForm Bean,在創建時應注意以下幾點
          ??????(1).提供和html表單隱藏字段page對應的page屬性
          ????????????private String page = null;
          ????????????public String getPage(){
          ???????????????return page;
          ????????????}
          ????????????public void setPage(){
          ????????????this.page = page;
          ????????????}
          ??????(2).在reset()方法中只能把和當前正在處理的表單相關的屬性恢復為默認值
          ????????????否則將使上一頁表單數據丟失。
          ????????????由于struts先調用reset()方法,再把用戶輸入的表單數據組裝到ActionForm中,
          ????????????因此在reset()中不能根據page屬性判斷處理的是哪個頁面,
          ????????????應直接從HttpServletRequest對象中讀取當前表單的page字段值
          ????????????int numPage = new Ingeter(request.getParameter("page")).intValue();
          ????
          ??(3).在validate()方法中,僅對和當前表單相關的屬性進行驗證
          ????????????由于在調用validate()方法前已經把數據組裝到ActionForm中,
          ????????????因此在validate()中可以根據page屬性決定正在處理那個表單
          代碼如下:
          public void reset(ActionMapping mapping, HttpServletRequest request) {
          ??????? int numPage=0;
          ??????? try{?
          ????????????numPage=new Integer(request.getParameter("page")).intValue();
          ????????}catch(Exception e){}????????
          ??????? if(numPage==1){
          ??????????? name=null;
          ??????????? phone=null;
          ??????? }????
          ?????? if(numPage==2){
          ???????? address=null;
          ?????? }
          ???????page=null;
          }


          public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
          ????????????ActionErrors errors = new ActionErrors();
          ??????????? int numPage=0;
          ?????????? ?try{?
          ???????????????numPage=new Integer(page).intValue();
          ????????????}catch(Exception e){}????????
          ?????????? if(numPage==1){
          ??????????????? if (((name == null) || (name.length() < 1)))
          ???????????????????? errors.add("name", new ActionMessage("error.name.required"));
          ?????????? if(((phone == null)|| (phone.length() < 1)))
          ?????????????????????errors.add("phone", new ActionMessage("error.phone.required"));
          ?????????? }?????
          ???????? ? if(numPage==2){
          ????????????????if(((address == null)|| (address.length() < 1)))
          ???????????????????? errors.add("address", new ActionMessage("error.address.required"));
          ?????????? }
          ??????? return errors;
          ??????}

          3.配置ActionForm和多個Action映射
          ???當ActionForm與多個表單對應時,應該把ActionForm存放在session范圍內
          ???本例中當用戶提交第一個表單時,請求由org.apache.struts.actions.ForwardAction處理
          ???以下是<action>元素配置代碼
          ???<action path="/insert1"
          ??????????????????parameter="/insert_next.jsp"
          ??????????????????type="org.apache.struts.actions.ForwardAction"
          ??????????????????name="insertForm"
          ??????????????????scope="session"
          ??????????????????input="/insert.jsp"
          ??????????????????validate="true">
          ???</action>
          ???<action path="/insert2"
          ??????????????????type="address.actions.InsertAction"
          ??????????????????name="insertForm"
          ??????????????????scope="session"
          ??????????????????input="/insert_next.jsp"
          ??????????????????validate="true">
          ???</action>

          posted on 2006-12-06 10:19 寶貝小豬嘜 閱讀(369) 評論(1)  編輯  收藏 所屬分類: 框架&設計模式

          評論

          # re: struts-處理標單跨頁 2009-08-29 10:40 鄧云飛

          有沒有struts跨頁注冊詳細的源碼?有的話能不能發個給我呢333333dd@163.com 非常感謝。  回復  更多評論   

          主站蜘蛛池模板: 临沭县| 揭阳市| 洛南县| 丰顺县| 澎湖县| 公安县| 朔州市| 大英县| 体育| 璧山县| 岫岩| 抚松县| 兴和县| 贡山| 巨野县| 桂林市| 西林县| 陇南市| 兴业县| 漳平市| 汪清县| 安平县| 贵南县| 岳阳市| 肇东市| 吉林省| 盈江县| 星子县| 错那县| 林州市| 馆陶县| 新宁县| 北票市| 岐山县| 方正县| 昌邑市| 汉寿县| 信宜市| 陆丰市| 丹棱县| 嵩明县|