「Spring MVC」404、500エラー画面を指定するサンプル

1.web.xmlに404、500を指定

<error-page>
        <error-code>404</error-code>
        <location>/404</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/500</location>
    </error-page>

2.Controller側

package com.arkgame.controller;

import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ErrPageController {

    Logger log = LogManager.getLogger(ErrorPageController.class);

    @RequestMapping("404")
    public String errPageF() {
        log.info("400 error");
        return "err404";
    }

    @RequestMapping("500")
    public String errPageB() {
        log.error("500 error");
        return "err500";
    }

}

 

SpringMVC

Posted by arkgame