如何在使用HibernateValidator违反任何验证规则时显示错误消息?

时间:2012-06-29 19:50:36

标签: java spring hibernate jsp hibernate-validator

我正在使用Spring中的Spring和Hibernate验证一个简单的表单(使用SimpleFormController),并在HibernateValidator的帮助下解释here。表格如下。

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<form:form method="post" action="Temp.htm" commandName="validationForm">

<table>
    <tr>
        <td>User Name:<font color="red"><form:errors path="userName" /></font></td>
    </tr>

    <tr>
        <td><form:input path="userName" /></td>
    </tr>

    <tr>
        <td>Age:<font color="red"><form:errors path="age" /></font></td>
    </tr>

    <tr>
        <td><form:input path="age" /></td>
    </tr>

    <tr>
        <td>Password:<font color="red"><form:errors path="password" /></font></td>
    </tr>

    <tr>

    <td><form:password path="password" /></td>

    </tr>

    <tr>
        <td><input type="submit" value="Submit" /></td>
    </tr>
    </table>

</form:form>

以下是验证表单字段的命令类。

final public class ValidationForm 
{
    @NotEmpty
    @Size(min = 1, max = 2)
    private String userName;
    @NotNull
    @NumberFormat(style = Style.NUMBER)
    @Min(1)
    @Max(110)
    private Integer age;
    @NotEmpty(message = "Password must not be blank.")
    @Size(min = 1, max = 1, message = "Password must between 1 to 10 Characters.")
    private String password;

    public void setUserName(String userName)
    {
            this.userName = userName;
    }

    public String getUserName()
    {
            return userName;
    }

    public void setAge(Integer age)
    {
            this.age = age;
    }

    public Integer getAge()
    {
            return age;
    }

    public void setPassword(String password)
    {
            this.password = password;
    }

    public String getPassword()
    {
            return password;
    }
}

以下是控制器类Temp

@SuppressWarnings("deprecation")
final public class Temp extends SimpleFormController
{
    private TempService tempService=null;
    public Temp()
    {
        //setCommandClass(Temp.class);
        setSuccessView("Temp");
        setFormView("Temp");
        setCommandClass(ValidationForm.class);
        setCommandName("validationForm");
    }

    public void setTempService(TempService tempService)
    {
        this.tempService = tempService;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("validationForm") @Valid Object command, BindException errors) throws Exception
    {
        ValidationForm validationForm=(ValidationForm) command;        
        tempService.add(validationForm);
        Map<String, Object>model=new HashMap<String, Object>();
        model.put(getCommandName(), new ValidationForm());
        ModelAndView mv=new ModelAndView("Temp", "validationForm", validationForm);
        return mv;
    }

    @Override
    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception
    {
        Map<String, Object>model=new HashMap<String, Object>();
        model.put(getCommandName(), new ValidationForm());
        ModelAndView mv=new ModelAndView("Temp", "validationForm", new ValidationForm());
        return mv;
    }
}

dispatchar-servlet.xml文件中有特定的映射。现在,我需要在ValidationForm类中提到的违反验证时显示特定的错误消息,但不显示错误消息。如何使用HibernateValidator

显示特定的错误消息

修改

已配置命令类dispatchar-servlet.xml和控制器类ValidationForm的{​​{1}}文件如下所示。

Temp


以下是<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean class="controller.Country" id="countryKey"></bean> <bean class="controller.State" id="stateKey"></bean> <bean class="controller.Theater" id="theaterKey"></bean> <bean class="controller.Screen" id="screenKey"></bean> <bean class="controller.ScreenClass" id="screenClassKey"></bean> <bean class="controller.ScreenClassSeating" id="screenClassSeatingKey"></bean> <bean class="controller.ScreenClassSeatingArrangement" id="screenClassSeatingArrangementKey"></bean> <bean class="controller.Movie" id="movieKey"></bean> <bean class="controller.ScreenShow" id="screenShowKey"></bean> <bean class="controller.City" id="cityKey"></bean> <bean class="controller.Language" id="languageKey"></bean> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> //Need to concentrate on the following few lines. <bean id="tempService" class="usebeans.TempServiceImpl" /> <bean id="tempController" class="controller.Temp" p:tempService-ref="tempService" p:formView="Temp" p:successView="Temp"/> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages" /> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> <prop key="Country.htm">countryKey</prop> <prop key="State.htm">stateKey</prop> <prop key="Theater.htm">theaterKey</prop> <prop key="Screen.htm">screenKey</prop> <prop key="ScreenClass.htm">screenClassKey</prop> <prop key="ScreenClassSeating.htm">screenClassSeatingKey</prop> <prop key="ScreenClassSeatingArrangement.htm">screenClassSeatingArrangementKey</prop> <prop key="Movie.htm">movieKey</prop> <prop key="ScreenShow.htm">screenShowKey</prop> <prop key="City.htm">cityKey</prop> <prop key="Language.htm">languageKey</prop> <prop key="Temp.htm">tempController</prop> //Need to concentrate here <prop key="ExcelSheet.htm">excelSheetKey</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> //The index controller <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> 界面。

TempService

以下是实现上述接口的import validators.ValidationForm; public interface TempService { public void add(ValidationForm validationForm); } 类。

TempServiceImpl

我在这里做错了什么?

0 个答案:

没有答案