2012-05-05 84 views
1

我正在使用hibernate驗證器來驗證表單。然而驗證器似乎沒有工作。下面是該模型Hibernate驗證器不能與Spring一起使用

package com.wallstreet.model; 

import javax.persistence.Column; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Entity; 
import org.hibernate.validator.constraints.Range; 
import org.hibernate.validator.constraints.NotEmpty; 


@Entity 
public class Company { 

public static final String[] COLUMN_NAMES = { "Company Id", "Share Id", 
     "Name", "Primary Industry", "Secondary Industry", 
     "Price", "Activity", "Rise Probability", 
     "Move Amount Proabbility", "Range Percent"}; 
public static final int NO_COLUMNS = COLUMN_NAMES.length; 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "COMPANY_ID") 
@NotEmpty 
private int id; 

@NotEmpty 
@Column(name = "SHARE_ID", unique = true) 
private String shareID; 

@NotEmpty 
@Column(name = "COMPANY_NAME", unique = true) 
private String companyName; 

@NotEmpty 
@Column(name = "PRIMARY_INDUSTRY") 
private String primaryIndustry; 

@Column(name = "SECONDARY_INDUSTRY") 
private String secondaryIndustry; 

@NotEmpty 
@Range(min = 0) 
@Column(name = "CURRENT_PRICE") 
private double currentPrice; 

@Range(min = 0, max=1) 
@Column(name = "ACTIVITY") 
private double activity; 

@Range(min = 0, max=1) 
@Column(name = "RISE_PROBABILITY") 
private double riseProbability; 

@Range(min = 0, max=1) 
@Column(name = "MOVE_AMOUNT_PROBABILITY") 
private double moveAmountProbability; 

@Range(min = 0) 
@Column(name = "RANGE_PERCENT") 
private double rangePercent; 

public Company() { 
} 

public int getId() { 
    return id; 
} 

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

public String getShareID() { 
    return shareID; 
} 

public void setShareID(String shareID) { 
    this.shareID = shareID; 
} 

public String getCompanyName() { 
    return companyName; 
} 

public void setCompanyName(String companyName) { 
    this.companyName = companyName; 
} 

public String getPrimaryIndustry() { 
    return primaryIndustry; 
} 

public void setPrimaryIndustry(String primaryIndustry) { 
    this.primaryIndustry = primaryIndustry; 
} 

public String getSecondaryIndustry() { 
    return secondaryIndustry; 
} 

public void setSecondaryIndustry(String secondaryIndustry) { 
    this.secondaryIndustry = secondaryIndustry; 
} 

public double getCurrentPrice() { 
    return currentPrice; 
} 

public void setCurrentPrice(double currentPrice) { 
    this.currentPrice = currentPrice; 
} 

public double getActivity() { 
    return activity; 
} 

public void setActivity(double activity) { 
    this.activity = activity; 
} 

public double getRiseProbability() { 
    return riseProbability; 
} 

public void setRiseProbability(double riseProbability) { 
    this.riseProbability = riseProbability; 
} 

public double getMoveAmountProbability() { 
    return moveAmountProbability; 
} 

public void setMoveAmountProbability(double moveAmountProbability) { 
    this.moveAmountProbability = moveAmountProbability; 
} 

public double getRangePercent() { 
    return rangePercent; 
} 

public void setRangePercent(double rangePercent) { 
    this.rangePercent = rangePercent; 
} 

}

下面是我的控制器

@RequestMapping(value="admin/registercompanies.html", method= RequestMethod.POST) 
public ModelAndView saveCompany(@Valid Company company,BindingResult result, SessionStatus status){ 
    if(result.hasErrors()){ 
     System.out.println("1"); 
     return new ModelAndView("admin/company/register"); 
    } 
    else{ 
     companyService.addCompany(company); 
     return new ModelAndView("admin/company/registered"); 
    } 
} 

1並沒有被打印出來,該公司正在註冊。

回答

2

也許你忘記了在你的配置文件中聲明一些東西。你可以添加你的dispatcher-servlet配置文件嗎?這是我的參考和我的驗證工作正常。

<?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: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/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 id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 
    <context:annotation-config /> 

    <context:component-scan base-package="com.dlinx90" /> 

    <mvc:annotation-driven /> 

    <import resource="hibernate-context.xml" /> 

    <mvc:interceptors> 
     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> 
    </mvc:interceptors> 

    <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" 
     id="localeResolver" /> 

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

</beans> 

希望這有助於!

+0

我剛剛回答自己的問題。非常感謝! –

相關問題