宝贝小猪唛

常用链接

统计

最新评论

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 宝贝小猪唛 阅读(354) 评论(1)  编辑  收藏 所属分类: 框架&设计模式

评论

# re: struts-处理标单跨页 2009-08-29 10:40 邓云飞

有没有struts跨页注册详细的源码?有的话能不能发个给我呢333333dd@163.com 非常感谢。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: