首页>代码>java web敏捷开发平台JeeWeb项目实例>/jeeweb-mybatis/src/main/java/cn/jeeweb/core/common/controller/BaseBeanController.java
package cn.jeeweb.core.common.controller;

import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import cn.jeeweb.core.utils.MessageUtils;
import cn.jeeweb.core.utils.ReflectionUtils;
import cn.jeeweb.core.utils.StringUtils;
import java.io.Serializable;
import java.util.List;

public abstract class BaseBeanController<Entity extends Serializable> extends BaseController {

	/**
	 * 实体类型
	 */
	protected final Class<Entity> entityClass;

	protected BaseBeanController() {
		this.entityClass = ReflectionUtils.getSuperGenericType(getClass());
	}

	protected Entity newModel() {
		try {
			return entityClass.newInstance();
		} catch (Exception e) {
			throw new IllegalStateException("can not instantiated model : " + this.entityClass, e);
		}
	}

	/**
	 * 共享的验证规则 验证失败返回true
	 *
	 * @param m
	 * @param result
	 * @return
	 */
	protected boolean hasError(Entity entity, BindingResult result) {
		Assert.notNull(entity);
		return result.hasErrors();
	}

	/**
	 * 
	 * @title: errorMsg
	 * @description: 错误信息组装
	 * @param result
	 * @return
	 * @return: String
	 */
	protected String errorMsg(BindingResult result) {
		String errorMsg = "";
		if (result.getErrorCount() > 0) {
			List<ObjectError> objectErrorList = result.getAllErrors();
			for (ObjectError objectError : objectErrorList) {
				String message = MessageUtils.getMessage(objectError.getCode(), objectError.getDefaultMessage(),
						objectError.getArguments());
				if (!StringUtils.isEmpty(message)) {
					errorMsg = errorMsg + message + "<br />";
				}
			}
		}
		return errorMsg;
	}

	/**
	 * @param backURL
	 *            null 将重定向到默认getViewPrefix()
	 * @return
	 */
	protected String redirectToUrl(String backURL) {
		if (StringUtils.isEmpty(backURL)) {
			backURL = getViewPrefix();
		}
		if (!backURL.startsWith("/") && !backURL.startsWith("http")) {
			backURL = "/" + backURL;
		}
		return "redirect:" + backURL;
	}

}
最近下载更多
jaonsang  LV25 7月20日
好我累个去哦  LV15 2023年8月5日
若你能读懂我的笑  LV3 2023年3月15日
getset  LV8 2022年12月10日
abc562311934  LV4 2022年10月8日
jiyun2021  LV9 2022年7月8日
aqin_qin  LV1 2022年6月10日
981352576  LV4 2022年5月10日
tangjj7260  LV18 2022年4月24日
crosa_Don  LV18 2022年4月1日
最近浏览更多
liyan54188  LV2 2024年6月25日
15719908287  LV10 2024年6月19日
坚持仙蛊 2024年6月13日
暂无贡献等级
xiao1111  LV3 2024年5月14日
denliv_hui  LV14 2024年4月2日
189453722  LV1 2024年3月18日
1134116035 2024年2月21日
暂无贡献等级
微信网友_6829521472425984  LV5 2024年1月20日
dsadasdwf  LV12 2024年1月12日
hxh666  LV2 2023年12月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友