无法看到Spring MVC验证错误消息

时间:2020-10-06 09:42:23

标签: spring spring-mvc

我是spring MVC的新手。为了练习我的技能,我制作了一个样本表单,其元素通过相应的controller进行填充。现在要检查验证部分,我通过注释使用了所有方法,即@ NotNull,@ Size,@ Valid,@ ModelAttribute和BindingResult对象。以下是我正在使用的文件。我一直在关注每个必要的方面,但不知道为什么我看不到红色的验证消息。

学生班

package com.sikka.springmvcworking.custombeans;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Student {
    private String firstName;
    @NotNull
    @Size(min=1,message="is required")
    private String lastName;
    private String country;
    private String course;
    private String[] knownOs;
    private String[] coutries;
    private Map<String,String> countryMap;
    private Map<String,String> courseMap;
    private Map<String,String> kosMap;
    public Student() {
    }
    public Student(String firstName, String lastName, String country, String course, String[] knownOs, String[] coutries,
            Map<String, String> countryMap, Map<String, String> courseMap, Map<String, String> kosMap) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.country = country;
        this.course = course;
        this.knownOs = knownOs;
        this.coutries = coutries;
        this.countryMap = countryMap;
        this.courseMap = courseMap;
        this.kosMap = kosMap;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public Map<String, String> getCountryMap() {
        return countryMap;
    }
    public void setCountryMap(Map<String, String> countryMap) {
        this.countryMap = countryMap;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    public String[] getKnownOs() {
        return knownOs;
    }
    public void setKnownOs(String[] knownOs) {
        this.knownOs = knownOs;
    }
    public String[] getCoutries() {
        return coutries;
    }
    public void setCoutries(String[] coutries) {
        this.coutries = coutries;
    }
    public Map<String, String> getCourseMap() {
        return courseMap;
    }
    public void setCourseMap(Map<String, String> courseMap) {
        this.courseMap = courseMap;
    }
    public Map<String, String> getKosMap() {
        return kosMap;
    }
    public void setKosMap(Map<String, String> kosMap) {
        this.kosMap = kosMap;
    }
}

学生控制器类

package com.sikka.springmvcworking.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.*;
import javax.validation.Valid;
import com.sikka.springmvcworking.custombeans.Student;
@Controller
@RequestMapping("stu")
public class StudentController {
    @RequestMapping("studentForm")
    public String getStudentForm(Model model) {
        Student student = new Student();
        Map<String,String> countryMap = new LinkedHashMap<String,String>();
        countryMap.put("IN", "INDIA");
        countryMap.put("BR", "BRAZIL");
        countryMap.put("MX", "MEXICO");
        student.setCountryMap(countryMap);
        Map<String,String> courseMap = new LinkedHashMap<String,String>();
        courseMap.put("java", "JAVA");
        courseMap.put(".net", ".NET");
        courseMap.put("php", "PHP");
        courseMap.put("andr", "ANDROID");
        student.setCourseMap(courseMap);
        Map<String,String> kosMap = new LinkedHashMap<String,String>();
        kosMap.put("win", "WINDOWS");
        kosMap.put("lnx", "LINUX");
        kosMap.put("mac", "MACINTOSH");
        student.setKosMap(kosMap);
        model.addAttribute("student", student);
        System.out.println("inside STUDENT-FORM");
        return "STUDENT-FORM";
    }

    @RequestMapping("processStudent")
    public String outputStudent(@Valid @ModelAttribute("student") Student student, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            return "redirect:studentForm";
        }
        return "VIEW-STUDENT";
    }
}

学生表格JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style>
.error {color: red;}
</style>
</head>
<body>
<form:form action="processStudent" modelAttribute="student">
FirstName : <form:input type="text" path="firstName" placeholder="enter your first name"/><br>
LastName : <form:input type="text" path="lastName" placeholder="enter your last name"/>
<form:errors path="lastName" cssClass="error"/><br>
select country : <form:select path="coutries">
<form:options items="${student.countryMap}"/>
</form:select><br>
select course : <form:radiobuttons path="course" items="${student.courseMap}"/><br>
known OS(s) : <form:checkboxes items="${student.kosMap}" path="knownOs"/><br>
<input type="submit" value="submit">
</form:form>
</body>
</html> 

学生提交/查看Jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form"  uri="http://www.springframework.org/tags/form" %>   
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
Student Name : ${student.firstName} ${student.lastName} <br>
Student visited countries:<br>
<c:forEach var="t" items="${student.coutries}">
<li> ${t} </li>
</c:forEach><br>
Known OS by student : <br>
<c:forEach var="t" items="${student.knownOs}">
<li> ${t} </li>
</c:forEach><br>
Student course : ${student.course}<br>
</body>
</html>

每次尝试lastName验证时,我都将其留空。我被重定向到相同的学生表格,但没有错误消息。

enter image description here

有没有办法以这种jsp形式显示验证错误消息。

1 个答案:

答案 0 :(得分:0)

我将学生控制器类更改为以下代码。基本上,我在 processStudent 映射中使用了RedirectAttributes。我还分离出spring表单元素,将其映射到不同的方法。在 studentForm 中,我检查了student是否已经存在(由于 processStudent 映射中的验证过程之后)。请查看代码:

学生控制器类

package com.sikka.springmvcworking.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.*;
import javax.validation.Valid;
import com.sikka.springmvcworking.custombeans.Student;
@Controller
@RequestMapping("stu")
public class StudentController {
    @RequestMapping("studentForm")
    public String getStudentForm(Model model) {
        Student student = new Student();
        student.setCountryMap(getCountryMap());
        student.setCourseMap(getCourseMap());
        student.setKosMap(getKosMap());
         if (!model.containsAttribute("student")) {
             System.out.println("student not there");
                model.addAttribute("student",student);
            }
         else {
             System.out.println("student already there");
         }
        return "STUDENT-FORM";
    }
    @RequestMapping("processStudent")
    public String outputStudent(@Valid @ModelAttribute("student") Student student, BindingResult bindingResult,RedirectAttributes attr) {
        if(bindingResult.hasErrors()) {
            attr.addFlashAttribute("org.springframework.validation.BindingResult.student", bindingResult);
            student.setCountryMap(getCountryMap());
            student.setCourseMap(getCourseMap());
            student.setKosMap(getKosMap());
            attr.addFlashAttribute("student", student);
            return "redirect:studentForm";
        }
        return "VIEW-STUDENT";
    }
    private Map<String,String> getCountryMap(){
        Map<String,String> countryMap = new LinkedHashMap<String,String>();
        countryMap.put("IN", "INDIA");
        countryMap.put("BR", "BRAZIL");
        countryMap.put("MX", "MEXICO");
        return countryMap;
    }
    private Map<String,String> getCourseMap(){
        Map<String,String> courseMap = new LinkedHashMap<String,String>();
        courseMap.put("java", "JAVA");
        courseMap.put(".net", ".NET");
        courseMap.put("php", "PHP");
        courseMap.put("andr", "ANDROID");
        return courseMap;
    }
    private Map<String,String> getKosMap(){
        Map<String,String> kosMap = new LinkedHashMap<String,String>();
        kosMap.put("win", "WINDOWS");
        kosMap.put("lnx", "LINUX");
        kosMap.put("mac", "MACINTOSH");
        return kosMap;
    }
}

以上方法解决了我的问题。我不是spring的专家。这里的任何人都可以更深入地解释。