我无法使用JSR303 @Valid显示错误消息

时间:2014-09-11 10:02:41

标签: java spring hibernate annotations

我尝试在我的jsp文件中显示错误消息,但它没有用。

这里是我的Model类,名为EmployeeEntity.java

@Entity
@Table(name="EMPLOYEE")
public class EmployeeEntity 
{
    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;

    @Column(name="FIRSTNAME")
    private String firstname;

    @Column(name="LASTNAME")
    private String lastname;

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

    @Column(name="EMAIL")
    private String email;

    @Column(name="TELEPHONE")
    private String telephone;

    @Column(name="USERNAME")
    private String username;

    @Column(name="PASSWORD")
    private String password;


    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPassword() {
        return password;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getId() {
        return id;
    }

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

    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;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
}

这是我的jsp页面

<form:form method="post" commandName="employeeEntity" action="add">
<form:errors path="*" cssClass="errorblock" element="div" />
<div class="panel panel-primary">
    <div class="panel-heading">
        Employee Register Form
    </div>
    <div class="panel-body">
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.firstname" />
            </label>
            <div class="col-lg-10">
                <form:input path="firstname" class="form-control" placeholder="first name" />
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.lastname"/>
            </label>
            <div class="col-lg-10">
                <form:input path="lastname" class="form-control" placeholder="last name" />
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.address"/>
            </label>
            <div class="col-lg-10">
                <form:textarea rows="5" path="address" class="form-control" placeholder="address" />
                <form:errors path="address" cssClass="error" />
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.gender"/>
            </label>
            <div class="col-lg-10">
                <label class="label-radio">
                    <form:radiobutton path="gender" value="male"/>
                    Male
                </label>
                <label class="label-checkbox">
                    <form:radiobutton path="gender" value="female" />
                    Female      
                </label>
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.email"/>
            </label>
            <div class="col-lg-10">
                <form:input path="email" class="form-control" placeholder="email" />
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.phonenumber"/>
            </label>
            <div class="col-lg-10">
                <form:input path="telephone" class="form-control" placeholder="phone number" />
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.username"/>
            </label>
            <div class="col-lg-10">
                <form:input path="username" class="form-control" placeholder="username" />
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label">
                <fmt:message key="label.password"/>
            </label>
            <div class="col-lg-10">
                <form:password path="password" class="form-control" placeholder="password" />
                <span class="help-block"></span>
            </div>
        </div>
    </div>
    <div class="panel-footer">
        <input type="submit" class="btn btn-primary" value="<fmt:message key="button.submit" />" />
        <input type="reset" class="btn btn-warning" value="<fmt:message key="button.reset" />" />
    </div>
</div>

这是我的xml文件

<context:annotation-config />
<context:component-scan base-package="com.howtodoinjava.controller" />

<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />


enter code here
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="employeeDAO" class="com.howtodoinjava.dao.EmployeeDaoImpl"></bean>
<bean id="carDAO" class="com.howtodoinjava.dao.CarDaoImpl"></bean>

<bean id="employeeManager" class="com.howtodoinjava.service.EmployeeManagerImpl"></bean>
<bean id="carManager" class="com.howtodoinjava.service.CarManagerImpl"></bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

这是我的控制器类

    @RequestMapping(value = "registerForm/add", method = RequestMethod.POST)
public String addEmployee(@Valid EmployeeEntity employee, BindingResult result){
    if(result.hasErrors()) {
        return "redirect:/registerForm/form";
    } else {
        employeeManager.addEmployee(employee);
        return "redirect:/list";
    }
}

我希望你能帮助我,我是春天的新生儿

谢谢, 最诚挚的问候

2 个答案:

答案 0 :(得分:0)

您需要有一个验证器类(例如EmployeeValidator),它实现包含验证逻辑的org.springframework.validation.Validator接口。除此之外,您需要在控制器类中绑定该验证器,其中addEmployee方法定义如下:

假设您通过控制器类中的变量employeeValidator声明了EmployeeValidator(您可以通过在bean config xml中声明它来自动装配),然后您需要在控制器类中绑定验证器,如下所示。

@InitBinder
private void initBinder(WebDataBinder binder) {
    binder.setValidator(employeeValidator);
}

然后验证器将自动调用并设置BindingResult中的错误(即导致您的方法签名)。

要编写验证器实现,请检查以下链接:

Spring MVC Form Validation Example using Annotation and Custom Validator implementation

Validation Framework in Spring with Example

希望它有所帮助。

答案 1 :(得分:0)

解决问题..

我已经更新了我的问题并包含了我的jsp页面,以及那里的问题。

我无法显示错误消息,因为<form:form>标记中的commandName不适用于我的模型类名称中的camelcase。所以我从“员工”改为“employeEntity”

我从this link

找到答案

希望这个问题能帮助你们......