`
whp0731
  • 浏览: 170380 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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");
			}
		}
		
	}
}

 

分享到:
评论

相关推荐

    struts2学习ppt

    掌握Struts2原理、基本配置及...掌握Struts2核心解析、国际化、类型转换、输入校验、OGNL、Struts2标签库 了解AJAX技术支持 掌握文件的上传与下载。 深入理解MVC与Struts之间的关系,并使用基本MVC和Struts进行项目开发

    BBS-struts2课程设计-南工考研论坛

    基本功能: 1.论坛实现基本的用户交流要求,前台浏览,后台管理 2.游客可以浏览帖子,注册为用户才可回复和发帖(自定义拦截器实现) 3.管理员管理后台,帖子信息,回复信息和用户信息 特色功能: (1) BBS是网络中...

    Struts2.1学习笔记

    基于 Struts2.1.8 包括Struts2的基本应用、文件上传、拦截器、输入校验、国际化、OGNL表达式、Struts2标签等内容。

    java web技术开发大全(最全最新)

    《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》内容包括Web客户端技术、JSP/Servlet技术、Struts 2(*、类型转换、输入校验、上传和下载文件、Struts 2的各种标签、对 AJAX的支持等)、Spring...

    毕业设计-基于SSM和EasyUI公司员工管理系统,包含源码、数据库文件、演示视频、运行截图、系统说明等

    技术要点: 1 此系统采用了目前最流行的ssm框架,其中的spingMVC框架相对于struts2框架更灵活,更安全。 2 本项目springMVC框架采用了注解映射器,使用了RESTful风格的url对系统发起http请求,开发更灵活。 3 同时...

    基于SSM+EasyUI公司员工管理系统(源码+运行截图+系统说明+数据库).zip

    1 此系统采用了目前最流行的ssm框架,其中的spingMVC框架相对于struts2框架更灵活,更安全。 2 本项目springMVC框架采用了注解映射器,使用了RESTful风格的url对系统发起http请求,开发更灵活。 3 同时使用了了...

    java web开发技术大全

    《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》内容包括Web客户端技术、JSP/Servlet技术、Struts 2(*、类型转换、输入校验、上传和下载文件、Struts 2的各种标签、对 AJAX的支持等)、Spring...

    基于SSM_EasyUI公司员工管理系统

    1 此系统采用了目前最流行的ssm框架,其中的spingMVC框架相对于struts2框架更灵活,更安全。 2 本项目springMVC框架采用了注解映射器,使用了RESTful风格的url对系统发起http请求,开发更灵活。 3 同时使用了了...

    SSM_EasyUI公司员工信息管理系统源码 SSMGSYGXXGLXT.rar

    1 此系统采用了目前最流行的ssm框架,其中的spingMVC框架相对于struts2框架更灵活,更安全。 2 本项目springMVC框架采用了注解映射器,使用了RESTful风格的url对系统发起http请求,开发更灵活。 3 同时使用了了...

    JSP基于SSM实现的和EasyUI公司员工管理系统毕业源代码+文档说明

    技术要点: 1 此系统采用了目前最流行的ssm框架,其中的spingMVC框架相对于struts2框架更灵活,更安全。 2 本项目springMVC框架采用了注解映射器,使用了RESTful风格的url对系统发起http请求,开发更灵活。 3 同时...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

     国内知名的高端IT技术作家,已出版《Spring 2.0宝典》、《基于J2EE的Ajax宝典》、《轻量级J2EE企业应用实战》、《Struts 2权威指南》、《Ruby On Rails敏捷开发最佳实践》等著作。 目录: 第0章 学习Java...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    在web 2.0应用中,博客(Blog)是web 2.0核心应用中最典型、最流行的代表之一,也是web 2.0技术应用的最直观的表现,是web 2.0精神和理念的具体体现。 1.2. 问题的提出 Blog记载了日常发生的事情和自己的兴趣爱好,把...

Global site tag (gtag.js) - Google Analytics