spring3.0有级联关系的表单验证

model类 一下情况分别是一对一 和 一对多 至于多对多的关系 只要建立中间表的model类应该可以实现(我没测试因为我的多对多的关联没有建中间表的model)

private CmsQuestionType cmsQuestionType;

private List<CmsQuestionOption> cmsQuestionOptions;


@Valid //加上@Valid注解即可级联验证 被关联那方只在属性上做判断就行

@ManyToOne(fetch = FetchType.LAZY)

@JoinColumn(name = "question_type_id")

public CmsQuestionType getCmsQuestionType() {

return this.cmsQuestionType;

}


public void setCmsQuestionType(CmsQuestionType cmsQuestionType) {

this.cmsQuestionType = cmsQuestionType;

}

@Valid

@OneToMany(fetch = FetchType.LAZY, mappedBy = "cmsQuestion")

public List<CmsQuestionOption> getCmsQuestionOptions() {

return this.cmsQuestionOptions;

}


public void setCmsQuestionOptions(List<CmsQuestionOption> cmsQuestionOptions) {

this.cmsQuestionOptions = cmsQuestionOptions;

}

@valid

@ManyToMany(fetch = FetchType.LAZY)

@JoinTable(name = "tb_cms_klpoint_question", 

joinColumns = { @JoinColumn(name = "question_id", nullable = false, updatable = false) }, 

inverseJoinColumns = { @JoinColumn(name = "knowledge_point_id", nullable = false, updatable = false) })

public List<CmsKnowledgePoint> getCmsKnowledgePoints() {

return this.cmsKnowledgePoints;

}

public void setCmsKnowledgePoints(List<CmsKnowledgePoint> cmsKnowledgePoints) {

this.cmsKnowledgePoints = cmsKnowledgePoints;

}

jsp代码

one to one

<form:errors path="cmsQuestionType.typeName" cssStyle="color:red"></form:errors>

one to many

<form:errors path="cmsQuestionOptions[0].content" cssStyle="color:red"></form:errors>

<form:errors path="cmsQuestionOptions[1].content" cssStyle="color:red"></form:errors>

<form:errors path="cmsQuestionOptions[2].content" cssStyle="color:red"></form:errors>

<form:errors path="cmsQuestionOptions[3].content" cssStyle="color:red"></form:errors>

many to many还没测试以后补起来

<form:errors path="cmsKnowledgePoints.中间表.pointName" cssStyle="color:red"></form:errors>



本文出自 “一直在爬坑” 博客,谢绝转载!

你可能感兴趣的:(一对多,级联表单验证一对一)