Spring表单提交外键验证

时间:2014-07-25 06:25:29

标签: forms spring-mvc

我的项目是一个spring mvc项目。在我的项目中我有一个域技术,它有外键引用。当我在表单上验证时,它会抛出错误....对于查看部分(jsp),我使用表单:select用于查看技术部门。 我如何验证外国参考????? 我试过下面的代码

@Entity
@Table(name = "technology")
public class Technology {
private int id;
@NotEmpty
private String name;
@NotEmpty
private Department department;
private Date createdDate;
private boolean isDelete;
}

message.properties

NotEmpty.technology.department=Required!

Technology.jsp

<form:form method="post" action="add-technology"
            commandName="technology" id="technologyForm">

            <label>Technology Name</label>
            <form:input path="name" /><form:errors path="name" class="error"></form:errors>
            <br />
            <label>Department</label>
            <form:select path="department.id">

                <form:option value="0" label="Select" />

                <form:options items="${departments}" itemValue="id" itemLabel="name" />

            </form:select><form:errors path="department" class="error"></form:errors>
            <%--    <form:select path="department.id" items="${departments}" /> --%>
            <input type="submit" class="btn btn-primary"/>

        </form:form>

控制器

@RequestMapping(value = "/add-technology")
public String addTechnology(
        @ModelAttribute(value = "technology")@Valid Technology technology,
        BindingResult result) {
    if(result.hasErrors()){
        return "/secure/admin/technology";
    }
    java.util.Date utilDate = new java.util.Date();
    Date sqlDate = new Date(utilDate.getTime());
    technology.setCreatedDate(sqlDate);

    technologyService.saveTechnology(technology);
    return "redirect:/technologies";
}

错误

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.UnexpectedTypeException: No validator could be found for type: com.company.product.domain.Department

我该如何解决这个问题???

1 个答案:

答案 0 :(得分:1)

在这里,您必须为Technology对象

实现验证器
class TechnologyValidator extends Validator {

    public boolean supports(Class<?> cls) {
        return Technology .class.equals(cls);
    }

    public void validate(Object target, Errors errors) {
        super.validate(target, errors);
        Technology tecObj= (Technology ) target;
        //here i am assuming Technology name is REQUIRED and   
        //NotEmpty.technology.name is in message.properties
       ValidationUtils.rejectIfEmptyOrWhitespace(errors,"name",
                                           "NotEmpty.technology.name");


      Department dept = tecObj.getDepartment();
      //as from from your are binding department ID
      if (dept==null || dept.getId()==null || dept.getId()==0L ) {
         errors.rejectValue("department.id", "NotEmpty.technology.department");
      }
  }
}

在Spring-context

创建此类
@Autowired
TechnologyValidator techValid;

并在控制器中调用此验证器,如

@RequestMapping(value = "/add-technology")
public String addTechnology(
        @ModelAttribute(value = "technology") Technology technology,
        BindingResult result) {

    //call validator
    techValid.validate(technology, result);

    if(result.hasErrors()){
        return "/secure/admin/technology";
    }
    java.util.Date utilDate = new java.util.Date();
    Date sqlDate = new Date(utilDate.getTime());
    technology.setCreatedDate(sqlDate);

    technologyService.saveTechnology(technology);
    return "redirect:/technologies";
}