《struts-利用struts的标签,兑现简单的表单提交及校验》
《struts---利用struts的标签,实现简单的表单提交及校验》
部分不重要的代码,未复制!
<!--注册页面--> <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head> <title>用户注册</title> </head> <body> <!-- 这里用的是struts的标签,它给我们提供了很多的便利 --> <html:form action="/UserMain"> 姓名 : <html:text property="name"/><html:errors property="name"/><br/> <!-- 在回显时,为了数据安全就将密码清空了,只需将其value值设为""即可 --> 密码 : <html:password property="password" value=""/><html:errors property="password"/><br/> 确认密码 : <html:password property="password2" value=""/><html:errors property="password2"/><br/> <html:submit value="提交"/> </html:form> </body> </html>
//用于储存用户信息的FormBean /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.fenghuo.struts.web.forms; 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; /** * MyEclipse Struts * Creation date: 10-07-2012 * * XDoclet definition: * @struts.form name="userForm" */ public class UserForm extends ActionForm { /** * */ private static final long serialVersionUID = 8184190723003145234L; /** password2 property */ private String password2; /** name property */ private String name; /** password property */ private String password; /* * Generated Methods */ /** * Method validate * @param mapping * @param request * @return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (!password.equals(password2)){ errors.add("password", new ActionMessage("<font color='red'>密码不相同</font>", false)); } if(name.equals("") || name == null){ errors.add("name", new ActionMessage("<font color='red'>用户名不能为空</font>", false)); } return errors; } /** * Method reset * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } /** * Returns the password2. * @return String */ public String getPassword2() { return password2; } /** * Set the password2. * @param password2 The password2 to set */ public void setPassword2(String password2) { this.password2 = password2; } /** * Returns the name. * @return String */ public String getName() { return name; } /** * Set the name. * @param name The name to set */ public void setName(String name) { this.name = name; } /** * Returns the password. * @return String */ public String getPassword() { return password; } /** * Set the password. * @param password The password to set */ public void setPassword(String password) { this.password = password; } }
//注册成功进行跳转的action /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.fenghuo.struts.web.actions; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.fenghuo.struts.web.forms.UserForm; /** * MyEclipse Struts * Creation date: 10-07-2012 * * XDoclet definition: * @struts.action path="/UserRegister" name="userForm" scope="request" validate="true" * @struts.action-forward name="main.jsp" path="/WEB-INF/jsp/main.jsp" redirect="true" */ public class UserMain extends Action { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward("success"); } }
<!--主页面--> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>主界面</title> </head> <body> <br> ${userForm.name } 欢迎来到主界面! </body> </html>
<!--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="userForm" type="com.fenghuo.struts.web.forms.UserForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <!--注释部分内容用不到 <action path="/Test" type="com.fenghuo.struts.test.Test"> <forward name="success" path="/WEB-INF/jsp/test.jsp"></forward> </action> --> <action attribute="userForm" name="userForm" path="/UserMain" scope="request" type="com.fenghuo.struts.web.actions.UserMain" input="/WEB-INF/jsp/register.jsp"> <set-property property="cancellable" value="true" /> <forward name="success" path="/WEB-INF/jsp/main.jsp"/> </action> <action path="/UserRegUI" type="com.fenghuo.struts.web.actions.UserRegUI"> <set-property property="cancellable" value="true" /> <forward name="success" path="/WEB-INF/jsp/register.jsp" /> </action> </action-mappings> <!-- <message-resources parameter="com.fenghuo.ApplicationResources" /> --> </struts-config>
1.用户名为空时报错,返回注册页面
2.密码不相同是报错,返回。用户名回显。
3.用户名为空,且密码不相同。
4.注册,登陆成功。
在struts中,将数据封装成FormBean时会调用里面的validate方法,因此可以将数据的校验。放在这个方法中。这个方法会返回错误,我们可以用struts提供的标签显示相应的错误。FormBean会自动封装。默认存放到Session域中。