工作一年多了,总是看自己写的代码很不顺眼,可能也有点强迫症,就按最近的一次项目整理一下代码以备自己以后看看,也希望前辈多指点,本人也好多学习一些。
这次重构的代码是关于数据校验的部分,因为没有用校验框架校验的数据量也大,时间也比较紧,所以校验部分都是用最基本的if语句来判断校验的,反正我看的是想吐。。。不多说了先看一下重构前的代码:
private List<String> validate(UserBean userInfo) {
List<String> errorMessages = new ArrayList<String>();
if (ValidateUtils.isEmpty(userInfo.getMailAddress())) {
errorMessages.add(this.addActionError("error.input.required", new String[] { Constant.MAIL_ADDRESS }));
}
if (ValidateUtils.isEmpty(userInfo.getName())) {
errorMessages.add(this.addActionError("error.input.required", new String[] { Constant.NAME }));
}
if (ValidateUtils.isEmpty(userInfo.getKanaName())) {
errorMessages.add(this.addActionError("error.input.required", new String[] { Constant.KANA_NAME }));
}
if (ValidateUtils.isEmpty(userInfo.getPassword())) {
errorMessages.add(this.addActionError("error.input.required", new String[] { Constant.PASSWORD }));
}
if (ValidateUtils.isEmpty(userInfo.getAgainPassword())) {
errorMessages.add(this.addActionError("error.input.required", new String[] { Constant.PASSWORD_CONFIRM }));
}
int nullFlag = 0;
for (String str : careRange) {
if (str != null) {
return "true";
}
return "";
}
if (nullFlag == 0) {
errorMessages.add(this.addActionError("errors.check.noselect", new String[] { Constant.CARE_RANGE }));
}
if (ValidateUtils.isEmpty(userInfo.getBelong())) {
errorMessages.add(this.addActionError("errors.check.noselect", new String[] { Constant.BELONG }));
}
if (ValidateUtils.isEmpty(userInfo.getPrefecture())) {
errorMessages.add(this.addActionError("errors.check.noselect", new String[] { Constant.PREFECTURE }));
}
if (ValidateUtils.isEmpty(userInfo.getSex())) {
errorMessages.add(this.addActionError("errors.check.noselect", new String[] { Constant.SEX }));
}
if (ValidateUtils.isEmpty(userInfo.getYearOfBirth())) {
errorMessages.add(this.addActionError("errors.check.noselect", new String[] { Constant.YEAR_OF_BIRTH }));
}
if (ValidateUtils.isEmpty(userInfo.getTosFlag())) {
errorMessages.add(this.addActionError("errors.check.noselect", new String[] { Constant.TOS_FLAG }));
}
if (errorMessages.size() == 0) {
if (userInfo.getMailAddress().length() > Constant.MAIL_ADDRESS_MAX_LENGTH
|| userInfo.getMailAddress().length() < Constant.MAIL_ADDRESS_MIN_LENGTH) {
errorMessages.add(
this.addActionError("errors.form.range", new String[] { Constant.MAIL_ADDRESS, "5", "254" }));
}
if (userInfo.getName().length() > Constant.NAME_MAX_LENGTH
|| !ValidateUtils.isZenkaku(userInfo.getName())) {
errorMessages.add(
this.addActionError("errors.form.doubleMaxlength", new String[] { Constant.NAME , "32" }));
}
if (userInfo.getKanaName().length() > Constant.KANA_NAME_MAX_LENGTH
|| !ValidateUtils.isKatakanaAndHankaku(userInfo.getKanaName())) {
errorMessages.add(this.addActionError("errors.form.doubleMaxlength",
new String[] { Constant.KANA_NAME , Constant.KANA_NAME + "32" }));
}
if (userInfo.getPassword().length() > Constant.PASSWORD_MAX_LENGTH
|| userInfo.getPassword().length() < Constant.PASSWORD_MIN_LENGTH
|| !ValidateUtils.isHankaku(userInfo.getPassword())) {
errorMessages.add(this.addActionError("errors.form.singleNumber.password.range",
new String[] { Constant.PASSWORD }));
}
if (userInfo.getAgainPassword().length() > Constant.PASSWORD_MAX_LENGTH
|| userInfo.getAgainPassword().length() < Constant.PASSWORD_MIN_LENGTH
|| !ValidateUtils.isHankaku(userInfo.getAgainPassword())) {
errorMessages.add(this.addActionError("errors.form.singleNumber.password.range",
new String[] { Constant.PASSWORD_CONFIRM }));
}
if (!userInfo.getAgainPassword().equals(userInfo.getPassword())) {
errorMessages.add(this.addActionError("error.password.twice.confirm"));
}
if (userInfo.getMailAddress().indexOf(userInfo.getPassword()) != -1) {
errorMessages.add(this.addActionError("errors.equals.password.mail"));
}
if (userInfo.getYearOfBirth().length() > Constant.YEAR_OF_BIRTH_MAX_LENGTH
|| !ValidateUtils.isHalfsizeNumeric(userInfo.getYearOfBirth())
|| Integer.valueOf(userInfo.getYearOfBirth()) < Constant.YEAR_OF_BIRTH_MIN_YEAR
|| Integer.valueOf(userInfo.getYearOfBirth()) > Constant.YEAR_OF_BIRTH_MAX_YEAR) {
errorMessages.add(this.addActionError("errors.form.yearOfBirth.range"));
}
}
return errorMessages;
}
大致上就是校验数据对不对,然后从properties文件取出error信息,这之中可能会传参给properties。
思路:将非空校验和其他校验分开,将需要非空验证的数据存入map之中,然后遍历map循环校验。其他校验写成独立方法,以便于管理。下面是一些代码片段。
private List<String> validateEmpty(UserBean userInfo, ValidateRegister va) throws Exception {
// 装载输入框所有信息
Map<String[], String> inputMap = new LinkedHashMap<String[], String>();
// 校验邮件地址
inputMap.put(new String[] { Constant.MAIL_ADDRESS }, userInfo.getMailAddress());
// 校验name
inputMap.put(new String[] { Constant.NAME }, userInfo.getName());
// 校验KANA_NAME
inputMap.put(new String[] { Constant.KANA_NAME }, userInfo.getKanaName());
// 校验密码
inputMap.put(new String[] { Constant.PASSWORD }, userInfo.getPassword());
// 执行输入框非空校验
va.checkEmptyParameter(inputMap, "errors.input.required");
// 装载下拉框所有信息
Map<String[], String> pulldownMap = new LinkedHashMap<String[], String>();
// 校验关心领域
pulldownMap.put(new String[] { Constant.CARE_RANGE }, va.checkCareRange(userInfo.getCareRange()));
// 校验所属
pulldownMap.put(new String[] { Constant.BELONG }, userInfo.getBelong());
// 校验都道府县
pulldownMap.put(new String[] { Constant.PREFECTURE }, userInfo.getPrefecture());
// 校验性别
pulldownMap.put(new String[] { Constant.SEX }, userInfo.getSex());
// 校验生年月
pulldownMap.put(new String[] { Constant.YEAR_OF_BIRTH }, userInfo.getYearOfBirth());
// 校验是否同意
pulldownMap.put(new String[] { Constant.TOS_FLAG }, userInfo.getTosFlag());
// 执行下拉框非空校验
va.checkEmptyParameter(pulldownMap, "errors.check.noselect");
List<String> errorMessage = va.getErrorMessageList();
return errorMessage.size() > 0 ? errorMessage : validateSpecial(userInfo, va);
};
其中map的key是向properties占位符传入的参数。value则是需要校验的数据。
private List<String> validateSpecial(UserBean userInfo, ValidateRegister va) throws Exception {
va.checkMailAddress(userInfo.getMailAddress(), new String[] { Constant.MAIL_ADDRESS, "5", "254" });
va.checkName(userInfo.getName(), new String[] { Constant.NAME , "32" });
va.checkKanaName(userInfo.getKanaName(),
new String[] { Constant.KANA_NAME, Constant.KANA_NAME + "32" });
va.checkPassword(userInfo.getPassword(), new String[] { Constant.PASSWORD });
va.checkEqualPassword(userInfo.getPassword(), userInfo.getAgainPassword());
va.checkMailContantPassword(userInfo.getMailAddress(), userInfo.getAgainPassword());
va.checkYearOfBirth(userInfo.getYearOfBirth());
return va.getErrorMessageList();
}
ValidateRegister是新加的一个类,他继承了一个抽象类ValidateRegister类里有个list集合用来储存error信息。
“errors.input.required”和”errors.check.noselect”是为了取出properties里的信息。
/**
* 类调用
*
* @param messageSource
* @return
*/
public static ValidateRegister getInstance(MessageSource messageSource) {
return new ValidateRegister(messageSource);
}
/**
* 初始化类方法
*
* @param messageSource
*/
public ValidateRegister(MessageSource messageSource) {
this.messageSource = messageSource;
}
/**
* 非空校验
*
* @param map
* @throws Exception
*/
@Override
public void checkEmptyParameter(Map<String[], String> map, String propertiesKey){
for (Entry<String[], String> entry : map.entrySet()) {
if (ValidateUtils.isEmpty(entry.getValue())) {
errorMessages.add(this.addActionError(propertiesKey, entry.getKey()));
}
}
}
/**
* 验证name
*
*/
@Override
public void checkName(String name, String[] propertiesParam) {
boolean isIncorrectName = name.length() > Constant.NAME_MAX_LENGTH || !ValidateUtils.isZenkaku(name);
if (isIncorrectName) {
errorMessages.add(this.addActionError("errors.form.doubleMaxlength", propertiesParam));
}
}
/**
* 验证KanaName
*
*/
@Override
public void checkKanaName(String kanaName, String[] propertiesParam) {
boolean isIncorrectKanaName = kanaName.length() > Constant.KANA_NAME_MAX_LENGTH
|| !ValidateUtils.isKatakana(kanaName);
if (isIncorrectKanaName) {
errorMessages.add(this.addActionError("errors.form.doubleMaxlength", propertiesParam));
}
}
/**
* 验证密码
*/
@Override
public void checkPassword(String password, String[] propertiesParam) {
boolean isIncorrectPasswprd = password.length() > Constant.PASSWORD_MAX_LENGTH
|| password.length() < Constant.PASSWORD_MIN_LENGTH
|| !ValidateUtils.isHalfsizeAlphabetAndNumeric(password);
if (isIncorrectPasswprd) {
errorMessages.add(this.addActionError("errors.form.singleNumber.password.range", propertiesParam));
}
}
/**
* 验证密码是否相等
* @param password
* @param againPassword
*/
@Override
public void checkEqualPassword(String password, String againPassword) {
if (!password.equals(againPassword)) {
errorMessages.add(this.addActionError("error.password.twice.confirm"));
}
}
/**
* 校验密码是否输入邮件地址一部分
* @param mailAddress
* @param password
*/
@Override
public void checkMailContantPassword(String mailAddress, String password) {
if (mailAddress.indexOf(password) != -1) {
errorMessages.add(this.addActionError("errors.equals.password.mail"));
}
}
/**
* 校验生年月
* @param yearOfBirth
* @throws Exception
*/
@Override
public void checkYearOfBirth(String yearOfBirth) {
boolean isIncorrectYearOfBirth = yearOfBirth.length() > Constant.YEAR_OF_BIRTH_MAX_LENGTH
|| !ValidateUtils.isHalfsizeNumeric(yearOfBirth)
|| Integer.valueOf(yearOfBirth) < Constant.YEAR_OF_BIRTH_MIN_YEAR
|| Integer.valueOf(yearOfBirth) > Constant.YEAR_OF_BIRTH_MAX_YEAR;
if (isIncorrectYearOfBirth) {
errorMessages.add(this.addActionError("errors.form.yearOfBirth.range"));
}
}
@Override
public String addActionError(String anErrorMessage, String[] field) {
return this.messageSource.getMessage(anErrorMessage, field, Locale.getDefault());
}
@Override
public String addActionError(String anErrorMessage) {
return this.messageSource.getMessage(anErrorMessage, null, Locale.getDefault());
}
最后从errorMessages里获取信息就行了。