0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SpringBoot_エラー画面と404画面を表示切替(スマホ版も)

Posted at

ErrorViewResolverで切り替える

ErrorViewResolverで簡単に切り替え可能
さらにintercepterでPCかスマホか判定していれば対応した画面に飛ばしてくれる

SampleErrorViewResolver

    @Component
    public class SampleErrorViewResolver implements ErrorViewResolver {

        /**
         * 表示決定
         *
         * @param request
         * @param status
         * @param model
         * @return ModelAndView or null
         */
        @Override
        public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
            boolean mobile = (boolean) request.getAttribute("mobile");
            ModelAndView modelAndView = new ModelAndView();
            mav.addAllObjects(model);

            if (status == HttpStatus.NOT_FOUND) {
                // 404
                if (!mobile) {
                    modelAndView.setViewName("/404");
                } else {
                    modelAndView.setViewName("/mobile/404");
                }

            } else {
                // 404 以外はエラー画面へ
                if (!mobile) {
                    modelAndView.setViewName("/error");
                } else {
                    modelAndView.setViewName("/mobile/error");
                }
            }
            return modelAndView;
        }
    }

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?