使用WebMvcConfigurationSupport可以設置所有翻頁的單頁件數(jpa)時,HandlerInterceptor設置會覆蓋,需要在support中註冊。

使用WebMvcConfigurationSupport可以設置所有翻頁的單頁件數(jpa)時,HandlerInterceptor設置會覆蓋,需要在support中註冊。

翻頁的單頁件數設定

addArgumentResolvers方法

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.util.ResourceUtils;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import jp.co.pia.ticket.sports.hawks.seasonseat.backoffice.common.BusinessContents;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.controller.LogInterceptor;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Autowired
    private BusinessContents businessContents;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();

        try {
            // ページ単位に表示する件數
            resolver.setFallbackPageable(PageRequest.of(0, Integer.valueOf(businessContents.getPage_max_count())));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        argumentResolvers.add(resolver);
        super.addArgumentResolvers(argumentResolvers);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/");
        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor());
    }

}

靜態文件地址設定

addResourceHandlers

註冊HandlerInterceptor

addInterceptors

每個request出log,LogInterceptor。

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.component.LogConfig;

public class LogInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger("ACCESS");

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        JSONObject json = new JSONObject();
        // セッションID
        json.put(LogConfig.SESSION_ID, request.getSession().getId());
        // ユーザーID
        Principal auth = request.getUserPrincipal();
        String userId = null;
        if (auth != null) {
            userId = auth.getName();
        }
        if (StringUtils.isEmpty(userId)) {
            json.put(LogConfig.AUTH_NAME, LogConfig.NONE_STR);
        } else {
            json.put(LogConfig.AUTH_NAME, userId);
        }
        // クライアントIP
        json.put(LogConfig.IP, request.getRemoteAddr());
        // httpメソッド
        json.put(LogConfig.HTTP_METHOD, request.getMethod());
        // URL
        json.put(LogConfig.URL, request.getRequestURI());
        // ステータスコード
        json.put(LogConfig.HTTP_STATUS_CODE, response.getStatus());
        // ログ種類(開始)
        json.put(LogConfig.LOG_TYPE, LogConfig.START);
        // // 出力メッセージ
        // Map<String, String[]> map = request.getParameterMap();
        // map.forEach((key, value) -> {
        // json.put(key, request.getParameter(key));
        // });

        logger.info(json.toString());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)
        throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        JSONObject json = new JSONObject();
        // セッションID
        json.put(LogConfig.SESSION_ID, request.getSession().getId());
        // ユーザーID
        Principal auth = request.getUserPrincipal();
        String userId = null;
        if (auth != null) {
            userId = auth.getName();
        }
        if (StringUtils.isEmpty(userId)) {
            json.put(LogConfig.AUTH_NAME, LogConfig.NONE_STR);
        } else {
            json.put(LogConfig.AUTH_NAME, userId);
        }
        // クライアントIP
        json.put(LogConfig.IP, request.getRemoteAddr());
        // httpメソッド
        json.put(LogConfig.HTTP_METHOD, request.getMethod());
        // URL
        json.put(LogConfig.URL, request.getRequestURI());
        // ステータスコード
        json.put(LogConfig.HTTP_STATUS_CODE, response.getStatus());
        // ログ種類(終了)
        json.put(LogConfig.LOG_TYPE, LogConfig.END);

        logger.info(json.toString());
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章