Spring validation with Javax.validation redirects to 400

1.1k views Asked by At

I am working on a Spring-MVC application, and I want to validate some data. Currently I am able to validate the data with no problems. Only thing is if the data is invalid, I would like to go to another JSP page, which is not happening right now. Instead I get an Apache 400 error, request was sent syntactically incorrect. Can anyone tell me what all is remaining to implement in validation.

Controller :

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String listPersons(Model model) {
        Person person = personService.getCurrentlyAuthenticatedUser();
        if(!(person==null)){
            return "redirect:/canvas/list";
        } else {
            model.addAttribute("person", new Person());
         //   model.addAttribute("listPersons", this.personService.listPersons());
            model.addAttribute("notices",new Notes());
            model.addAttribute("canvases",new Canvas());
            return "person";
        }
    }

    @RequestMapping(value= "/person/add", method = RequestMethod.POST)
    public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,Model model,BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            return "redirect:/";
        }
            this.personService.addPerson(p);
            return "redirect:/";

    }

Entity :

@Entity
@Table(name="person")
public class Person implements UserDetails{

    @NotEmpty @Email
    @Column(name = "username")
    private String username;

    @NotEmpty(message = "Please enter password")
    @Column(name = "password")
    private String password;

    @Size(min = 2,max = 30)
    @Column(name = "firstname")
    private String firstName;

    @Size(min = 2,max = 50)
    @Column(name = "secretquestion")
    private String secretquestion;


    @Size(min = 2,max = 500)
    @Column(name = "secretanswer")
    private String secretanswer;
}

JSP :

<tr>
    <td>
        <form:label path="firstName">
            <spring:message text="FirstName"/>
        </form:label>
    </td>
    <td>
        <form:input path="firstName" />
    </td>
    <td><form:errors path="firstName"/>Please enter Firstname properly</td>
</tr>
<tr>
    <td>
        <form:label path="username">
            <spring:message text="Email"/>
        </form:label>
    </td>
    <td>
        <form:input path="username" />
    </td>
    <td><form:errors path="username"/>Please enter Email properly</td>
</tr>
<tr>
    <td>
        <form:label path="password">
            <spring:message text="Password"/>
        </form:label>
    </td>
    <td>
        <form:input path="password" />
    </td>
    <td><form:errors path="password"/>Please enter password properly</td>
</tr>

<tr>
<td>
    <form:label path="secretquestion">
        <spring:message text="secretquestion"/>
    </form:label>
</td>
<td>
    <form:input path="secretquestion" />
</td>
    <td><form:errors path="secretquestion"/>Please enter secretquestion properly</td>
</tr>


<tr>
    <td>
        <form:label path="secretanswer">
            <spring:message text="secretanswer"/>
        </form:label>
    </td>
    <td>
        <form:input path="secretanswer" />
    </td>
    <td><form:errors path="secretanswer"/>Please enter secretanswer properly</td>
</tr>

Servlet-context.xml

 <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>

Pom.xml

<!-- Validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.1.Final</version>
        </dependency>

Any pointers would be nice. Mainly I would like to avoid going on Apache 400, but just display what exactly went wrong with the input field.

1

There are 1 answers

1
Rohan On BEST ANSWER

This is might be because of public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,Model model,BindingResult bindingResult) signature .
BindingResult must follow @ModelAttribute as method signature might have more that one model object and Spring will create a separate BindingResult instance for each of them.That is why when the data is invalid spring is not able to bind errors to BindingResult and throws 400 error.

Try changing method signature to public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,BindingResult bindingResult,Model model).

Read more on BindingResult.