Struts中一起提交多个JSP中的表单

      如果需要提交的表单内容比较多,一个JSP页面不够用,就需要多个表单依次提交,最后一起汇总给
ActionServlet处理

   JSP1页面(其中hidden字段很重要,表示是第一个提交页面)
  
  < html >
     
< head >
     
< title > jsp1 </ title >
     
</ head >
     
< body  bgcolor ="#ffffff" >
     
< html:form  action ="/insertAction.do" >
       
< html:hidden  property ="page"  value ="1" />
       name:
< html:text  property ="name" />
       pass:
< html:text  property ="password" />
      
< html:submit > submit
     
</ html:submit >
    
</ html:form >
    
</ body >
    
</ html >

JSP2页面

 

      < html >
     
< head >
     
< title > jsp2 </ title >  
     
</ head >
     
< body  bgcolor ="#ffffff" >
     
< html:form  action ="/insertAction.do" >
       
< html:hidden  property ="page"  value ="2" />
       city:
< html:text  property ="city" />
      
< html:submit > submit
     
</ html:submit >
    
</ html:form >
    
</ body >
    
</ html >

 

为两个页面建立共同的ActionForm,注意,scope要为session

 

public   class  InsertForm  extends  ActionForm  {
    
private String name=null;
    
private String password=null;
    
private String city=null;
    
private String page=null;
    
public ActionErrors validate(ActionMapping actionMapping,
                                 HttpServletRequest  
                                 httpServletRequest) 
{

        ActionErrors errors
=new ActionErrors();
        
int numpage=0;
        
try {
            numpage
=new Integer(page).intValue();
        }
 catch (Exception ex) {          
        }

        
if(numpage==1){
            
if((name==null)||(name.length()<1)){
                errors.add(
"name",new ActionMessage("123"));
            }

            
if((password==null)||(password.length()<1)){
                errors.add(
"password",new ActionMessage("123"));
            }

        }

        
if(numpage==2){
           
if((city==null)||(city.length()<1)){
               errors.add(
"city",new ActionMessage("123"));
           }

       }

       
return errors;
    }

    
public void reset(ActionMapping actionMapping,
                      HttpServletRequest servletRequest) 
{
        
int numpage=0;
        
try {
            numpage
=new Integer(servletRequest.getParameter
                   (
"page")).intValue();
        }
 catch (Exception ex) {  
        }

        
if(numpage==1){
            name
=null;
            password
=null;
        }

        
if(numpage==2){
            city
=null;
        }

    }

    
public String getCity() {
        
return city;
    }

    
public String getName() {
        
return name;
    }

    
public String getPage() {
        
return page;
    }

    
public String getPassword() {
        
return password;
    }

    
public void setPassword(String password) {
        
this.password = password;
    }

    
public void setPage(String page) {
        
this.page = page;
    }

    
public void setName(String name) {
        
this.name = name;
    }

    
public void setCity(String city) {
        
this.city = city;
    }

}


以上是两个html表单都对应I"nsertForm,在创建InsertForm时有以下几点需要注意
(1)提供html表单的隐藏字段page对应的page属性
(2)在reset方法中,只把和当前正在处理的表单相关的属性恢复为默认值
(3)在validate中,仅对和当前表单相关的属性进行验证
Struts-config.xml内容
< struts-config >
  
< form-beans >
    
< form-bean  name ="insertForm"  type ="untitled2.InsertForm"   />
  
</ form-beans >
  
< action-mappings >
    
< action  input ="/jsp1.jsp"  name ="insertForm"  
       parameter
="/jsp2.jsp"  path ="/insertAction"  
       scope
="session"  
       type
="org.apache.struts.actions.ForwardAction"  
       validate
="true"   />
    
< action  input ="/jsp2.jsp"  name ="insertForm"  
       path
="/insertAction2"  scope ="session"  
       type
="untitled2.InsertAction2"  validate ="true"   />
  
</ action-mappings >
  
< message-resources  parameter ="ApplicationResources"   />
</ struts-config >

 

JSP1页面的action为/insertAction.do
JSP2页面的action为/insertAction2.do
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值