struts2输入校验

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'regist.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  		<h1 style = "color:red;">用户注册</h1>
  		<s:actionerror/>
  		----------------------------
  		<s:fielderror cssStyle="color:red;"/>
  		<!-- 
  		<form action = "register.action">
  			<table>
  			<tr><td>username:</td><td><input type = "text" name = "username" size = "20"></td></tr>
  			<tr><td>password:</td><td><input type = "password" name = "password" size = "20" /></td></tr>
  			<tr><td>repassword:</td><td><input type = "text" name = "repassword" size = "20" /></td></tr>
  			<tr><td>age:</td><td><input type = "text" name = "age" size = "20" /></td></tr>
  			<tr><td>birthday:</td><td><input type = "text" name = "birthday" size = "20" /></td></tr>
  			<tr><td>graduate:</td><td><input type = "text" name = "graduation" size = "20"/></td></tr>
  			<tr><td colspan="2"><input type = "submit" value = "submit" /></td></tr>
  			</table>
  		</form>	
  		-->
  		<s:form action = "register.action" theme="simple">
  		<s:textfield name = "username" label = "username"></s:textfield>
  		<s:password name = "password" label = "password"></s:password>
  		<s:textfield name = "repassword" label = "repassword"></s:textfield>
  		<s:textfield name = "age" label = "age"></s:textfield>
  		<s:textfield name = "birthday" label = "birthday"></s:textfield>
  		<s:textfield name = "graduation" label = "graduation"></s:textfield>
  		<s:submit name = "submit"></s:submit>
  		</s:form>
  </body>
</html>

package action;

import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{
	private String username;
	
	private String password;
	
	private String repassword;
	
	private int age;
	
	private Date birthday;
	
	private Date graduation;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getGraduation() {
		return graduation;
	}

	public void setGraduation(Date graduation) {
		this.graduation = graduation;
	}
	
	@Override
	public String execute() throws Exception {
		
		System.out.println("execute invoked ");
		return SUCCESS;
	}
	
	@Override
	public void validate() {
		if (null == username || username.length() < 4 || username.length() > 6) {
			this.addActionError("username invalid");
			this.addFieldError("username", "invalid");
		}
		if (null == password || password.length() < 4 || password.length() > 6) {
			this.addActionError("password invalid");
		}else if (null == repassword || repassword.length() < 4 || repassword.length() > 6) {
			this.addActionError("repassword invalid");
		}else if (!password.equals(repassword)) {
			this.addActionError("the password not the same repassword");
		}
		if (age < 10 || age > 50) {
			this.addActionError("age invalid!");
		}
		if (null == birthday) {
			this.addActionError("birthday invalid");	
		}
		if (null == graduation) {
			this.addActionError("graduation invalid");
		}
		if (null != birthday && null != graduation) {
			Calendar c1 = Calendar.getInstance();
			c1.setTime(birthday);
			Calendar c2 = Calendar.getInstance();
			c2.setTime(graduation);
			if (c1.after(c2)) {
				this.addActionError("birthday should be before graduation");
			}
		}
	}
}

result:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name = "sss" extends="struts-default" namespace="/">

<action name="UserAction2" class="action.UserAction2">
</action>
<action name = "login" class="action.LoginAction" method="invoke">
</action>
<action name = "register" class = "action.RegisterAction">
<result name = "success">/registerResult.jsp</result>
<result name = "input">/regist.jsp</result>
</action>
</package>
</struts>
执行流程:

1:首先进行类型转换

2:然后进行输入校验(执行validate方法)

3:如果在上述过程中出现了任何错误,都不会再去执行execute方法会转向struts.xml中action的名为input的result所对应的页面。如果在进行类型转换的过程中出现了错误,struts2会把错误放在addFieldError中addActionError中都是手动注入值。而addFieldError中struts会自动设置。ActionSupport类的addActionError()方法的实现:首先会创建一个Arraylist对象,然后将错误的消息添加到ArrayList对象中。当调用getActionErrors()方法返回的Action级别的错误信息时,返回的是集合的一个副本而不是集合本身,因此对集合副本调用clear方法清除的依旧是副本中的元素,而非原集合中的元素,此时原集合中的内容没有收到任何的影响。换句话来说:Action级别的错误信息列表对开发者来说是只读的。Field Error级别的错误的信息底层是用LinkedHashMap实现的。该map的key是string类型的,value是List<String>类型的。这就表示一个Field Name 可以对应多条错误信息。这个信息放在List<String>中。

Action中自定义方法的输入校验,对于通过action的method属性所指定的自定义方法,其对应的自定义输入校验方法名validateMyExecute(我们的方法名为:myExecute),本质上是使用反射实现的。当在Action中指定了自定义的execute方法时,首先会执行自定义的execute方法所对应的输入校验方法,然后在去执行validate方法,执行完后如果出现任何的错误都不会再去执行自定义的execute方法,流程转向了input这个名称对应的页面上。(不管使用execute方法,还是自定义的execute方法)validate方法都会执行的。

自定义Field级别的错误提示消息

1)新建一个以Action名命名的properties文件,如RegisterAction.properties.

2)然后在该属性文件中指定每一个出错字段的错误消息Invalid.fieldvalue.birthday=birthday invalid!!使用struts2的校验框架(有效的xml文件):具体来说分为字段优先校验器与校验器优先字段。


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值