Struts 处理表单跨页
来源:互联网 发布:网络网名吸引女孩子 编辑:程序博客网 时间:2024/04/28 12:42
<%@ 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>
<%@ 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>
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;
}
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 处理表单跨页
- struts表单乱码处理
- 最简单的Struts程序(3)---处理表单跨页(参考孙卫琴的struts一书)
- 最简单的Struts程序(3)---处理表单跨页
- struts 表单中的cancel按钮出错处理
- STRUTS 跨页面表单提交
- [Struts]处理表单中值为空的日期类型字段
- struts in action 学习之01表单提交处理流程
- struts form表单提交action处理之后没有跳转页面
- 【struts】struts2的tokenSession处理表单重复提交
- struts 跨表单提交,分步注册
- struts表单中多个submit的处理方法:org.apache.struts.actions.DispatchAction
- struts表单中多个submit的处理方法:org.apache.struts.actions.LookupDispatchAction
- struts 表单验证
- struts表单验证
- Struts分步表单提交
- struts表单验证详解
- Struts Validator检查表单
- 力戒浮躁与功利
- 解决JSP中文乱码问题
- 最后更新的我自己要得到的sql,累了我四天终于弄出了点成绩,虽然很小
- 动态导出为csv文件
- __stdcall 和 __cdecl 的区别
- Struts 处理表单跨页
- 项目记录
- 动态导出为csv文件 (2)不带引号!
- linux 学习笔记(4.26)
- 一道笔试题
- DVD-Video 解谜 - IFO文件 - VTS specific tables
- Windows多线程多任务设计初步
- 改变ASP.NET2.0中Login控件的密码规则
- NumberFormat的使用技巧