【问题标题】:Why is the error message not getting displayed?为什么不显示错误消息?
【发布时间】:2021-03-04 01:24:56
【问题描述】:

尝试使用注释 @NotNull @NotBlank 验证表单,但网页中未显示任何消息
_@ 注释不起作用,因为页面上没有显示任何验证_
检查表单验证

模型类用户

模型类

package com.cognizant.bmi.bean;


import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


public class User {

    private int userId;
    @NotBlank(message="Name is required")
    private String name;
    @NotBlank(message="phoneNumber is required")
    private String phoneNumber;
    @NotNull(message="Height is required")
    @Min(value=0,message="Height should be >0")
    private Integer height;
    @NotNull(message="Weight is required")
    @Min(value=0,message="Weight should be >0")
    private Double weight;
    private String genderType;
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public Integer getHeight() {
        return height;
    }
    public void setHeight(Integer height) {
        this.height = height;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }
    public String getGenderType() {
        return genderType;
    }
    public void setGenderType(String genderType) {
        this.genderType = genderType;
    }
    @Override
    public String toString() {
        return "User [userId=" + userId + ", name=" + name + ", phoneNumber=" + phoneNumber + ", height=" + height
                + ", weight=" + weight + ", genderType=" + genderType + "]";
    }

    
    

}

表单在需要验证的JSP中定义

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>BMI Calculator</title>
</head>
<body>
    <br>
    <br>
    <h1 id="heading">BMI Calculator</h1>
    <table>
    <caption style="font-weight: bolder;">Body Mass Index Calculator Form</caption>
    
    <sf:form action="getBMI" modelAttribute="user" method="GET" name="form">
            <tr>
                <td>Enter Name:</td> 
                <td><sf:input path="name" id="name"  required="true"
                        name="name" /> <sf:errors style="color:red" path="name" ></sf:errors></td>
            </tr>
                <tr>
                <td>Phone Number:</td>
                <td><sf:input path="phoneNumber" id="phoneNumber" required="true"
                        name="phoneNumber" /> <sf:errors  style="color:red" path="phoneNumber"></sf:errors></td>
            </tr>
                <tr>
                <td>Height in CM:</td>
                <td><sf:input path="height" id="height" required="true"
                        name="height" /> <sf:errors style="color:red" path="height" ></sf:errors></td>
            </tr>
                
                <tr>
                <td>Weight in KG:</td>
                <td><sf:input path="weight" id="weight" required="true"
                        name="weight" /> <sf:errors style="color:red" path="weight" ></sf:errors></td>
            </tr>
            
            <tr>
                <td>Gender:</td>
                <td><sf:select path="genderType" items="${genderList}"
                        id="genderType" name="genderType">
                        <sf:errors path="genderType"></sf:errors>
                    </sf:select><br></td>
            </tr>
            <tr>
                <td><input type="submit" id="submit" name="calculateBMI"
                    value="CalculateBMI"></td>
                <td><input type="reset" id="Clear" name="Clear" value="Clear"></td>
            </tr>
        </sf:form>
    </table>
    ${status}
</body>
</html>

控制器类

控制器

package com.cognizant.bmi.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cognizant.bmi.bean.User;
import com.cognizant.bmi.service.BMIService;

@Controller
public class BMIController {
    @Autowired
private BMIService bmiService;
    @RequestMapping(value = "/bmiForm", method = RequestMethod.GET)
    public String showBMIForm(@ModelAttribute("user") User user) {
        return "bmiCalculatorForm";
    }
    @RequestMapping(value = "/getBMI", method = RequestMethod.GET)
    public String getBMIStatus(@ModelAttribute("user") @Valid User user, BindingResult result, ModelMap map) {
        if (result.hasErrors())
        {
            return "bmiCalculatorForm";
        }
        User user1=bmiService.addUserDetails(user);
        double bmi=bmiService.calculateBMI(user);
        String bmiStatus=bmiService.getBMIStatus(bmi);
        map.put("user.userId",user.getUserId());
        map.put("user.name", user.getName());
        map.put("user.height",user.getHeight());
        map.put("user.weight", user.getWeight());
        map.put("bmi", bmi);
        map.put("bmiStatus",bmiStatus);
    
        
        return "bmiStatus";

    }
    @ModelAttribute("genderList")
public List<String> populateGenderList() {
    return bmiService.genderType;
    }
}

【问题讨论】:

  • 你试过@Validated而不是@Valid吗?
  • @Eniss 没有变化

标签: java spring-boot model-view-controller


【解决方案1】:

问题是我在 JSP 上使用了验证以及使用 @Annotation 进行验证

从已解决的 jsp 问题中删除了“Required='true'”,现在它可以正常工作了。
感谢@Eniss 的即时回复

工作 JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>BMI Calculator</title>
</head>
<body>
    <br>
    <br>
    <h1 id="heading">BMI Calculator</h1>
    <table>
    <caption style="font-weight: bolder;">Body Mass Index Calculator Form</caption>
    
    <sf:form action="getBMI" modelAttribute="user" method="GET" name="form">
            <tr>
                <td>Enter Name:</td> 
                <td><sf:input path="name" id="name"  
                        name="name" /> <sf:errors style="color:red" path="name" ></sf:errors></td>
            </tr>
                <tr>
                <td>Phone Number:</td>
                <td><sf:input path="phoneNumber" id="phoneNumber" 
                        name="phoneNumber" /> <sf:errors  style="color:red" path="phoneNumber"></sf:errors></td>
            </tr>
                <tr>
                <td>Height in CM:</td>
                <td><sf:input path="height" id="height" 
                        name="height" /> <sf:errors style="color:red" path="height" ></sf:errors></td>
            </tr>
                
                <tr>
                <td>Weight in KG:</td>
                <td><sf:input path="weight" id="weight" 
                        name="weight" /> <sf:errors style="color:red" path="weight" ></sf:errors></td>
            </tr>
            
            <tr>
                <td>Gender:</td>
                <td><sf:select path="genderType" items="${genderList}"
                        id="genderType" name="genderType">
                        <sf:errors path="genderType"></sf:errors>
                    </sf:select><br></td>
            </tr>
            <tr>
                <td><input type="submit" id="submit" name="calculateBMI"
                    value="CalculateBMI"></td>
                <td><input type="reset" id="Clear" name="Clear" value="Clear"></td>
            </tr>
        </sf:form>
    </table>
    ${status}
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-30
    • 2014-01-03
    • 2023-03-23
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多