Struts 跨页提交表单

问题描述:有时,表单数据太多,无法在同一个页面显示,需要分页完成(如用户注册表单)。这时,既可以为每一个表单创建一个ActionForm,也可以只创建一个ActionForm,它和多个表单对应。这里讨论如何用一个ActionForm对应表单。

[color=darkred]1.把HTML表单拆分到多个JSP页面中[/color]

这里我们把注册表单拆分为两个表单:第一个在insertContent.jsp中定义,包括name和phone字段,第二个表单在insertContent_next.jsp中定义,包括address字段。这两个表单分别对应不同的Action: “/insert1”和“/insert2”,但是这两个Action与同一个ActionForm映射。

注意在insertContent.jsp和insertContent_next.jsp中定义一个隐含字段page,它代表当前页面编号,AcitonForm将通过这个字段来识别当前正在处理的是哪个表单。insertContent.jsp中:
<html:form action="/insert1.do">

<html:hidden property="page" value="1" />省略. . . . . .

</html:form>

insertContent_next.jsp类似:

<html:form action="/insert2.do">

<html:hidden property="page" value="2" />省略. . . . . .

</html:form>


[color=darkred]2.创建和多个HTML表单对应的ActionForm(InsertForm.java)[/color]

需要注意一下几点:

[color=green](1)[/color]提供和HTML表单的隐藏字段page对应的page属性,并生成get和set方法

[color=green](2)[/color] 在reset()方法中,只能把和当前正在处理的表单相关的属性恢复为默认值,否则,如果每次都把ActionForm的所有属性恢复为默认值,将使用户输入的上一页表单数据丢失。由于Struts框架先调用reset()方法,然后再把用户输入的表单数据组装到ActionForm中,因此在reset()方法中,不能根据page属性来判断处理的是哪个页面,而应该直接从HttpServletRequest对象中读取当前表单的page字段值:page=new Integer(request.getParameter("page")).intValue();

完整代码如下:

public void reset(ActionMapping mapping, HttpServletRequest request) {

//根据“page”的值处理不同的字段

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;

}

[color=green](3) [/color]在validate()方法中,仅对和当前表单相关的属性进行验证。由于Struts框架在调用validate()方法之前,已经把用户输入的表单数据组装到ActionForm中,因此在validate()方法中可以根据page属性决定正在处理哪个表单。 代码如下:
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;

}


[color=darkred]3.配置ActionForm和多个Action映射[/color]
当ActionForm 与多个表单对应时,应该把 ActionForm 存放在 session 中,当用户提交第一个表单时,请求由 org.apache.strutsactions.ForwardAction来处理,ForwardAction 类是 Struts 框架内置的 Action 类,他的 execute()方法负责把请求再转发给<action>元素的 parameter 属性指定的 web 组件。当用户提交第 二个表单时,请求被转发给相应的 InsertAction.
struts-config.xml 源代码
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

<data-sources />

<form-beans >

<form-bean name="insertForm" type="com.yourcompany.struts.form.InsertForm" />

</form-beans>

<global-exceptions />

<global-forwards />

<action-mappings >

<action path="/insert1"

parameter="/insertContent_next.jsp"

type="org.apache.struts.actions.ForwardAction"

name="insertForm"

scope="session"

input="/insertContent.jsp"

validate="true">

</action>

<action path="/insert2"

type="com.yourcompany.struts.action.InsertAction"

name="insertForm"

scope="session"

input="/insertContent_next.jsp"

validate="true">

</action>

</action-mappings>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />

</struts-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值