验证表单时不显示错误消息

fiei3ece  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(186)

因此,我必须使这个应用程序,你有一个鸟类物种的名单,你可以添加新的名单。此表单需要进行一些验证,但由于某些原因,没有显示错误消息。另外,当您正确输入所有输入时,一切都会正常工作,但是当您输入错误时,它应该再次显示相同的页面,但它会返回一个白标签错误。
下面是我的模型类和验证:

public class BirdSpecie {
@NotEmpty(message="This input may not be empty")
private String name;
@NotNull(message="This input may not be empty")
@DecimalMin(value="1250", message="The earliest year of discovery allowed is 1250")
private Integer yearOfDiscovery;

@NotEmpty(message="This input may not be empty")
@Pattern(regexp= "^[A-Z]{1,2}[0-9]{3}$", message="Code should start with one or more capital letters [A-Z] followed by 3 digits")
private String code;

以下是我的控制器方法:

@PostMapping(value="/{locationName}/addBirdSpecie")
public String onSubmit (
        @PathVariable("locationName")String locationName,
        @Valid @ModelAttribute("spottedBirdSpecie") BirdSpecie birdSpecie,
        Model model, BindingResult result)
{
    model.addAttribute("spottedBirdSpecie", birdSpecie);
    BirdSpotLocation birdSpotLocation = spottedBirdService.findByName(locationName).get();
    yearValidation.validate(birdSpecie,result);
    if(result.hasErrors()) {
        return "addSpecie";
    }else {
    birdSpotLocation.increaseBirdSpot(birdSpecie);
    model.addAttribute("location", birdSpotLocation);
    return "birdsList";
    }

}

这是我的jsp文件:

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

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<spring:url value="/css/style.css" var="urlCss"/>
<link rel="stylesheet" href="${urlCss}" type="text/css" />
<title>Create a new bird Specie</title>
</head>
<body>
<h1>Create new bird specie:  </h1>
<form:form method="post" action="addBirdSpecie" modelAttribute="spottedBirdSpecie">
<spring:url value="/birdspotting/" var="showLocation" />
<div>
<p>
Specie: <form:input path="name" size="25"/><br>
<form:errors path="name" cssClass="error"/>
</p>
<p>
Year of discovery: <form:input path="yearOfDiscovery" size="25"/><br>
<form:errors path="yearOfDiscovery" cssClass="error"/>
</p>
<p>
Books of birds code: <form:input path="code" size="25"/><br>
<form:errors path="code" cssClass="error"/>
</p>

<input type="submit" value="Spot new bird"/> 
</div>

</form:form>

</body>
</html>

错误消息在我的控制台中正确显示,但不会显示在网站上。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题