Struts2 输入校验(使用编码方式)

一、Struts输入校验

1、创建register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<!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>Insert title here</title>
</head>
<body>
<form action="register">
    <h2> 用户注册</h2>
    <s:actionerror />
	username: <input type="text" name="username" size="20"><br>
	password: <input type="password" name="password" size="20"><br>
	repassword: <input type="password" name="password" size="20"><br>
	age: <input type="text" name="age" size="20"><br>
	birthday: <input type="text" name="birthday" size="20"><br>
	graduation: <input type="text" name="graduation" size="20"><br>
	
	
	<input type="submit" name="submit" size="20">
	
</form>
</body>
</html>

  

2、struts.xml配置

<action name="register" class="com.example.struts2.RegisterAction">
			<result name="success">/registerResult.jsp</result>
			<result name="input">/register.jsp</result>
</action>

  

3、创建registerResult.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<!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>Insert title here</title>
</head>
<body>

username: <s:property value="username" /><br>
password: <s:property value="password" /><br>
age: <s:property value="age" /><br>
brithday: <s:property value="brithday" /><br>
graduation: <s:property value="graduation" /><br>
</body>
</html>

  

4、创建RegisterAction.java

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() {
		System.out.println("validate invoked");
		//username的长度在4~6
		if(username == null || username.length() < 4 || username.length() > 6){
			this.addActionError("user invalid");
		}
		
		
		if(password == null || password.length() < 4 || password.length() > 6){
			this.addActionError("password invalid");
		}else if(repassword == null || repassword.length() < 4 || repassword.length() > 6){
			this.addActionError("repassword invalid");
		}else if(!password.equals(repassword)){
			this.addActionError("the passwords not the same");
		}
		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 != graduation && null != birthday ){
			Calendar c1 = Calendar.getInstance();
			c1.setTime(birthday);
			
			Calendar c2 = Calendar.getInstance();
			c2.setTime(graduation);
			
			if(c1.before(c2)){
				this.addActionError("birthday shoud be before graduation invalid");
			}
		
		}
	}
	

	
}

  重写validate方法

二、FieldError使用

1、在RegisterAction.java增加FieldError

this.addFieldError("username", "username invalid");

2、在register.jsp增加

 <s:fielderror cssStyle="color:blue"></s:fielderror>

 3、测试

 可以发现,用户名输入11, 将出现上图的错误,并且提交后,刚才输入的字符被清空了。如何不让输入的字符被清空?

三、如何不让输入的字符被清空

	<s:form  action="register" theme="simple">
		<s:textfield name="username" label="username"></s:textfield>
		<s:password name="password" label="password"></s:password>
	    <s:password name="repassword" label="repassword"></s:password>
	    <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 value=""></s:submit>
	</s:form>

  

原文地址:https://www.cnblogs.com/linlf03/p/11530740.html