前言:有时,表单数据太多,无法在同一个页面显示,需要分页完成(如用户注册表单)。这时,既可以为每一个表单创建一个ActionForm,也可以只创建一个ActionForm,它和多个表单对应。
这里我们把注册表单拆分为两个表单:第一个在insertContent.jsp中定义,包括name和phone字段,第二个表单在insertContent_next.jsp中定义,包括address字段。
1. 把HTML表单拆分到多个JSP页面中
insertContent.jsp
<%
@ page contentType
=
"
text/html;charset=gb2312
"
language
=
"
java
"
%>
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<% @ taglib uri = " /WEB-INF/struts-logic.tld " prefix = " logic " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html:html >
< head >
< html:base />
</ head >
< body >
< html:errors />
< html:form action = " /insert1.do " focus = " title " >
< html:hidden property = " page " value = " 1 " />
<! — - 定义一个隐含字段,它代表当前页面编号,AcitonForm将通过这个字段来识别当前正在处理的是哪个表单。 -->
< center >
< table border = " 0 " cellspacing = " 2 " cellpadding = " 2 " width = " 100% " >
< tr >
< td align = " right " >
< bean:message key = " prompt.name " />
</ td >
< td >
< html:text property = " name " size = " 25 " maxlength = " 25 " />
</ td >
</ tr >
< tr >
< td align = " right " >
< bean:message key = " prompt.phone " />
</ td >
< td >
< html:text property = " phone " size = " 25 " maxlength = " 10 " />
</ td >
</ tr >
< tr >
< td align = " right " >
< html:submit property = " submit " value = " next " >
< bean:message key = " button.next " />
</ html:submit >
</ td >
< td align = " left " >
< html:reset >
< bean:message key = " button.reset " />
</ html:reset >
</ td >
</ tr >
</ table >
</ center >
</ html:form >
</ body >
</ html:html >
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<% @ taglib uri = " /WEB-INF/struts-logic.tld " prefix = " logic " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html:html >
< head >
< html:base />
</ head >
< body >
< html:errors />
< html:form action = " /insert1.do " focus = " title " >
< html:hidden property = " page " value = " 1 " />
<! — - 定义一个隐含字段,它代表当前页面编号,AcitonForm将通过这个字段来识别当前正在处理的是哪个表单。 -->
< center >
< table border = " 0 " cellspacing = " 2 " cellpadding = " 2 " width = " 100% " >
< tr >
< td align = " right " >
< bean:message key = " prompt.name " />
</ td >
< td >
< html:text property = " name " size = " 25 " maxlength = " 25 " />
</ td >
</ tr >
< tr >
< td align = " right " >
< bean:message key = " prompt.phone " />
</ td >
< td >
< html:text property = " phone " size = " 25 " maxlength = " 10 " />
</ td >
</ tr >
< tr >
< td align = " right " >
< html:submit property = " submit " value = " next " >
< bean:message key = " button.next " />
</ html:submit >
</ td >
< td align = " left " >
< html:reset >
< bean:message key = " button.reset " />
</ html:reset >
</ td >
</ tr >
</ table >
</ center >
</ html:form >
</ body >
</ html:html >
insertContent_next.jsp
<%
@ page contentType
=
"
text/html;charset=gb2312
"
language
=
"
java
"
%>
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<% @ taglib uri = " /WEB-INF/struts-logic.tld " prefix = " logic " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html:html >
< head >
< html:base />
</ head >
< body >
< html:errors />
< html:form action = " /insert2.do " focus = " title " >
< html:hidden property = " page " value = " 2 " />
< center >
< table border = " 0 " cellspacing = " 2 " cellpadding = " 2 " width = " 100% " >
< tr >
< td align = " right " >
< bean:message key = " prompt.address " />
</ td >
< td >
< html:text property = " address " size = " 25 " maxlength = " 50 " />
</ td >
</ tr >
< tr >
< td align = " right " >
< html:submit property = " submit " >
< bean:message key = " button.insert " />
</ html:submit >
</ td >
< td align = " left " >
< html:reset >
< bean:message key = " button.reset " />
</ html:reset >
</ td >
</ tr >
</ table >
</ center >
</ html:form >
</ body >
</ html:html >
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<% @ taglib uri = " /WEB-INF/struts-logic.tld " prefix = " logic " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html:html >
< head >
< html:base />
</ head >
< body >
< html:errors />
< html:form action = " /insert2.do " focus = " title " >
< html:hidden property = " page " value = " 2 " />
< center >
< table border = " 0 " cellspacing = " 2 " cellpadding = " 2 " width = " 100% " >
< tr >
< td align = " right " >
< bean:message key = " prompt.address " />
</ td >
< td >
< html:text property = " address " size = " 25 " maxlength = " 50 " />
</ td >
</ tr >
< tr >
< td align = " right " >
< html:submit property = " submit " >
< bean:message key = " button.insert " />
</ html:submit >
</ td >
< td align = " left " >
< html:reset >
< bean:message key = " button.reset " />
</ html:reset >
</ td >
</ tr >
</ table >
</ center >
</ html:form >
</ body >
</ html:html >
以上两个表单分别对应不同的Action: “/insert1”和“/insert2”,但是这两个Action与同一个ActionForm映射。
2.创建和多个HTML表单对应的ActionForm(InsertForm.java)
3.配置ActionForm和多个Action映射
struts-config.xml
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<! DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >
< struts-config >
<!-- ========== Form Bean Definitions =================================== -->
< form-beans >
< form-bean name ="insertForm"
type ="forms.InsertForm" />
</ form-beans >
<!-- ========== Global Forward Definitions ============================== -->
< global-forwards >
< forward name ="confirmation" path ="/jsp.jsp" />
</ global-forwards >
<!-- ========== Action Mapping Definitions ============================== -->
< action-mappings >
<!-- 当提交第一个表单时,请求由org.apache.struts.actions.ForwardAction来处理,ForwardAction类是Struts框架内置的Action类,它的execute()方法负责把请求再转发给<action>元素的parameter属性指定的WEB组件。 -->
< 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 ="actions.InsertAction"
name ="insertForm"
scope ="session"
input ="/insertContent_next.jsp"
validate ="true" >
</ action >
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
<! DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >
< struts-config >
<!-- ========== Form Bean Definitions =================================== -->
< form-beans >
< form-bean name ="insertForm"
type ="forms.InsertForm" />
</ form-beans >
<!-- ========== Global Forward Definitions ============================== -->
< global-forwards >
< forward name ="confirmation" path ="/jsp.jsp" />
</ global-forwards >
<!-- ========== Action Mapping Definitions ============================== -->
< action-mappings >
<!-- 当提交第一个表单时,请求由org.apache.struts.actions.ForwardAction来处理,ForwardAction类是Struts框架内置的Action类,它的execute()方法负责把请求再转发给<action>元素的parameter属性指定的WEB组件。 -->
< 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 ="actions.InsertAction"
name ="insertForm"
scope ="session"
input ="/insertContent_next.jsp"
validate ="true" >
</ action >
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
package
forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/** */ /**
* <strong>InsertForm</strong> handles the form that the user will use to
* insert a new Address into the database.
*/
public final class InsertForm extends ActionForm ... {
private String name = null;
private String phone = null;
private String address = null;
private String page = null;
public String getName() ...{
return name;
}
public String getPhone() ...{
return phone;
}
public String getAddress() ...{
return address;
}
public String getPage() ...{
return page;
}
//根据“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 void setName(String name) ...{
this.name = name;
}
public void setPhone(String phone) ...{
this.phone = phone;
}
public void setAddress(String address) ...{
this.address = address;
}
public void setPage(String page) ...{
this.page = 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;
}
}
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/** */ /**
* <strong>InsertForm</strong> handles the form that the user will use to
* insert a new Address into the database.
*/
public final class InsertForm extends ActionForm ... {
private String name = null;
private String phone = null;
private String address = null;
private String page = null;
public String getName() ...{
return name;
}
public String getPhone() ...{
return phone;
}
public String getAddress() ...{
return address;
}
public String getPage() ...{
return page;
}
//根据“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 void setName(String name) ...{
this.name = name;
}
public void setPhone(String phone) ...{
this.phone = phone;
}
public void setAddress(String address) ...{
this.address = address;
}
public void setPage(String page) ...{
this.page = 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;
}
}