struts2学习之五(最基本的)输入校验

一、register.jsp注册页面

//以下第一两句 的作用是输出struts中的filderror中的错误信息!
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:fielderror></s:fielderror>

<body>
		<s:fielderror></s:fielderror>
		<form action="register.action" method="post">

			<table align="center" width="40%" border="1">

				<tr>

					<td>
						username
					</td>

					<td>
						<input type="text" name="username" value=${username}>
					</td>
				</tr>

				<tr>
					<td>
						password
					</td>

					<td>
						<input type="password" name="password">
					</td>
				</tr>


				<tr>
					<td>
						re-password
					</td>

					<td>
						<input type="password" name="repassword">
					</td>
				</tr>

				<tr>
					<td>
						age
					</td>

					<td>
						<input type="text" name="age" value=${age}>
					</td>
				</tr>

				<tr>
					<td>
						birthday
					</td>

					<td>
						<input type="text" name="birthday" value=${birthday}>
					</td>
				</tr>

				<tr>
					<td>
						graduation
					</td>

					<td>
						<input type="text" name="graduation"value=${graduation}>					</td>
				</tr>

				<tr>
					<td>
						<input type="submit" value=" submit ">
					</td>

					<td>
						<input type="reset" value=" reset ">
					</td>
				</tr>
			</table>
		</form>


	</body>
</html>

 二、success.jsp成功返回页面

<body>
  <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>
 
  </body>

 

 

 三、struts.xml

		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
                       <!--一旦校验错误的话,struts会转到input指向的页面-->
			<result name="input">/register.jsp</result>
		</action>

 

四、输入校验RegisterAction.java  (extends 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()
	{
		System.out.println("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","re-password 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");
			}
		}
		
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值