0

This is my customer.java class using for as 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 validator doesn't work. my code run properly but doesn't show any error I am attaching file structure

following libraries are being used

hibernate version 6.0.2

spring version 5.0.6 
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85

1 Answers1

0

The code looks fine. Reading your question, it seems that you might be a little confused between these two:

Hibernate ORM: Implementation of JPA

Hibernate Validator: Implementation of Bean Validation

So, for Bean Validation to work, you need to add Hibernate Validator in classpath. Means simply add it in dependencies of your build.gradle/pom.xml i.e. the build script of your build tool.

Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
  • yes dear i add all jars manually hibernate validator and springs jars common-logging jars and etc i'm following by video lectures but i see same condition errors are not showing so that's why i am confused.. – Zeeshan Memon Jun 11 '18 at 19:52
  • @ZeeshanMemon: Your code seems okay. Please check if the jar files that you added have been added properly. – Minar Mahmud Jun 13 '18 at 15:13