1. 程式人生 > >工具類封裝之--BaseController

工具類封裝之--BaseController

OS sig pen ring tty author vax state IT

package cn.xxx.base;

import cn.xxx.gecustomer.beans.GeCustomer;
import cn.xxx.gecustomer.beans.GeCustomerProperty;
import cn.xxx.utils.UserPersonalConstant;
import cn.xxx.utils.WebUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.support.ApplicationObjectSupport; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.support.RequestContext; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException;
import java.util.List; import java.util.UUID; /** * 所有controller的基類 * * @author * */ public abstract class BaseController extends ApplicationObjectSupport { public Logger log = LoggerFactory.getLogger(this.getClass()); @Value("${project.version}") public String ver; /** * 要跳轉的頁面 * * @Title: go * @Description: TODO * @param path * @return * @author 2017年9月15日 下午1:51:10 */ protected ModelAndView go(String path) { return new ModelAndView(path); } /** * 到bean信息 * * @param name * @return */ protected Object getObject(String name) { return this.getApplicationContext().getBean(name); } /** * 獲取登錄用戶信息 * * @return */ public IUserInfo getUserSessionVo() { Subject subject = SecurityUtils.getSubject(); Object obj = null;//subject.getSession().getAttribute(UserAuthenticationRealm.LOGIN_SESSION_ATTR); if (obj instanceof IUserInfo) { return (IUserInfo) obj; } return null; } public String getOrganCode() { return (String) this.getSession().getAttribute(WebUtil.ORGAN_CODE); } public HttpServletRequest getRequest() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); } public HttpSession getSession() { return getRequest().getSession(); } public HttpServletResponse getResponse() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); } /** * 獲取國際化信息 * * @param key * @return */ public String getI18nMsg(String key) throws Exception { // 從後臺代碼獲取國際化信息 String value = new RequestContext(this.getRequest()).getMessage(key); return value != null ? value : ""; } /** * 返回國際化國家標識串 * * @return * @throws Exception */ public String getI18nCountry() throws Exception { String lc = null; RequestContext requestContext = new RequestContext(this.getRequest()); String language = requestContext.getLocale().getLanguage(); String country = requestContext.getLocale().getCountry(); if (country == null || "".equals(country)) { if ("zh".equals(language)) { country = "CN"; } else if ("en".equals(language)) { country = "US"; } } // logger.debug("本地國際化語言:" + language + "本地國際化國家:" + country); lc = "_" + language + (StringUtils.isEmpty(country) ? "" : "_" + country); return lc; } /** * 請求方式判斷 * * @param request * @return */ public boolean isAjaxRequest(HttpServletRequest request) { if (!(request.getHeader("accept").indexOf("application/json") > -1 || (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1) || "XMLHttpRequest".equalsIgnoreCase(request.getParameter("X_REQUESTED_WITH")))) { return false; } return true; } /** * 保存信息到request中 * * @param key * @param value */ public void setRequestAttribute(String key, Object value) { this.getRequest().setAttribute(key, value); } protected String getCookie(String cookieName) { Cookie[] cookies = getRequest().getCookies(); for (Cookie cookie : cookies) { if (cookieName.equals(cookie.getName())) { return cookie.getValue(); } } return null; } /** * 設置 cookie * * @param cookieName * @param value * @param age */ protected void setCookie(String cookieName, String value, int age) { Cookie cookie = new Cookie(cookieName, value); cookie.setMaxAge(age); // cookie.setHttpOnly(true); getResponse().addCookie(cookie); } /** * 返回成功信息對象 * * @return */ public ReturnInfoVo<Object> getReturnSuccessVo() throws Exception { return new ReturnInfoVo<Object>(true, this.getI18nMsg(Constants.COMMON_SUCCESS_MSG_KEY)); } /** * 解析返回信息,處理國際化部分 * * @param riv * @return */ public <T> ReturnInfoVo<T> returnInfoVoParser(ReturnInfoVo<T> riv) { try { List<ReturnInfoVo<T>.MsgVo> msgList = riv.getMsgList(); if (!msgList.isEmpty()) { StringBuilder sb = new StringBuilder(); ReturnInfoVo<T>.MsgVo msg; String info; for (int i = 0; i < msgList.size(); i++) { msg = msgList.get(i); if (Constants.NEEDI18N) { info = this.getI18nMsg(msg.getMsg()); } else { info = msg.getMsg(); } if (msg.getParams() != null && msg.getParams().length > 0) { info = String.format(info, msg.getParams()); } sb.append(info); sb.append(";"); if (i != msgList.size() - 1) { sb.append("\r\n"); } } riv.setMsg(sb.toString()); } else { if (riv.isSuccess()) { riv.setMsg(this.getI18nMsg(Constants.COMMON_SUCCESS_MSG_KEY)); } else { riv.setMsg(this.getI18nMsg(Constants.COMMON_FAILED_MSG_KEY)); } } } catch (Exception e) { logger.error("解析返回信息對象失敗", e); } return riv; } /** * 方法名稱: render<br> * 描述:返回給瀏覽器 */ public void render(String text, String contentType){ HttpServletResponse response; try{ response = getResponse(); response.setContentType(contentType); response.getWriter().write(text); response.getWriter().flush(); } catch (IOException e) { throw new IllegalStateException(e); } } /** * 方法名稱: renderText<br> * 描述:返回普通文本瀏覽器 */ public void renderText(String text){ render(text, "text/plain;charset=UTF-8"); } /** * 方法名稱: renderJson<br> * 描述:返回JSON格式數據 */ public void renderJson(String text){ render(text,"text/json;charset=UTF-8"); } /** * 方法名稱: getCurrentLoginGeCustomer<br> * 描述:獲取當前登錄用戶<br> * 保全、登錄 * 作者: <br> * 修改日期:2014-9-30下午07:06:26<br> */ protected GeCustomer getCurrentLoginGeCustomer() { Object obj = this.getRequest().getSession().getAttribute(UserPersonalConstant.LOGIN_USER); if (obj instanceof GeCustomer) { return (GeCustomer) obj; } else { return null; } } /** * 方法名稱: getCurrentLoginGeCustomerProperty<br> * 描述:獲取當前登錄用戶屬性信息<br> * 作者: <br> * 修改日期:2014-9-30下午07:06:26<br> */ protected GeCustomerProperty getCurrentLoginGeCustomerProperty() { Object obj = this.getRequest().getSession().getAttribute(UserPersonalConstant.LOGIN_USER_PROPERTY); if (obj instanceof GeCustomerProperty) { return (GeCustomerProperty) obj; } else { return null; } } /** * 獲取農銀在線登錄用戶 <br/> * Date: 2017年6月8日 下午4:51:55 * 綁定、產品、支付 * @author * @param * @return */ protected GeCustomer getOnlineCurrentLoginGeCustomer() { Object obj = this.getRequest().getSession().getAttribute(UserPersonalConstant.ONLINE_LOGIN_USER); if (obj instanceof GeCustomer) { return (GeCustomer) obj; } else { return null; } } /** * 方法名稱:initCurrentLoginGeCustomer<br> * 描述:存放登錄用戶信息<br> * 作者:<br> * 修改日期:2014年8月18日下午7:57:07<br> */ protected void initCurrentLoginGeCustomer() { Object obj = this.getRequest().getSession().getAttribute(UserPersonalConstant.LOGIN_USER); if (obj != null && obj instanceof GeCustomer) { // 有登陸用戶,帶登陸信息到填寫投保信息頁 this.setRequestAttribute("geCustomer", (GeCustomer) obj); } } /** * 方法名稱: getUUID<br> * 描述:獲得唯一標識(目前用於驗證表單提交唯一性)<br> * 作者: <br> * 修改日期:2014年10月13日上午3:15:24<br> */ protected String getUUID() { return UUID.randomUUID().toString(); } }

工具類封裝之--BaseController