最简单的Struts程序(3)---处理表单跨页
2007-04-20 16:39
381 查看
处理表单跨页(参考孙卫琴的struts一书)
前言:有时,表单数据太多,无法在同一个页面显示,需要分页完成(如用户注册表单)。这时,既可以为每一个表单创建一个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>
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>
以上两个表单分别对应不同的Action: “/insert1”和“/insert2”,但是这两个Action与同一个ActionForm映射。
2.创建和多个HTML表单对应的ActionForm(InsertForm.java)
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;
}
}
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>
前言:有时,表单数据太多,无法在同一个页面显示,需要分页完成(如用户注册表单)。这时,既可以为每一个表单创建一个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>
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>
以上两个表单分别对应不同的Action: “/insert1”和“/insert2”,但是这两个Action与同一个ActionForm映射。
2.创建和多个HTML表单对应的ActionForm(InsertForm.java)
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;
}
}
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>
相关文章推荐
- 最简单的Struts程序(3)---处理表单跨页(参考孙卫琴的struts一书)
- Struts 处理表单跨页
- input(type="file")+Handler(一般处理程序)上传文件简单Demo
- 简单的学生信息处理程序实现(C++程序设计第2周)
- Struts学习之简单入门程序
- 求一跨页提交的例子,尽量使用jsp+js+struts1(别用struts标签)开发出来的例子 ,该怎么处理
- 提交表单最简单的AJAX程序分享
- 提交表单最简单的AJAX程序分享
- jsp基本语法与简单表单处理
- 最简单的Struts程序(3)------显示MYSQL数据库中的数据
- 利用Struts 处理 web程序时 一些容易出错的问题
- 一个简单的四则运算程序C语言实现--无法处理括号
- 文本框的简单处理程序
- 实战 HTTP 处理程序(HTTP Handler) (1) -- 创建一个最简单的 HTTP Handler
- windows 的最简单的窗口程序的处理流程
- 一个简单的事件处理程序
- Ajax表单提交及后台处理简单应用
- 最简单的ASP.Net程序处理过程(后续)
- 第一个简单的程序——Struts 2 官方文档翻译(Struts 2官方文档中文版)
- 一般处理程序中使用隐藏域来实现非Form表单元素上数据的改变