Struts温习(5)--ActionForm的validator方法验证

现在这个验证呢,跟第一篇文章的那个还有所不一样,之前的是配置资源文件和配置验证文件的做法,这次呢是在ActionForm里面的一个validator方法中纯手工写代码的方式,实现的登录验证效果一样

login.jsp

 

<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Struts第一个应用:登录验证</title>
  </head>
  
  <body>
    <h3>Struts第一个应用:登录验证</h3><hr/>
    <ul style="color:red">
    <html:messages id="msg">
    	<li>${msg}</li>
    </html:messages>
    </ul>
    <form id="myForm" name="myForm" action="login.do" method="post">
    	<table border="1" width="600px">
    		<tr>
    			<td>登录名</td>
    			<td><input type="text" name="loginname"/></td>
    		</tr>
    		<tr>
    			<td>密码</td>
    			<td><input type="password" name="pwd"/></td>
    		</tr>
    		<tr>
    			<td colspan="2" align="center">
    				<input type="submit" value="提交"/>&nbsp;
    				<input type="reset" value="重置"/>
    			</td>
    		</tr>
    	</table>
    </form>
    
  </body>
</html>

 

 LoginForm.java

 

package com.javacrazyer.web.formbean;

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 LoginForm extends ActionForm {
	
	private static final long serialVersionUID = 6619272689058619128L;

	private String loginname;
	
	private String pwd;
	
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		System.out.println("*******调用LoginForm的reset方法");
		this.pwd = null;
		this.loginname = null;
		
		super.reset(mapping, request);
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		System.out.println("调用LoginForm的validate方法------");
		//如果本方法返回null或ActionErrors中没有存放任何对象,表示验证通过。否则验证失败,跳转到对应<action>元素中input属性指定的URL
		ActionErrors errors = new ActionErrors();
		if(this.loginname == null || "".equals(this.loginname)){
			ActionMessage msg = new ActionMessage("用户名不能为空",false);
			errors.add(ActionErrors.GLOBAL_MESSAGE, msg);
		}
		
		if(this.pwd == null || "".equals(this.pwd)){
			ActionMessage msg = new ActionMessage("密码不能为空",false);
			errors.add(ActionErrors.GLOBAL_MESSAGE, msg);
		}
		
		return errors;
	}

	public LoginForm(){
		System.out.println("调用默认构造方法创建出LoginForm的一个实例");
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
		System.out.println("调用setLoginname()方法");
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
		System.out.println("调用setPwd()方法");
	}
}

 WEB-INF/struts-config.xml

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

<!-- ================================================ Form Bean Definitions -->
    <form-beans>
		<form-bean name="loginForm" type="com.javacrazyer.web.formbean.LoginForm"/>
		<form-bean name="regForm" type="com.javacrazyer.web.formbean.RegisterForm"/>
    </form-beans>

<!-- =========================================== Action Mapping Definitions -->
    <action-mappings>
	<action path="/login" name="loginForm" type="com.javacrazyer.web.action.LoginAction" validate="true"		
		   input="/index.jsp">
			<forward name="succ" path="/login_success.jsp"/>
			<forward name="failure" path="/login_failure.jsp"/>
		</action>
    </action-mappings>
</struts-config>

 这里的示例就不要用到validator.xml和validator-rules.xml了,也不需要资源文件

 关于web.xml的配置、LoginAction和其他JSP页面都与第一篇文章中的一样,这里就不写了,实现的效果也就是第一篇文章中所展示的效果

 

你可能感兴趣的:(apache,Web,jsp,bean,struts)