简体   繁体   中英

hibernate validator doesn't show error message

Hibernate Validator doesn't show error message. What did i miss? Please see my code below.

Here is a dependency:

<!-- Hibernate Validator -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.2.0.Final</version>
</dependency>

And Entity with annotated column:

@Entity
@Table(name = "transport")
public class Transport {

    ....

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

    ....
}

Here are methods from controller:

//show all and add form
    @RequestMapping (value = "/admin/transports", method = RequestMethod.GET)
    public String findAll(ModelMap map){
        List<Transport> transports = transportService.findAll();
        map.put("transport", new Transport());
        map.put("transports", transports);
        return "admin/transports/list";
    }

//add new
@RequestMapping(value = "/admin/transport/add", method = RequestMethod.POST)
public String addTypeShop(@ModelAttribute("type") @Valid Transport transport, BindingResult result) {
    if (result.hasErrors()) {
        return "redirect:/admin/transports";
    } else {
        this.transportService.addTransport(transport);
        return "redirect:/admin/transports";
    }
}

And jsp page:

<form:form role="form" action="/admin/transport/add" method="post" commandName="transport">
    <div class="row">
        <div class="col-lg-6">
            <div class="form-group">
                <label for="name">Name</label>
                <form:input type="text" path="name" class="form-control input-sm" id="name" autofocus="true"/>
                <form:errors path="name"/>
            </div>
        </div>
    <input type="submit" class="btn btn-sm btn-primary" value="Add" onclick="loading()"/>
</form:form>

You do not have any code showing the errors back to the end user.

Please see the following link for an example: http://www.mkyong.com/spring-mvc/spring-mvc-form-errors-tag-example/

Also, It doesn't look like you actually set your command object in your form. You might not have shown that code though.

Small side note, I would not do a redirect if you have validation errors, just send them directly back to the page they came from.

EDIT

After further review, I do see that you have . Normally this goes outside and above the form itself.

<form:errors path="transport"/>
<form:form role="form" action="/admin/transport/add" method="post" commandName="transport">
    <div class="row">
        <div class="col-lg-6">
            <div class="form-group">
                <label for="name">Name</label>
                <form:input type="text" path="name" class="form-control input-sm" id="name" autofocus="true"/>

            </div>
        </div>
        <input type="submit" class="btn btn-sm btn-primary" value="Add" onclick="loading()"/>
     </div>
</form:form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM