【问题标题】:Spring framework custom 404 error specific caseSpring框架自定义404错误具体案例
【发布时间】:2022-01-03 13:29:24
【问题描述】:

如果我在 URL 路径中包含 webApp 根目录 myweb,我可以获得自定义 404 错误页面 -

http://localhost/myweb/users/david

我在 web.xml 中添加了以下条目以获取自定义 404 页面 -

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/pageNotFound.jsp</location>
  </error-page>

但是,当我点击以下 URL 时,我仍然收到 Spring 默认的 404 错误页面 -

http://localhost/users/david

为什么在 URL 路径中没有 webApp 根目录就不能工作?有没有办法为此获得相同的自定义 404 Not Found Page?

编辑:

我的相关 web.xml 代码 -

<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>myweb</param-value>
  </context-param>

  <context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.gridpoint.energy.web.common.servlet.listeners.RequestContextInitializer</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
  </listener>
<!-- Dispatch Servlet -->
  <servlet>
    <servlet-name>services-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <servlet-name>services-servlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>services-servlet</servlet-name>
    <url-pattern>/applications/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>services-servlet</servlet-name>
    <url-pattern>/healthCheck</url-pattern>
  </servlet-mapping>

  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/pageNotFound.jsp</location>
  </error-page>

【问题讨论】:

  • 我认为您的第一个请求到达了 tomcat 的“默认”servlet,该 servlet 以默认错误页面响应。
  • @Barracuda - 所以我需要在项目根目录上添加另一个 web.xml 来为其获取自定义 404 页面?
  • 我不确定如何更改 tomcat 的默认错误页面(如果这里是这种情况)。等其他人回答。据我所知,您在 web.xml 中定义的默认错误页面会影响您在应用程序中定义的所有 servlet,但不会影响 tomcat 的。同样,不是 100% 确定。试试这个stackoverflow.com/questions/47869500/apache-tomcats-error-page

标签: java spring spring-mvc spring-security


【解决方案1】:

在我的例子中,我使用 Spring mvc 4 创建了客户控制器 ErrorController

我设置下一个配置web.xml。

<error-page>
    <location>/errors</location>
</error-page>

接下来。我创建了控制器。我分享了课堂。 (ModelAndView 是我的 .jsp 的路径(异常/错误页面)。

@Controller
public class ErrorController {
 
    @RequestMapping(value = "errors", method = RequestMethod.GET)
    public ModelAndView renderErrorPage(HttpServletRequest httpRequest, ModelMap model) {
         
        ModelAndView errorPage = new ModelAndView("exception/errorPage");
        String errorMsg = "";
        String customerMsg = "";
        
        int httpErrorCode = getErrorCode(httpRequest);
 
        switch (httpErrorCode) {
            case 400: {
                errorMsg     = "Http Error Code: 400. Bad Request";
                customerMsg  = "";
                break;
            }
            case 401: {
                errorMsg     = "Http Error Code: 401. Unauthorized";
                customerMsg  = "";
                break;
            }
            case 403: {
                errorMsg = "Http Error Code: 403. access denied";
                customerMsg  = "";
                break;
            }
            case 404: {
                errorMsg = "Http Error Code: 404. Resource not found";
                customerMsg  = "";
                break;
            }
            case 500: {
                errorMsg = "Http Error Code: 500. Internal Server Error";
                customerMsg  = "";
                break;
            }
        }
      
        errorPage.addObject("errorMsg", errorMsg);
        errorPage.addObject("customerMsg", customerMsg);
        return errorPage;
    }
     
    private int getErrorCode(HttpServletRequest httpRequest) {
        return (Integer) httpRequest
          .getAttribute("javax.servlet.error.status_code");
    }
}

分享了基本的errorPage.jsp

<!DOCTYPE html>
<html lang="es">
<head>
    <title>Error</title>
</head>
<body>
    
    <table>
        <tr>
            <td>
                <h1>${errorMsg}</h1>
            </td>
        </tr>
        <tr>
            <td>
                <h1>${customerMsg}</h1>
            </td>
        </tr>
        <tr>
            
        </tr>
    </table>
</body>
</html>

希望这个例子对你有所帮助。

【讨论】:

  • 谢谢,但这对我不起作用。就我而言,问题是缺少 webApp 根目录 myweb(在 URL 中)。否则,所有这些解决方案都会起作用。
猜你喜欢
  • 2015-09-04
  • 2014-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多