【问题标题】:<error-page> setup doesn't work in Spring MVC<error-page> 设置在 Spring MVC 中不起作用
【发布时间】:2016-06-07 19:16:02
【问题描述】:

问题

我想向用户显示自定义错误页面。简单地说,web.xml 上的&lt;error-page&gt; 似乎不起作用。我可以猜到问题出在哪里,但我需要了解它到底为什么不起作用。


我的设置

我在我的 spring mvc 应用程序中的 web.xml 上设置了&lt;error-page&gt;。以下是设置。

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>*.*</url-pattern>
    <url-pattern>*.do</url-pattern>     
</servlet-mapping>

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

我的错误页面位于...

WEB-INF
└   views
    └   Errorpages
        └   ErrorPage.jsp

为了您的信息,我也在下面尝试了这些方法,但这些方法都不起作用。

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/views/Errorpages/ErrorPage.jsp</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/views/Errorpages/ErrorPage.jsp</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/Errorpages/ErrorPage.jsp</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/ErrorPage.jsp</location>
</error-page>

<!-- Or tried those above without the .jsp that comes after ErrorPage -->
<!-- And also, tried those after moving the resources out of WEB-INF -->

第二次尝试

我查看了服务器日志,发现 Spring 尝试通过重定向查找资源,因此我改变了计划并尝试通过 Spring MVC 中的典型视图返回来实现这个目标。

我做了这个动作

@RequestMapping("/error")
public String throwsErrorPage(Locale locale, Model model) {
    logger.info("Errorpage action..." + locale.getLanguage());              
    return "/Errorpages/ErrorPage";
}

我的预期部分正确,因为它成功地调用了这个动作。我的服务器日志很清楚。

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/notfound] in DispatcherServlet with name 'appServlet'
INFO : com.company.web.controller.HomeController - Errorpage action...ko

如您所见,我通过在 url 上键入 notfound 生成了 404 错误,并且它重定向到 /error,正如我在 web.xml 中的 &lt;location&gt; 中指定的那样。但它不会返回视图。

为确保操作返回视图,我尝试更改操作的类型,例如...

@RequestMapping("/error")
public ModelAndView throwsErrorPage(Locale locale, ModelAndView mv) {
    logger.info("Errorpage action..." + locale.getLanguage());              
    return mv.setViewName("/Errorpages/ErrorPage");
}

但是效果不太好……


奇怪的事情

如果我通过localhost:8080/error 调用动作,它肯定会调用动作并返回预期的错误页面。好吧,重定向和通过在 url 或 ajax 上键入内容的典型请求之间应该存在一些差异。


我想知道的

  1. 如何使&lt;error-page&gt; 配置工作?
  2. Spring 的自动重定向与Spring MVC 中用户操作的典型请求有什么区别?

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    错误页面大小必须 > 1024 字节。
    添加一些无用的东西到你的错误页面进行测试。
    添加此以确保错误页面具有此属性:[isErrorPage="true"]

    【讨论】:

      【解决方案2】:
      <error-page>
      <exception-type>java.lang.Exception</exception-type>
      <location>/error.jsp</location>
      </error-page>
      

      错误页面可能没有放入web-inf,移出多试

      【讨论】:

      • 移出错误页面不是答案,对不起。并且异常类型与我的问题无关..
      【解决方案3】:
       <error-page>
          <error-code>500</error-code>
          <location>/error.jsp</location>
      </error-page>
      <error-page>
          <error-code>400</error-code>
          <location>/index.jsp</location>
      </error-page>
      <error-page>
          <error-code>403</error-code>
          <location>/403.jsp</location>
      </error-page>
      <error-page>
          <error-code>404</error-code>
          <location>/404.jsp</location>
      </error-page
      

      您必须将您的 errorpages.jsp 放在 WEB_INF 文件夹之外r,然后将以下代码放入您的 web.xml 中。

      【讨论】:

      • 我也试过这个,但效果不佳......不幸的是:[
      【解决方案4】:

      你能检查你的 servlet-context.xml 配置吗?对我来说,它使用以下配置。

      <servlet-mapping>
          <servlet-name>appServlet</servlet-name>
          <url-pattern>/</url-pattern>
          <url-pattern>*.*</url-pattern>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <error-page>
          <error-code>404</error-code>
          <location>/error404</location>
      </error-page>
      

      和spring控制器方法

      @RequestMapping(value = "/error404", method = RequestMethod.GET)
      public String error() {
          logger.info("in error method");
          return "/errorPages/error404";
      }
      

      如果你想在 web.xml 中指定 jsp 的位置,你可以在那里指定,否则你可以添加 url。如果您添加 url DispatcherServlet 将负责响应浏览器,如果您指定 jsp 它将像正常的 servlet 响应。

      在 servlet-context.xml 中验证这一点

      <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
      <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <beans:property name="prefix" value="/WEB-INF/views/" />
          <beans:property name="suffix" value=".jsp" />
      </beans:bean>
      

      【讨论】:

        猜你喜欢
        • 2017-08-20
        • 2016-06-20
        • 1970-01-01
        • 2011-09-04
        • 2012-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多