2017-01-02 2 views
2

다음 컨트롤러 로직이 있습니다. 그러나 존재하지 않는 페이지 (예 :/임의 페이지)로 이동하면 TemplateInputException으로 끝납니다. 어떻게하면 이것을 포착하고 404 페이지로 이동할 수 있습니까? 스프링 부팅핸들 org.thymeleaf.exceptions.TemplateInputException

@ExceptionHandler(Exception.class) 
public ModelAndView handleAllException(Exception ex) { 

    ModelAndView model = new ModelAndView("error/generic_error"); 
    model.addObject("errMsg", "this is Exception.class"); 

    return model; 

} 
+1

당신이 해결책을 발견해야

@RequestMapping(value = { "{path:(?!resources|error).*$}", "{path:(?!resources|error).*$}/**" }, headers = "Accept=text/html") public String index(final HttpServletRequest request) { try { String path = (String) request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); return path.split("/")[1]; } catch (Exception e) { log.error("Failed to render the page. {}",e); return "error/general"; } } 

Thymeleaf는 무시 ExceptionHandler 것 같다 아직? – Kousalik

답변

0

내 해결 방법이 문제 (exceptionmessage PARAM와도이다) :

@Controller 
public class ErrorController implements org.springframework.boot.autoconfigure.web.ErrorController { 
    private static final String ERROR_PATH = "/error"; 

    @Autowired 
    private ErrorAttributes errorAttributes; 

    @Override 
    public String getErrorPath() { 
     return ERROR_PATH; 
    } 

    @RequestMapping(ERROR_PATH) 
    public String error(HttpServletRequest request, Model model) { 
     Map<String, Object> errorMap = errorAttributes.getErrorAttributes(new ServletRequestAttributes(request), false); 
     String exception = (String) errorMap.get("exception"); 
     if (exception != null && exception.contains("TemplateInputException")) { 
      errorMap.put("message", "Неверный запрос"); 
     } 
     model.addAllAttributes(errorMap); 
     return "exception"; 
    } 
}