0

由于某种原因,它不起作用。我搜索并尝试了在网上找到的所有解决方案。没有骰子。看起来我错过了一些东西。

我的光束:

@Entity
@Table(name="employees")
public class Person {

    private Integer person_id;
    private String name;
    private String name2;
    private String email;
    private double phone;
    private String desc;

    @Id
    @Max(value=500)
    @Column (name="id")
    public Integer getPerson_id() {
        return person_id;
    }

    public void setPerson_id(Integer person_id) {
        this.person_id = person_id;
    }

    @NotNull
    @NotEmpty
    @Column (name="fn")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Column(name="ln")
    public String getName2() {
        return name2;
    }
    public void setName2(String name2) {
        this.name2 = name2;
    }

    @Email
    @Column (name="em", unique = true)
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name="phone")
    public double getPhone() {
        return phone;
    }
    public void setPhone(double phone) {
        this.phone = phone;
    }

    @Column (name="de")
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String toString() {
        return getPerson_id() + " - " + getName() + " - " + getName2() + " - " +  getEmail();
    }
}

处理 POST 的控制器方法:

    @RequestMapping(value="/register", method =  RequestMethod.POST, headers="Accept=*/*")
public String registerUer( @javax.validation.Valid Person registerForm, BindingResult br){


    System.out.println( "Erros?" + br.hasErrors());
    if ( br.hasErrors() ) {
        System.out.println(br.getAllErrors());
    }
    System.out.println(registerForm);
    return "thankyou";
}

主页.jsp

 <form:form action="register.htm" commandName="registerForm" method="post">
  Name1: <form:input path="name"/><br />
  Name2: <form:input path="name2"/><br />
  Email: <form:input path="email"/><br />
  Desc:  <form:input path="desc" /><br />
  Phone: <form:input path="phone" /><br />
  <input type="submit" />
 </form:form>

应用程序上下文 xml 文件:

  <mvc:annotation-driven />
  <context:component-scan base-package="com.springmvcsample"/>
  <context:annotation-config/>
  <import resource="hibernate_config.xml"/>

  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix">
  <value>/WEB-INF/views/</value>
   </property>
   <property name="suffix">
  <value>.jsp</value>
   </property>
  </bean>

  <bean id="contentNegotiationManager"    
        class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <!-- Turn off working out content type based on URL file extension, should fall back   
                to looking at the Accept headers -->
   <property name="favorPathExtension" value="false" />
  </bean>

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

我总是hasErrors()返回 false 我的类路径中有休眠验证器(GA jar)文件。我看到这个加载:

  INFO  [Version] Hibernate Validator 4.2.0.Final

我错过了什么吗?

4

1 回答 1

0

如果我删除 JPA 注释(因为我想避免对 db 进行进一步配置)并使用您的表单和控制器代码,它会按预期工作。

肯定有一些缺失的部分,您可以尝试通过验证注释制作一个简单的 pojo 并查看。

于 2013-04-29T07:00:16.767 回答