Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NotNull注释消息未显示在Spring MVC中_Java_Spring Mvc_Jsp - Fatal编程技术网

Java NotNull注释消息未显示在Spring MVC中

Java NotNull注释消息未显示在Spring MVC中,java,spring-mvc,jsp,Java,Spring Mvc,Jsp,在这里,我创建了一个小型客户项目 如果验证失败,则会打印一条自定义消息,@NotNull注释工作正常,但验证失败时不会显示其消息 Customer.java package com.luv2code.springdemo.mvc; 导入javax.validation.constraints.Max; 导入javax.validation.constraints.Min; 导入javax.validation.constraints.NotNull; 导入javax.validation.con

在这里,我创建了一个小型客户项目
如果验证失败,则会打印一条自定义消息,
@NotNull
注释工作正常,但验证失败时不会显示其消息

Customer.java

package com.luv2code.springdemo.mvc;
导入javax.validation.constraints.Max;
导入javax.validation.constraints.Min;
导入javax.validation.constraints.NotNull;
导入javax.validation.constraints.Pattern;
导入javax.validation.constraints.Size;
公共类客户{
@NotNull(message=“输入邮政编码”)
@模式(regexp=“^[a-zA-Z0-9]{5}”,message=“仅允许5个字符/数字”)
专用字符串后代码;
@最小值(值=0,消息=“必须大于或等于零”)
@最大值(值=10,message=“必须小于10”)
私家车通行证;
私有字符串名;
@NotNull(message=“is required”)
@尺寸(最小值=1)
私有字符串LastName;
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
名字=名字;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串lastName){
LastName=LastName;
}
public int getFreePass(){
返回通行证;
}
公共无效设置自由通行证(int自由通行证){
这个。通行证=通行证;
}
公共字符串getPostalCode(){
返回后代码;
}
公共无效setPostalCode(字符串postalCode){
PostalCode=PostalCode;
}
}
CustomerController.java

package com.luv2code.springdemo.mvc;
导入javax.validation.Valid;
导入org.springframework.beans.propertyeditors.StringTrimmerEditor;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.validation.BindingResult;
导入org.springframework.web.bind.WebDataBinder;
导入org.springframework.web.bind.annotation.InitBinder;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
@控制器
@请求映射(“/customer”)
公共类客户控制器{
@InitBinder
public void initBinder(WebDataBinder-dataBinder){
StringTrimmerEditor修剪器=新建StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class,trimmer);
}
@请求映射(“/showForm”)
公共字符串显示窗体(模型){
model.addAttribute(“客户”,新客户());
返回“客户表格”;
}
@请求映射(“/processForm”)
公共字符串processForm(@Valid@modeldattribute(“customer”)customer客户,BindingResult绑定){
if(bind.hasErrors()){
System.out.println(“返回旧页”);
返回“客户表格”;
}否则{
返回“客户确认”;
}
}
}
客户表单.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Customer is confirmed</title>
</head>
<body>
    The customer is confirmed : ${customer.firstName} ${customer.lastName}
    <br>
    No. of Free Passes available to the customer : ${customer.freePasses}
    <br>
    Postal Code : ${customer.postalCode}
</body>
</html>

客户登记表
.错误{颜色:红色}
名字:


姓氏*:

免费通行证:

邮政编码:

客户确认.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Customer is confirmed</title>
</head>
<body>
    The customer is confirmed : ${customer.firstName} ${customer.lastName}
    <br>
    No. of Free Passes available to the customer : ${customer.freePasses}
    <br>
    Postal Code : ${customer.postalCode}
</body>
</html>

客户确认
已确认客户:${customer.firstName}${customer.lastName}

客户可用的免费通行证数量:${customer.freePasses}
邮政编码:${customer.postalCode}