問題描述:有時,表單資料太多,無法在同一個頁面顯示,需要分頁完成(如使用者註冊表單)。這時,既可以為每一個表單建立一個ActionForm,也可以只建立一個ActionForm,它和多個表單對應。這裡討論如何用一個ActionForm對應表單。
1. 把HTML表單拆分到多個JSP頁面中
這裡我們把註冊表單拆分為兩個表單:第一個在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>
2.建立和多個HTML表單對應的ActionForm(InsertForm.java)
需要注意一下幾點:
(1) 提供和HTML表單的隱藏欄位page對應的page屬性,並產生get和set方法
(2)
在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;
(3)
在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;
3.配置ActionForm和多個Action映射
當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>