Custom 404 error page not working with Struts 2

前端 未结 2 1194
旧巷少年郎
旧巷少年郎 2021-01-27 00:21

I am converting a JSP/Servlet dynamic web project to Struts 2 and the custom error pages (that previously worked with the JSP/Servlet version) have stopped working.

When

2条回答
  •  故里飘歌
    2021-01-27 01:15

    Instead of custom error page create a custom error action that returns a result to a custom error page.

    struts.xml:

    
      /404.jsp
      /500.jsp
     
    

    ErrorAction:

    public class ErrorAction extends ActionSupport {
    
      private Integer statusCode;
      //getter and setter
    
      @Override
      public String execute() {
        if (stausCode != null)
        switch (statusCode){
          case 404:
            return "404";
          case 500:
            return "500";
        }
      }
    }
    

    web.xml:

    
        404
        /error_code.jsp
    
    
    
        500
        /error_code.jsp
    
    

    error_code.jsp:

    <% 
     Integer statusCode = (Integer) request
                           .getAttribute("javax.servlet.error.status_code");
    
     response.sendRedirect(pageContext.getServletContext().getContextPath()
                           +"/error?statusCode="+statusCode ); 
    %>
    

提交回复
热议问题