【问题标题】:Spring and Hibernate Form Validation Error not showSpring 和 Hibernate 表单验证错误未显示
【发布时间】:2018-06-10 15:43:01
【问题描述】:

这是我的 customer.java 类,使用 for 作为 bean

package com.zeeshan.form;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Customer {

    private String firstName;

    @NotNull(message="is required")
    @Size(min=1)
    private String lastName;


    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;
    }


}

CustomerController.java

package com.zeeshan.form;

import javax.validation.Valid;

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;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @RequestMapping("/showForm")
    public String showFormModel(Model theModel) {
        theModel.addAttribute("customer", new Customer());
        return "customer-form";
    }

    @RequestMapping("/processForm")
    public String processForm(@ModelAttribute("customer") @Valid Customer theCustomer, BindingResult theBindingresult) {

        if(theBindingresult.hasErrors()) {
            return "customer-form";
        }
        else {

            return "customer-confirmation";
        }

    }
}

customer-form.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<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>
    <h2>Customer Registeration Form</h2>
    <form:form action="processForm" modelAttribute="customer">
        First Name : <form:input path="firstName"/>
        <br><br>

        Last Name (*) : <form:input path="lastName"/>
        <form:errors path="lastName" cssClass="error" />
        <br><br>
        <input type="submit" value="Submit" />
    </form:form>
</body>
</html>

休眠验证器不起作用。 我的代码运行正常但没有显示任何错误 我正在附加文件结构

正在使用以下库

hibernate version 6.0.2

spring version 5.0.6 

【问题讨论】:

  • 您是否在绑定结果中遇到任何绑定错误?
  • 不,我没有收到任何绑定结果错误,这就是我感到困惑的原因
  • 那么可能是缺少hibernate验证器jar..请检查maven依赖项中的jar
  • 我手动添加所有罐子,然后是视频讲座,但我仍然得到相同的条件

标签: spring forms validation bean-validation hibernate-validator


【解决方案1】:

代码看起来不错。阅读您的问题,您似乎对这两者有些困惑:

Hibernate ORM:实现JPA

Hibernate Validator:实现Bean Validation

因此,要使Bean Validation 工作,您需要在类路径中添加Hibernate Validator。意味着只需将其添加到您的 build.gradle/pom.xml 的依赖项中,即构建工具的构建脚本。

【讨论】:

  • 是的,亲爱的,我手动添加了所有 jars 休眠验证器和 springs jars common-logging jars 等等,我正在关注视频讲座,但我看到相同的条件错误没有显示,所以这就是我感到困惑的原因..
  • @ZeeshanMemon:你的代码看起来不错。请检查您添加的jar文件是否添加正确。
猜你喜欢
  • 1970-01-01
  • 2011-01-28
  • 2018-04-19
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
相关资源
最近更新 更多