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並沒有被打印出來,該公司正在註冊。
我剛剛回答自己的問題。非常感謝! –