我正在使用hibernate验证器来验证表单。但是验证器似乎没有工作。以下是模型
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没有打印出来,公司正在注册。
答案 0 :(得分: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>
希望这有帮助!