首页>代码>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;
	}

}
最近下载更多
好我累个去哦  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日
Zeorwyc  LV8 2021年10月9日
最近浏览更多
denliv_hui  LV13 4月2日
189453722  LV1 3月18日
1134116035 2月21日
暂无贡献等级
dsadasdwf  LV12 1月12日
hxh666  LV2 2023年12月11日
蹇金金  LV7 2023年11月6日
ggl163163eel  LV2 2023年10月19日
zj20020510  LV5 2023年9月18日
yangxb2  LV10 2023年9月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友