1.配置好附件内容
2.validate类:
package com.guohualife.rcms.riskGrade.web.controller.validate; import java.util.List; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component; import com.guohualife.rcms.common.api.utils.CheckValidate; import com.guohualife.rcms.common.web.validate.Errors; import com.guohualife.rcms.common.web.validate.WebValidator; import com.guohualife.rcms.common.web.validate.rule.NullableRule; import com.guohualife.rcms.common.web.validate.rule.StringLengthRule; import com.guohualife.rcms.riskGrade.api.util.ConstantEnum; import com.guohualife.rcms.riskGrade.dto.GradeIndexPropertyDTO; import com.guohualife.rcms.riskGrade.dto.QualitativeRuleDTO; @Component("gradeRuleValidate") public class GradeRuleValidate extends WebValidator { @SuppressWarnings("unchecked") @Override protected void baseValidate() { NullableRule nullableRule = null;// 基础校验:非空 StringLengthRule stringLengthRule = null;// 基础校验:长度 } @Override protected void validate(Object obj, Errors errors) { GradeIndexPropertyDTO gradeIndexPropertyDTO = (GradeIndexPropertyDTO) obj; String gradeIndexType = gradeIndexPropertyDTO.getGradeIndexType(); if (StringUtils.equals(gradeIndexType, ConstantEnum.GRADE_INDEX_TYPE_DL)) { List salechnlList = gradeIndexPropertyDTO.getSalechnlList(); for (int i = 0; i < salechnlList.size(); i++) { } } else if (StringUtils.equals(gradeIndexType, ConstantEnum.GRADE_INDEX_TYPE_DX)) { String gradeScore= gradeIndexPropertyDTO.getGradeScore(); double gradeScoreDou = 0 ; if(StringUtils.isNotEmpty(gradeScore)){ gradeScoreDou =Double.parseDouble(gradeScore); } List qualitativeRuleList = gradeIndexPropertyDTO.getQualitativeRuleList(); double allQuestItemScore = 0 ; double eqQualitativeScore = 0; String eqQuestionBankId =""; /*====begin 计算题目总分之和的参数====*/ double allQualitativeScore = 0; /*====end 计算题目总分之和的参数====*/ for (int i = 0; i < qualitativeRuleList.size(); i++) { boolean checkOneFlag = false; boolean checkTwoFlag = false; QualitativeRuleDTO qualitativeRuleDTO = (QualitativeRuleDTO) qualitativeRuleList.get(i); String rowSpan = qualitativeRuleDTO.getOptionNum(); String qualitativeScore = qualitativeRuleDTO.getQualitativeScore(); boolean checkIsNotNullFlag = this.checkIsNotNullMethod(errors, qualitativeRuleDTO, i); String questionBankId = qualitativeRuleDTO.getQuestionBankId(); if(!checkIsNotNullFlag){ //校验业务逻辑 String qualitativeScoreObj =""; double qualitativeScoreDou =0; double questItemScoreDou = 0 ; if (StringUtils.isNotEmpty(rowSpan)) { qualitativeScoreObj = "qualitativeRuleList[" + i + "].qualitativeScore"; qualitativeScoreDou =Double.parseDouble(qualitativeScore); if( qualitativeScoreDou > gradeScoreDou){ errors.rejectValue(qualitativeScoreObj, "不能大于总分值"); checkOneFlag = true; } eqQuestionBankId = questionBankId; if (StringUtils.isNotEmpty(qualitativeScore)) { eqQualitativeScore = Double.parseDouble(qualitativeScore); } } String questItemScore = qualitativeRuleDTO.getQuestItemScore(); String questItemScoreObj ="qualitativeRuleList[" + i + "].questItemScore"; questItemScoreDou =Double.parseDouble(questItemScore); if( questItemScoreDou > eqQualitativeScore){ errors.rejectValue(questItemScoreObj, "单条选项不能大于当前题目分值"); checkOneFlag = true; } if(!checkOneFlag){ /*====begin 计算题目总分之和====*/ allQualitativeScore = allQualitativeScore + qualitativeScoreDou; /*====end 计算题目总分之和====*/ checkTwoFlag = true ; } /*====begin 计算题目选项总分之和==== if(StringUtils.equals(eqQuestionBankId , questionBankId)){ allQuestItemScore = allQuestItemScore + questItemScoreDou; }else{ System.out.println(" 题目选项总分="+allQuestItemScore); if( allQuestItemScore > eqQualitativeScore){ errors.rejectValue("qualitativeRuleList[" + (i-1) + "].qualitativeScore", "题目分值不能小于选项分值总和"); } allQuestItemScore =0; } ====end 计算题目选项总分之和====*/ } } //for 循环结束后 if(allQualitativeScore > gradeScoreDou){ errors.rejectValue("gradeScore", "不能小于题目分值之和"); } System.out.println("题目总分="+allQualitativeScore); } } /** * 校验非空方法 * @param errors * @param qualitativeRuleDTO * @param i * @return */ private boolean checkIsNotNullMethod(Errors errors,QualitativeRuleDTO qualitativeRuleDTO,int i){ boolean checkResult = false; String rowSpan = qualitativeRuleDTO.getOptionNum(); String qualitativeScore = qualitativeRuleDTO.getQualitativeScore(); if (StringUtils.isNotEmpty(rowSpan)) { String qualitativeScoreObj = "qualitativeRuleList[" + i + "].qualitativeScore"; if (StringUtils.isEmpty(qualitativeScore)) { errors.rejectValue(qualitativeScoreObj, "题目分值不能为空"); checkResult = true; } else { if (!CheckValidate.isOnlyFloatNum(qualitativeScore)) { errors.rejectValue(qualitativeScoreObj, "只能输入整数或两位小数点"); checkResult = true; } } String assignType = qualitativeRuleDTO.getAssignType(); String assignTypeObj = "qualitativeRuleList[" + i + "].assignType"; if (StringUtils.isEmpty(assignType)) { errors.rejectValue(assignTypeObj, "得分类型不能为空"); } String scoreType = qualitativeRuleDTO.getScoreType(); String scoreTypeObj ="qualitativeRuleList[" + i + "].scoreType"; if (StringUtils.isEmpty(scoreType)) { errors.rejectValue(scoreTypeObj, "计分方式不能为空"); } } String questItemScore = qualitativeRuleDTO.getQuestItemScore(); String questItemScoreObj ="qualitativeRuleList[" + i + "].questItemScore"; if (StringUtils.isEmpty(questItemScore)) { errors.rejectValue(questItemScoreObj, "选项分值不能为空"); checkResult = true; } else { if (!CheckValidate.isOnlyFloatNum(questItemScore)) { errors.rejectValue(questItemScoreObj, "只能输入整数或两位小数点"); checkResult = true; } } return checkResult; } }
3.controller中注入validate校验类:
@Resource(name = "gradeRuleValidate") private Validator validator;