Spring MVC 使用JSR303校验表单

导入JSR303相关JAR包

Spring MVC 使用JSR303校验表单

创建registerForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<!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>测试JSR 303</title>
</head>
<body>
<h3>注册页面</h3>
<form:form modelAttribute="user" method="post" action="login" >
	<table>
		<tr>
			<td>登录名:</td>
			<td><form:input path="loginname"/></td>
			<td><form:errors path="loginname" cssStyle= "color:red"/></td>
		</tr>
		<tr>
			<td>密码:</td>
			<td><form:input path="password"/></td>
			<td><form:errors path="password" cssStyle= "color:red"/></td>
		</tr>
		<tr>
			<td>用户名:</td>
			<td><form:input path="username"/></td>
			<td><form:errors path="username" cssStyle= "color:red"/></td>
		</tr>
		<tr>
			<td>年龄:</td>
			<td><form:input path="age"/></td>
			<td><form:errors path="age" cssStyle= "color:red"/></td>
		</tr>
		<tr>
			<td>邮箱:</td>
			<td><form:input path="email"/></td>
			<td><form:errors path="email" cssStyle= "color:red"/></td>
		</tr>
		<tr>
			<td>生日:</td>
			<td><form:input path="birthday"/></td>
			<td><form:errors path="birthday" cssStyle= "color:red"/></td>
		</tr>
		<tr>
			<td><input type="submit" value="提交"/></td>
		</tr>
	</table>
</form:form>
</body>
</html>

创建success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>测试JSR 303</title>
</head>
<body>
<h3>测试JSR 303</h3>
登录名:${requestScope.user.loginname }<br>
密码:${requestScope.user.password }<br>
用户名:${requestScope.user.username }<br>
年龄:${requestScope.user.age }<br>
邮箱:${requestScope.user.email }<br>
生日:<fmt:formatDate value="${requestScope.user.birthday}" pattern="yyyy年MM月dd日"/><br>
</body>
</html>

创建User

package com.po;
import java.util.Date;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
public class User {
	@NotBlank
	private String loginname;	
	@NotBlank
	@Length(min=6,max=8)
	private String password;	
	@NotBlank
	private String username;	
	@Range(min=15, max=60)
	private int age;	
	@Email
	private String email;	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Past
	private Date birthday;	
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "User [loginname=" + loginname + ", password=" + password
				+ ", email=" + email + ", username="
				+ username + ", birthDate=" + birthday + "]";
	}
}

创建UserController

package com.controller;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.User;
@Controller
public class UserController {
	@RequestMapping(value="/registerForm")
	 public String registerForm(Model model){
		User user = new User();
		model.addAttribute("user",user);
		// 跳转到注册页面
		return "registerForm";
	}
	 
	// 数据校验使用@Valid,后面跟着Errors对象保存校验信息
	 @RequestMapping(value="/login")
	 public String login(
			 @Valid @ModelAttribute  User user,
			 Errors  errors,
			 Model model) {
		 System.out.println(user);
		 if(errors.hasErrors()){
			 return "registerForm";
		 }
		 model.addAttribute("user", user);
	     return "success";
	 }
}

配置springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 使用注解驱动 -->
<mvc:annotation-driven />
<!-- 定义扫描的包 -->
<context:component-scan base-package="com.*" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/view/"></property>
	<property name="suffix" value=".jsp" />
</bean>
</beans>

启动Tomcat访问以下地址:http://localhost:8080/jackson/registerForm

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云淡风轻58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值