`

Struts分步表单提交

阅读更多

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

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

JSP2页面

<html>
<head>
<title>jsp2</title>
</head>
<bodybgcolor="#ffffff">
<html:formaction="/insertAction.do">
<html:hiddenproperty="page"value="2"/>
city:
<html:textproperty="city"/>
<html:submit>submit
</html:submit>
</html:form>
</body>
</html>

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

publicclassInsertFormextendsActionForm...{
privateStringname=null;
privateStringpassword=null;
privateStringcity=null;
privateStringpage=null;
publicActionErrorsvalidate(ActionMappingactionMapping,
HttpServletRequest
httpServletRequest)
...{

ActionErrorserrors
=newActionErrors();
intnumpage=0;
try...{
numpage
=newInteger(page).intValue();
}
catch(Exceptionex)...{
}

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

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

}

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

}

returnerrors;
}

publicvoidreset(ActionMappingactionMapping,
HttpServletRequestservletRequest)
...{
intnumpage=0;
try...{
numpage
=newInteger(servletRequest.getParameter
(
"page")).intValue();
}
catch(Exceptionex)...{
}

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

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

}

publicStringgetCity()...{
returncity;
}

publicStringgetName()...{
returnname;
}

publicStringgetPage()...{
returnpage;
}

publicStringgetPassword()...{
returnpassword;
}

publicvoidsetPassword(Stringpassword)...{
this.password=password;
}

publicvoidsetPage(Stringpage)...{
this.page=page;
}

publicvoidsetName(Stringname)...{
this.name=name;
}

publicvoidsetCity(Stringcity)...{
this.city=city;
}

}


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

JSP1页面的action为/insertAction.do
JSP2页面的action为/insertAction2.do
分享到:
评论

相关推荐

    跨页提交注册,是一中需要验证的注册和登陆

    Struts框架,作为Java EE领域中的一个老牌MVC(Model-View-Controller)框架,常被用来处理这种多步骤的表单提交。下面将详细阐述这一主题。 首先,我们来看“跨页提交注册”这个概念。在传统的网页表单中,用户...

    struts1 Validater 分页注册例子

    Validater是Struts1中的一个核心组件,它主要用于处理用户表单提交的数据验证。这个框架提供了一种声明式和编程式的验证方式,使得开发者可以方便地定义和执行验证规则。声明式验证通过XML配置文件进行,而编程式...

    Struts2 Result类型

    这不仅改变了URL,还意味着新的Action上下文,适合处理多级导航或分步表单。 7. **Stream Result (type="stream")** Stream Result专门用于处理文件下载。它可以将Action生成的InputStream对象直接发送到浏览器,...

    struts in action

    - 分步解释每个步骤的目的和实现细节。 - **第4章:配置Struts组件**(Configuring Struts components) - 详细介绍如何通过配置文件(如struts-config.xml)来定制框架行为。 - 探讨高级配置选项,如国际化支持...

    Stripes 快速入门 pdf 中文版 下载

    Stripes提供了丰富的功能来处理表单提交的数据,包括自动类型转换、数据验证等。 #### 4.2. 用户注册程序 用户注册是Web应用程序中最常见的场景之一。Stripes通过内置的验证机制和数据绑定功能,可以方便地实现...

    stripes 和css 一些常用功能

    分步提交允许用户在一系列表单中逐步提交数据,而不需要一次性完成全部数据的输入。 在文件处理方面,Stripes支持单个文件上传和多文件上传,以及文件下载功能。Stripes通过注解的方式简化了这些操作的处理流程。 ...

    Spring基础教程

    ### Spring基础教程知识点详解 #### 一、Spring框架简介与快速入门 - **Spring框架概述**:Spring框架是由Rod Johnson创建的一...这种分步实施的方法有助于更好地理解每个部分的功能和作用,从而提高开发效率和质量。

Global site tag (gtag.js) - Google Analytics