简体   繁体   English

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

[英]I can't display error message using JSR303 @Valid

I try to display error message in my jsp's file, but it didn't work. 我尝试在我的jsp文件中显示错误消息,但没有成功。

here my Model class, named EmployeeEntity.java 这是我的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;
    }
}

Here my jsp page 这是我的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>

Here is my xml file 这是我的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>

and here is my controller class 这是我的控制器课

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

I hope you can help me, and i'am new in spring hibernate 希望您能帮助我,我在春季冬眠时刚来

Thanks, Best Regards 谢谢,最好的问候

You need to have a validator class (eg EmployeeValidator) that implements org.springframework.validation.Validator interface which contain the validation logic. 您需要具有一个实现org.springframework.validation.Validator接口的验证器类(例如EmployeeValidator),该接口包含验证逻辑。 Apart from that you would need to bind that validator in the controller class where your addEmployee method is defined like following: 除此之外,您还需要将该控制器绑定到定义了addEmployee方法的控制器类中,如下所示:

Let's say you declared the EmployeeValidator via the variable employeeValidator in the controller class (you can autowire by having it declared in your beans config xml), then you need to bind the validator in the controller class like below. 假设您通过控制器类中的变量employeeValidator声明了EmployeeValidator(可以通过在bean config xml中声明它来自动装配),那么您需要像下面那样在控制器类中绑定验证器。

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

Then the validator would automatically call and set the errors in the BindingResult (ie result in your method signature). 然后,验证器将自动调用并设置BindingResult中的错误(即,导致您的方法签名)。

For writing the validator implementation check the following links: 要编写验证器实现,请检查以下链接:

Spring MVC Form Validation Example using Annotation and Custom Validator implementation 使用注释和自定义验证器实现的Spring MVC表单验证示例

Validation Framework in Spring with Example 带有示例的Spring验证框架

Hope it helps. 希望能帮助到你。

Problem Solved.. 问题解决了..

I have update my question and include my jsp page, and the problem in there. 我已经更新了我的问题,并在其中包含了我的jsp页面和问题。

I can't display my error message because commandName in <form:form> tag doesn't apply camelcase from my model class name. 我无法显示错误消息,因为<form:form>标记中的commandName不适用于我的模型类名称中的camelcase。 So i change from "employee" into "employeEntity" 所以我从“员工”变成“ employeEntity”

I found the answer from this link 我从此链接找到了答案

hope this question help you too guys... 希望这个问题对您也有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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