2015-03-25 19 views
7

问题<error-page>设置没有在Spring MVC

我想向用户显示自定义错误页的工作。 Simeply在web.xml上放置<error-page>似乎不起作用。我可以猜到问题是什么,但我需要了解为什么它不起作用。


我的设置

我在春天MVC应用程序的web.xml文件中建立<error-page>。以下是设置。

<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 --> 

第二次尝试

我看着服务器日志,发现春天试图通过redirecion查找的资源,所以我改变了我的计划,并试图通过在典型视图返回达致这目标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 

正如你所看到的,我产生404错误由NOTFOUND上键入URL ,正如我在web.xml上<location>指定重定向到/错误。但它不返回视图。

要确保动作返回一个观点,我试图改变动作的类型像...

@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. 我怎样才能让<error-page>配置工作?
  2. Spring的自动重定向和Spring MVC中用户操作的典型请求之间的区别是什么?

回答

0
<error-page> 
<exception-type>java.lang.Exception</exception-type> 
<location>/error.jsp</location> 
</error-page> 

错误页面可能没有放在WEB-INF,迁出,并尝试了很多

+0

搬出错误页面不是答案,我很抱歉。而异常类型与我的问题无关。 – hina10531 2015-03-25 08:28:51

0
<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外folde r和刚把下面的代码放到你的web.xml中。

+0

我也试过这个,但那并没有工作......不幸的是:[ – hina10531 2015-03-25 08:27:13

0

你可以检查你的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> 

和弹簧控制方法

@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> 
1

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