• baseController


    package cn.ylu.stxy.xj2014.controller;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authz.AuthorizationException;
    import org.apache.shiro.session.Session;
    import org.apache.shiro.subject.Subject;
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.InitBinder;
    import org.springframework.web.bind.annotation.ModelAttribute;
    
    import com.google.gson.Gson;
    
    import cn.ylu.stxy.xj2014.parameter.Attribute;
    import cn.ylu.stxy.xj2014.parameter.Categorys;
    import cn.ylu.stxy.xj2014.parameter.Directory;
    import cn.ylu.stxy.xj2014.vo.ResultVo;
    import cn.ylu.stxy.xj2014.vo.UserVo;
    
    public class BaseController {
    
        protected HttpServletRequest request;
        protected HttpServletResponse response;
        
        public int defEnd = 0;
        public int defSTART = 29;
    
        protected Gson gson = new Gson();
    
    
        @ModelAttribute
        public void setReqAndResp(HttpServletRequest request, HttpServletResponse response) {
            this.request = request;
            this.response = response;
            
        }
    
        /**
         * 登录认证异常
         */
        // @ExceptionHandler({ UnauthenticatedException.class,
        // AuthenticationException.class })
    /*    @ExceptionHandler({ UnauthenticatedException.class})
        public String authenticationException(HttpServletRequest request, HttpServletResponse response) {
            if (isAjaxRequest(request)) {
                // 输出JSON
                System.out.println("检测到Ajax");
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("code", "-999");
                map.put("message", "未登录");
                writeJson(map, response);
                return null;
            } else {
                return "redirect:/system/login";
            }
        }*/
    
        /**
         * 权限异常
         */
    
        @ExceptionHandler(AuthorizationException.class)
        public String authorizationException(HttpServletRequest request, HttpServletResponse response) {                        
            if (isAjaxRequest(request)) {
                // 输出JSON        
                ResultVo resultVo = failed();
                writeJson(resultVo, response);
                return null;
            } else {
                return "redirect:404";
            }
        }
    
        /**
         * 输出JSON
         *
         * @param response
         * @author SHANHY
         * @create 2017年4月4日
         */
        private void writeJson(ResultVo resultVo, HttpServletResponse response) {
            PrintWriter out = null;
            try {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                out = response.getWriter();
                out.write(gson.toJson(resultVo));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    
        /**
         * 是否是Ajax请求
         *
         * @param request
         * @return*/
        public static boolean isAjaxRequest(HttpServletRequest request) {
            String requestedWith = request.getHeader("x-requested-with");
            if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
                return true;
            } else {
                return false;
            }
        }
    
        @InitBinder
        protected void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Date.class,
                    new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
            binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
        }
    
        /**
         * 返回主目录页面
         * 
         * @param htmlName
         * @return
         */
        public String HtmlModel(String htmlName) {
    
            return "/" + htmlName;
    
        }
    
        /**
         * 重定向
         * 
         * @param htmlName
         * @return
         */
        public String redirectHtmlModel(String htmlName) {
    
            return "redirect:" + HtmlModel(htmlName);
    
        }
    
        /**
         * 返回含目录页面
         * 
         * @param directory
         * @param htmlName
         * @return
         */
        public String HtmlModel(Directory directory, String htmlName) {
    
            return "/" + directory.val() + "/" + htmlName;
    
        }
    
        /**
         * 重定向
         * 
         * @param directory
         * @param htmlName
         * @return
         */
        public String redirectHtmlModel(Directory directory, String htmlName) {
    
            return "redirect:" + HtmlModel(directory, htmlName);
    
        }
    
        public ResultVo success() {
            return success(null);
        }
    
        public ResultVo success(Object data) {
            ResultVo result = new ResultVo();
            result.setState(true); // 默认返回成功
            result.setData(data);
            result.setStateCode(Categorys.CG.val());
            result.setMessage("操作成功");
            return result;
        }
    
        public ResultVo success(Object data, String mess) {
            ResultVo resultVo = success(data);
            resultVo.setMessage(mess);
            return resultVo;
        }
        public ResultVo failed() {
            ResultVo result = new ResultVo();
            result.setMessage("权限不足");
            result.setState(false); // 默认返回成功
            result.setStateCode(Categorys.SB.val());
            result.setData(null);
            return result;
        }
        public ResultVo error() {
            ResultVo result = new ResultVo();
            result.setMessage("系统错误");
            result.setState(false); // 默认返回成功
            result.setStateCode(Categorys.ERROR.val());
            result.setData(null);
            return result;
        }
    
        public ResultVo error(String msg) {
            ResultVo result = error();
            result.setMessage(msg);
            return result;
        }
    
        public ResultVo error(String stateCode, String msg) {
            ResultVo result = error(msg);
            result.setStateCode(stateCode);
            return result;
        }
        /**
         * 获取shiro 的session
         * 
         * @return
         */
        protected Session getSession() {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            return session;
        }
        /**
         * 获取登录用户信息
         * 
         * @return
         */
        public UserVo getUserVo() {
            Object session = request.getSession().getAttribute(Attribute.LOGIN_USER.getName());
            return session == null ? null : (UserVo) session;
        }
    
        /**
         * 获取请求完整路径
         * 
         * @param request
         * @return
         */
        public String getUrl(HttpServletRequest request) {
            String url = request.getRequestURI();
            String params = "";
            if (request.getQueryString() != null) {
                params = request.getQueryString().toString();
            }
            if (!"".equals(params)) {
                url = url + "?" + params;
            }
            return url;
        }
    
    
    
        /**
         * 获取日期
         * 
         * @param day
         *            天
         */
        public String getDate(int day) {
            StringBuffer s = new StringBuffer();
            Calendar c = Calendar.getInstance();
            int currentDay = c.get(Calendar.DATE);
            if (day < 0) {
                c.add(Calendar.YEAR, -1);
                c.set(Calendar.DATE, currentDay);
            } else if (day == 29) {
                c.add(Calendar.MONTH, -1);
                c.set(Calendar.DATE, currentDay);
            } else {
                c.add(Calendar.DATE, -day);
            }
    
            s.append(c.get(Calendar.YEAR) + "-");
            s.append((c.get(Calendar.MONTH) + 1) < 10 ? ("0" + (c.get(Calendar.MONTH) + 1)) : (c.get(Calendar.MONTH) + 1));
            s.append("-");
            s.append(c.get(Calendar.DATE) < 10 ? ("0" + c.get(Calendar.DATE)) : c.get(Calendar.DATE));
            return s.toString();
        }
    
        protected String log() {
            String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
            StringBuilder stringBuilder = new StringBuilder(getUserVo() == null ? "" : getUserVo().getName());
            stringBuilder.append("请求").append(methodName);
            return stringBuilder.toString();
        }
    
        protected String log(Map map) {
            String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
            StringBuilder stringBuilder = new StringBuilder(getUserVo() == null ? "" : getUserVo().getName());
            stringBuilder.append("请求").append(methodName).append(gson.toJson(map));
            return stringBuilder.toString();
        }
    
        protected String log(Object map) {
            String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
            StringBuilder stringBuilder = new StringBuilder(getUserVo() == null ? "" : getUserVo().getName());
            stringBuilder.append("请求").append(methodName);
            try {
                stringBuilder.append(gson.toJson(map));
            } catch (Exception e) {
                stringBuilder.append(map.toString());
            }
            return stringBuilder.toString();
        }
    }
  • 相关阅读:
    ubuntu老版本下载地址
    Device Tree
    内存映射与访问机制
    makefile要点
    lds文件
    测试风险问题探讨
    2 Player and N Coin
    google maps v3 添加自定义图标(marker,overlay)
    Evatech 机器人修剪器
    受蚂蚁启发的四足机器人链接在一起克服障碍
  • 原文地址:https://www.cnblogs.com/sylwh/p/9289581.html
Copyright © 2020-2023  润新知