数据校验二 JR03校验

下面两种下载哪个都可以

1.https://sourceforge.net/projects/hibernate/files/hibernate-validator/    下载Hibernate Validator

2.http://bval.apache.org/downloads.html 下载Apache bval

 

1.JavaBean

package com.bean;

import java.util.Date;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;

@SuppressWarnings("deprecation")
public class User {
 @NotBlank(message="登录名不能为空")
 private String loginname;
 @NotBlank
 @Length(min=6,max=20)
 private String password;
 @NotBlank
 private String username;
 @Range(min=6,max=150)
 private int age;
 @Email
 private String email;
 @DateTimeFormat(pattern="yyyy-MM-dd")
 private Date birthday;
 @Pattern(regexp="1[34578][0-9]{9}")
 private String phone;
 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 String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 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 Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 public String getPhone() {
  return phone;
 }
 public void setPhone(String phone) {
  this.phone = phone;
 }

}

 

 

2.前端页面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>电话:</td>
   <td><form:input path="phone"/></td>
   <td><form:errors path="phone" cssStyle= "color:red"/></td>
  </tr>
  <tr>
   <td><input type="submit" value="提交"/></td>
  </tr>
 </table>
</form:form>
</body>
</html>

3.前端页面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>
电话:${requestScope.user.phone }<br>
</body>
</html>

4.配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd    
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
       
    <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
     如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
    <context:component-scan base-package="com.controller"/>
    <mvc:annotation-driven />
   
    <!-- 视图解析器  -->
     <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <!-- 后缀 -->
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
   
</beans>

5.Controller类


 

package com.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.User;

@Controller
public class UserController {
 @RequestMapping("/{registerForm}")
 public String registerForm(@PathVariable String registerForm, ModelMap mm){
  mm.addAttribute("user", new User()) ;
  return registerForm;
 }
 
 @RequestMapping("/login")
 //@Valid进行校验
 public String login(@Valid User user, Errors errors, ModelMap mm){
  if(errors.hasErrors())
   return "registerForm";
  mm.addAttribute("user", user);
  return "success";
 }

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值