`
zhaolianyang
  • 浏览: 94670 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2学习笔记五(第5讲.Struts2的输入校验)

阅读更多
Struts2的输入校验
数据校验是建立在类型转换的基础之上的。
一、在WebRoot根目录下创建一个类似于注册的未使用struts2的页面register.jsp(是用html表单的形式写的):
<body>
	
	<form action="register" method="post">
		<table align="center" width="40%" border="1">
			<tr>
				<td>username:</td>
				<td><input type="text" name="username"></td>
			</tr>
			<tr>
				<td>password:</td>
				<td><input type="password" name="password"></td>
			</tr>
			<tr>
				<td>repassword:</td>
				<td><input type="password" name="repassword"></td>
			</tr>
			<tr>
				<td>age:</td>
				<td><input type="text" name="age"></td>
			</tr>
			<tr>
				<td>birthday:</td>
				<td><input type="text" name="birthday"></td>
			</tr>
			<tr>
				<td>graduation:</td>
				<td><input type="text" name="graduation"></td>
			</tr>
			<tr>
				<td><input type="submit" value=" submit "></td>
				<td><input type="reset" value=" reset "></td>
			</tr>
		</table>
	</form>
	
  </body>

二、在包com.test.action中创建一个处理register表单的Action类RegisterAction.java继承ActionSupport类:
package com.test.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

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

	public String getUsernme() {
		return usernme;
	}

	public void setUsernme(String usernme) {
		this.usernme = usernme;
	}

	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 {
		
		return SUCCESS;
	}
}

三、创建一个输出的success.jsp页面:
  <body>
	
	<form action="register.action" method="post">
		<table align="center" width="40%" border="1">
			<tr>
				<td>username:</td>
				<td>${requestScope.username}</td>
			</tr>
			<tr>
				<td>password:</td>
				<td>${requestScope.password }</td>
			</tr>
			<tr>
				<td>age:</td>
				<td>${requestScope.age }</td>
			</tr>
			<tr>
				<td>birthday:</td>
				<td>${requestScope.birthday }</td>
			</tr>
			<tr>
				<td>graduation:</td>
				<td>${requestScope.graduation }</td>
			</tr>
		</table>
	</form>
	
  </body>


四、在struts.xml中创建并配置Action的属性:
<action name="register" class="com.test.action.RegisterAction">
		<result name="success">/success.jsp</result>
		<result name="input">/register.jsp</result>
	</action>

五、继续修改RegisterAction.java类,添加验证的信息:
package com.test.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 {
		
		return SUCCESS;
	}

	@Override
	public void validate() {
		
		if(null == username || username.length() < 6 || username.length() > 10){
			this.addFieldError("username", "username invalid");
		}
		if(null == password || password.length() <6 || password.length() > 10){
			this.addFieldError("password", "password invalid");
		}
		else if(null == repassword || repassword.length() < 6 || repassword.length() > 10){
			this.addFieldError("repassword", "repassword invalid");
		}
		else if(!password.equals(repassword)){
			this.addFieldError("password", "two passwords not the same");
		}
		if(age < 1 || age > 150){
			this.addFieldError("age", "age invalid");
		}
		if(null == birthday){
			this.addFieldError("birthday", "birthday invalid");
		}
		if(null == graduation){
			this.addFieldError("graduation", "graduation invalid");
		}
		if(null != birthday && null != graduation){
			Calendar c1 = Calendar.getInstance();
			c1.setTime(birthday);
			
			Calendar c2 = Calendar.getInstance();
			c2.setTime(graduation);
			
			if(!c1.before(c2)){
				this.addFieldError("birthday", "birthday should be before graduation");
			}
		}
	}
}

演示效果:
未输入时的初页面:

输入全部正确是的页面:

输入全部不正确时的页面:

最终的项目文件:(注意:未完,待续)
  • 大小: 88.7 KB
  • 大小: 78.8 KB
  • 大小: 96.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics