java - Spring MVC validator 中不显示错误消息

标签 java spring hibernate validation error-handling

我的代码如下,当我将输入字段输入为错误值时, validator 会使输入的 bean 值无效,但在 View 中不会显示错误消息。

Controller

@RequestMapping(value = "/AddInventory.html", method = RequestMethod.POST)
public ModelAndView inventorymgmt(@Valid Itemtype itemTypeForm, BindingResult result, Map<String, Object> model) throws Exception {
    logger.log(Level.OFF, "Add Inventory called with inventory details ####### ." + itemTypeForm);

    if (result.hasErrors()) {
        logger.log(Level.OFF, "Error occured while inserting the reconrd for the item type." + result.getAllErrors());
        ModelAndView modelAndView = new ModelAndView("add-item_category");
        modelAndView.addAllObjects(model);
        return modelAndView;
    }
    else {
        logger.log(Level.OFF, "Insert result ####### ." + itemTypeDAO.insert(itemTypeForm));
        return new ModelAndView("redirect://item_category.html");
    }
}

查看

<form:form action="AddInventory.html" method="POST" commandName="itemTypeForm">
    <form:errors path="*" cssClass="errorblock" element="div" />
    <div class="col-md-9">
        <div class="catagory-main-box top-radius">
            <div class="cat-box-title cat-title-font top-radius">Item Category </div>

            <div class="row tb-margin">
                <div class="col-sm-2"></div>
                <div class="col-sm-8 visible-lg visible-md visible-sm">

                    <div class="form-group">
                        <label class="col-sm-4 col-xs-12 control-label search-text">Name:</label>
                        <div class="col-sm-8 col-xs-12">
                            <form:input  type="text" class="form-control" path="name" placeholder="Name"/>
                            <form:errors path="name" cssClass="error" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 col-xs-12 control-label search-text"> Description:</label>
                        <div class="col-sm-8 col-xs-12">
                            <form:input  type="text" class="form-control" path="description" placeholder="Description"/>
                            <form:errors path="description" cssClass="error" />
                        </div>
                    </div>
                </div>

                <div class="col-sm-8 visible-xs">
                    <div class="form-group">
                        <div class="col-sm-8 col-xs-12">
                            <form:input  type="text" class="form-control" path="name" placeholder="Name"/>
                            <form:errors path="name" cssClass="error" />
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-8 col-xs-12">
                            <form:input  type="text" class="form-control" path="description" placeholder="Description"/>
                        </div>
                    </div>
                </div>

                <div class="col-sm-2"></div>
            </div>

            <div class="row text-pad-top visible-lg visible-md visible-sm">
                <div class="div-center">
                    <button type="button" class="btn btn-orange" onclick="submitDetailsForm();">Save</button>
                    <button type="button" class="btn btn-orange" onclick="javascript:history.back();">Cancel</button>
                </div>
            </div>

            <div class="row text-pad-top visible-xs ">
                <div class="div-center-xs">
                    <button type="button" class="btn btn-orange" onclick="submitDetailsForm();">Save</button>
                    <button type="button" class="btn btn-orange" onclick="javascript:history.back();">Cancel</button>
                </div>
            </div>
        </div>
    </div>
</form:form>

bean

package com.tss.ocean.pojo;
// Generated 4 Aug, 2014 6:30:10 PM by Hibernate Tools 3.2.1.GA

import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * Itemtype generated by hbm2java
 *
 * @author Bhavik Ambani
 */
public class Itemtype implements java.io.Serializable {

    private Integer id;

    @NotEmpty(message = "Please enter item name.")
    @Size(min = 10, max = 45, message = "Item name must between 1 and 45 characters")
    private String name;

    @Size(min = 0, max = 45, message = "Item description must between 0 and 65535 characters")
    private String description;

    public Itemtype() {
    }

    public Itemtype(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Itemtype{" + "id=" + id + ", name=" + name + ", description=" + description + '}';
    }
}

最佳答案

将您的 Controller 方法更改为以下,只是在 @Valid 之前添加:

@ModelAttribute("itemTypeForm") // Pass your Model Attribute here. I assumed it to be `itemTypeForm`.

Controller 代码在这里

@RequestMapping(value = "/AddInventory.html", method = RequestMethod.POST)
public ModelAndView inventorymgmt(@ModelAttribute("itemTypeForm") @Valid Itemtype itemTypeForm, BindingResult result, Map<String, Object> model) throws Exception {
    logger.log(Level.OFF, "Add Inventory called with inventory details ####### ." + itemTypeForm);

    if (result.hasErrors()) {
        logger.log(Level.OFF, "Error occured while inserting the reconrd for the item type." + result.getAllErrors());
        ModelAndView modelAndView = new ModelAndView("add-item_category");
        modelAndView.addAllObjects(model);
        return modelAndView;
    }
    else {
        logger.log(Level.OFF, "Insert result ####### ." + itemTypeDAO.insert(itemTypeForm));
        return new ModelAndView("redirect://item_category.html");
    }
}

关于java - Spring MVC validator 中不显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465186/

相关文章:

java - 如何使我的根 jaxb 生成的类扩展我自己的类

java - 如何正确使用JTA资源回收方法?

java - 以异步方式实现长轮询

java - 当变量在多个类中发生更改时,如何同步对变量的访问?

java - 事务需要异常 : no transaction is in progress with Spring Data JPA

java - 无法在 hibernate 状态下同时使用 where 和 groupby

java - 如何使用由静态初始化程序调用的 PowerMockito 在 java 中模拟私有(private)静态方法?

java - Hibernate 运行所有表并进行简单的登录

java - 如何在Spring中动态启用系统文件访问?

java.lang.NoClassDefFoundError : org/hibernate/cache/spi/RegionFactory - When upgrading from spring 3 to spring 4 错误