Validation spring mvc中对象的验证错误
这是我在此financialTransactionType中的验证程序代码,作为对象的financialTransactionType在模型中Validation spring mvc中对象的验证错误,validation,spring-mvc,Validation,Spring Mvc,这是我在此financialTransactionType中的验证程序代码,作为对象的financialTransactionType在模型中 @Override public void validate(Object target, Errors errors) { Date today = new Date(); MiniStatementViewModel miniStatementViewModel=(MiniStatementViewModel) target;
@Override
public void validate(Object target, Errors errors) {
Date today = new Date();
MiniStatementViewModel miniStatementViewModel=(MiniStatementViewModel) target;
else if(miniStatementViewModel.getFinancialTransactionType()==null)
{
errors.rejectValue("financialTransactionType","financialTransactionTypeNull");
}
所以,当我在JSP上显示错误消息时,会给出一个错误消息,如
public class MiniStatementViewModel {
private boolean isMiniStatement;
private Date fromDate;
private Date toDate;
private String accountCode;
private FinancialTransactionType financialTransactionType;
该错误意味着您正在从表单中获取字符串值
NONE
,为了使该值适合financialTransactionType
字段,它执行financialTransactionType.valueOf(“NONE”),并返回一个错误,因为它不存在
将您的班级改为:
Failed to convert property value of type java.lang.String to required type mkcl.os.apps.solar.account.model.FinancialTransactionType for property financialTransactionType;
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type mkcl.os.apps.solar.account.model.FinancialTransactionType for value NONE;
nested exception is java.lang.IllegalArgumentException: No enum const class mkcl.os.apps.solar.account.model.FinancialTransactionType.NONE
和您的验证器:
public class MiniStatementViewModel {
private boolean isMiniStatement;
private Date fromDate;
private Date toDate;
private String accountCode;
private String financialTransactionType;
@Override
public void validate(Object target, Errors errors) {
Date today = new Date();
MiniStatementViewModel miniStatementViewModel=(MiniStatementViewModel) target;
else if(FinancialTransactionType.valueOf(miniStatementViewModel.getFinancialTransactionType())==null)
{
errors.rejectValue("financialTransactionType","financialTransactionTypeNull");
}