spring-mvc 表单验证

1.首先pom.xml需要引用两个jar包(spring-mvc需要的包这里就不赘述了),如下:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.1.2.Final</version>
</dependency>
View Code

2.定义一个需要输入的对象Product

package cn.duanjt.pojo;

import java.util.Date;

import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;

public class Product {
    //定义验证规则
    @DecimalMin("0.01")
    private int price;
    @Size(max=10,min=1)
    private String name;
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date time;
    private String email;
    
    /** getter and setter,这里为了简洁就这么写了 **/
    
}
View Code

3.定义控制器

package cn.duanjt;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.google.gson.Gson;

import cn.duanjt.pojo.Product;
import cn.duanjt.pojo.Student;

@Controller
@RequestMapping("/student")
public class StudentController {

    //注意:@Valid
    @RequestMapping("/save")
    public ModelAndView save(@Valid Product product, Errors errors) {
        if (errors.hasErrors()) {
            System.out.println("验证失败");
            List<FieldError> fieldErrors = errors.getFieldErrors();
            for (FieldError fe : fieldErrors) {
                System.out.println(fe.getField() + "\t" + fe.getDefaultMessage());//输出错误消息
            }
        } else {
            System.out.println("验证成功");
        }

        System.out.println(new Gson().toJson(product));//输出获取的对象

        ModelAndView mv = new ModelAndView();
        mv.addObject(product);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

}
View Code

4.jsp代码测试如下:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!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>首页</title>
</head>
<body>
    <form action="student/save.do" method="post">
        <p>
            单价:<input type="text" name="price" value="1" />
        </p>
        <p>
            名称:<input type="text" name="name" value="茄子" />
        </p>
        <p>
            日期:<input type="text" name="time" value="2018-07-21" />
        </p>
        <p>
            邮件:<input type="text" name="email" />
        </p>
        <p>
            <input type="submit" value="提交"/>
        </p>
    </form>
</body>
</html>
View Code

 

最后附上验证规则:

 

posted @ 2018-08-07 16:43  段江涛IT  阅读(131)  评论(0编辑  收藏  举报
页脚HTML代码