Struts-分页表单

有时候,由于表单数据太多,无法在同一个页面显示(如用于用户注册的表单),可以把它拆分成多个表单,分多个页面显示,在这种情况下,即可以为每个表单创建单独的ActionForm,也可以只创建一个ActionForm,它和多个表单对应,下面就来介绍通过struts来实现此功能,本例使用一个ActionForm共享多个表单的机制来实现,运行截图如下:

1.进入首页,输入帐号与手机信息:

2.点击“下一页”,进入下页表单页面:

3.最后输入信息点击“提交”后,显示用户输入的信息:

4.同时如果用户在表单提交中出现错误给予提示信息:

下面就是相应的代码:

1.表单的第一页index.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分页表单</title>
</head>
<body>
  <html:form action="/nextPageForm.do" method="post">
  <html:hidden property="page" value="1" />
  <table>
    <tr>
       <td>帐号:</td>
       <td><html:text property="name" size="14" /></td>
       <td><font color="red"><html:errors property="name" /></font></td>
    </tr>
    <tr>
       <td>手机:</td>
       <td><html:text property="phone" size="14" /></td>
       <td><font color="red"><html:errors property="phone" /></font></td>
    </tr>
    <tr>
       <td colspan="2">
          <html:submit>下一页</html:submit>
          <html:reset>重置</html:reset>
       </td>
    </tr>
  </table>
  </html:form>
</body>
</html>

2.表单的第二页next.jsp视图:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分页表单</title>
</head>
<body>
  <html:form action="/confirmAction.do">
  <html:hidden property="page" value="2" />
      邮箱:<html:text property="email" />
      <font color="red"><html:errors property="email" /></font>
      <br />
      <html:button property="prePage" οnclick="location.href='index.jsp'">上一页</html:button>
      <html:submit>提交</html:submit>
      <html:reset>重置</html:reset>
  </html:form>
</body>
</html>
3.对应的信息息确认画面confirm.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>信息确认</title>
</head>
<body>
<logic:present name="insertForm">
   帐号:<bean:write name="insertForm" property="name" /><br />
   手机:<bean:write name="insertForm" property="phone" /><br />
   邮件:<bean:write name="insertForm" property="email" /><br />
</logic:present>
</body>
</html>
注意:上面的表单页面视图中,采用一个隐藏域page来标记当前表单的页数,这样做方便ActionForm后台的检证与reset()恢复默认值的操作。

4.相应的ActionForm代码:

package test.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class InsertForm extends ActionForm {
	private String name;
	private String phone;
	private String email;
	private String page = null;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		int numPage = 0;
		try {
			numPage = Integer.parseInt(request.getParameter("page"));
		} catch(NumberFormatException e) {
			
		}
		if(numPage==1) {
			name=null;
			phone=null;
		} else if (numPage==2) {
			email=null;
		}
		page=null;
	}
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		int numPage = 0;
		try {
			numPage = Integer.parseInt(page);
		} catch(NumberFormatException e) {
			
		}
		
		if (numPage==1){
			if(name==null||name.length()<=0) {
				errors.add("name", new ActionMessage("error.name.requried"));
			}
			if(phone==null||phone.length()<=0) {
				errors.add("phone", new ActionMessage("error.phone.requried"));
			}
		} else if(numPage==2) {
			if (email==null||email.length()<=0) {
				errors.add("email", new ActionMessage("error.email.requried"));
			}
		}
		return errors;
	}
	
	
}
上面的ActionForm通过page来判断对不同页面的不同处理;这在里要注意到在reset()方法中使用request的方式来得到page,而不是直接通过page属性来判断处理的是哪一页,是因为Struts框架先调用reset()方法之后,才把用户输入的表单数据组装到ActionForm中,因此要通过request对象得到;而执行validate()方法时Struts框架在调用validate()方法之前就已经把用户输入的表单数据组装到ActionForm中,因此可以直接根据page属性来判断页面。

5.Struts配置文件struts-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
       "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <form-beans>
        <form-bean name="insertForm" type="test.struts.form.InsertForm" />
    </form-beans>
    
    <action-mappings>
       <action path="/nextPageForm" name="insertForm"
          validate="true" input="/index.jsp"
          scope="session" parameter="/next.jsp"
          type="org.apache.struts.actions.ForwardAction" >
       </action>
       <action path="/confirmAction" name="insertForm"
          validate="true" input="/next.jsp"
          scope="session" parameter="/confirm.jsp"
          type="org.apache.struts.actions.ForwardAction" >
       </action>
    </action-mappings>
    
    <message-resources parameter="application" />
</struts-config>
由于在这里只是简单的数据传递,没有涉及到业务逻辑代码,因此没有构建自定义的Action类,而是使用了Struts框架的内置Action:ForwardAction。

注意:在使用ForwardAction类时在Struts1.3中注意引入struts-extras-1.3.10.jar包,它其中包含了一些Struts框架内置的Action类。

最后给出上面示例中引入的application.properties资源文件,如下:

error.email.requried=邮件为必须项!
error.name.requried=帐号为必须项!
error.phone.requried=手机为必须项!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值