簡體   English   中英

使用java配置在Spring MVC中進行404錯誤重定向

[英]404 error redirect in Spring MVC with java config

我有簡單的Spring MVC應用程序,我希望在我的Advice Controller類中找到404 Not found異常

組態:

   public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class, SecurityConfig.class};
    }


    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new SessionListener());
        FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
        encodingFilter.setInitParameter("encoding", "UTF-8");
        encodingFilter.setInitParameter("forceEncoding", "true");
        encodingFilter.addMappingForUrlPatterns(null, true, "/*");
    }
}

控制器:

@Controller
@RequestMapping("/")
public class HomeController {
 @RequestMapping(value = "/error/", method = RequestMethod.GET)
    public String error(){return "error";}
}

ControllerAdvice:

@ControllerAdvice
public class AdviceController {

    @ExceptionHandler(MyOwnException.class)
    @ResponseStatus(value= HttpStatus.BAD_REQUEST)
    public String checkoutException(CheckoutException e, HttpServletRequest httpServletRequest)   {
                return "error";
    }
}

當我手動拋出MyOwnException但我無法獲取如何捕獲NoHandlerFound異常時,我可以捕獲自己的異常。 當沒有控制器方法來處理請求時,我需要發送404錯誤代碼和相應的error.jsp頁面

如果您的webapp使用的是web.xml那么它非常簡單 - 只需添加以下內容(假設使用InternalResourceViewResolver ,前綴指向您的WEB-INF視圖文件夾和后綴.jsp )。 您也可以擁有其他錯誤代碼的多個錯誤頁面元素。

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

如果你沒有使用web.xml它會更復雜,你必須定義和注冊你自己的ExceptionResolver 看一下這個spring.io博客文章 ,了解如何執行此操作的詳細信息。


(評論后編輯)

如果你想捕獲NoHandlerFound異常,首先必須告訴Spring通過直接在DispatcherServlet中設置一個標志來拋出它。 為此,在AppInitializer類中添加DispatcherServlet定義,在您當前正在執行的操作之前添加標志:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    servletContext.addListener(new SessionListener());
    //BEGIN OF NEW CODE
    WebApplicationContext context = getContext();
    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    //we did all this to set the below flag
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",dispatcherServlet );
    //END OF NEW CODE
    FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
    encodingFilter.setInitParameter("encoding", "UTF-8");
    encodingFilter.setInitParameter("forceEncoding", "true");
    encodingFilter.addMappingForUrlPatterns(null, true, "/*");
}

然后,您可以直接在AdviceController捕獲NoHandlerFound異常:

@ControllerAdvice
public class AdviceController {
    //..
    @ExceptionHandler(NoHandlerFoundException.class)
    public String dealWithNoHandlerFoundException(CheckoutException e, HttpServletRequest httpServletRequest)   {
                return "error";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM