【问题标题】:How do I create a generic page for all my Spring controller exceptions?如何为我的所有 Spring 控制器异常创建一个通用页面?
【发布时间】:2014-12-31 19:57:59
【问题描述】:

我正在使用 Spring 3.2.11.RELEASE。我正在尝试将 JSP 设置为来自我的控制器的任何异常的包罗万象的页面。但是,该机制没有启动。我已将其添加到我的 web.xml 文件中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    metadata-complete="true">

    <display-name>subco Application</display-name>
    <session-config>
        <cookie-config>
            <path>/myproject</path>
        </cookie-config>
    </session-config>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/META-INF/spring/applicationContext-myproject.xml,
            classpath:/META-INF/spring/infrastructure.xml
        </param-value>
    </context-param>

    <context-param>
        <param-name>Owasp.CsrfGuard.Config</param-name>
        <param-value>csrfguard.properties</param-value>
    </context-param>

    <context-param>
        <param-name>Owasp.CsrfGuard.Config.Print</param-name>
        <param-value>true</param-value>
    </context-param>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

    <filter>
        <filter-name>CSRFGuard</filter-name>
        <filter-class>org.owasp.csrfguard.CsrfGuardFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CSRFGuard</filter-name> 
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.owasp.csrfguard.CsrfGuardServletContextListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.owasp.csrfguard.CsrfGuardHttpSessionListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.mainco.subco.myproject.mvc.listener.SbSessionAttributeListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>JavaScriptServlet</servlet-name>
        <servlet-class>org.owasp.csrfguard.servlet.JavaScriptServlet</servlet-class>
        <init-param>
            <param-name>source-file</param-name>
            <param-value>WEB-INF/csrfguard.js</param-value>
        </init-param>
        <init-param>
            <param-name>inject-into-forms</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>inject-into-attributes</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>domain-strict</param-name>
            <param-value>false</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>JavaScriptServlet</servlet-name>
        <url-pattern>/csrfjs</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>MyprojectDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/jboss-as-spring-mvc-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyprojectDispatcher</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

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

    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/error.jsp</location>
    </error-page>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

并在我的 WEB-INF 目录中创建了 error.jsp。但是,当从我的控制器中生成异常时,例如NullPointerExceptions,我没有看到 error.jsp 页面,而是一个通用的 spring 消息,上面写着“{“status”:“failure”“exception”:“NullPointerException”}。我还需要做哪些其他配置才能使用我的通用错误页面?如果这很重要,我正在使用 JBoss 7.1.3.Final。

编辑:

这是应用程序上下文文件,在上面的 web.xml 文件中引用:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="org.mainco.subco.assessment.mvc"/>
    <context:component-scan base-package="org.mainco.subco.myproject.mvc"/>
    <context:component-scan base-package="org.mainco.subco.myproject.validator"/>
    <context:component-scan base-package="org.mainco.subco.standards.mvc"/>
    <context:component-scan base-package="org.mainco.subco.resource.mvc"/>
    <context:component-scan base-package="org.mainco.subco.registration"/>

    <context:component-scan base-package="org.mainco.subco.section.mvc" />
    <context:component-scan base-package="org.mainco.subco.user.mvc" />
    <context:component-scan base-package="org.mainco.subco.util.mvc" />

    <context:component-scan base-package="org.mainco.subco.security" />
    <context:component-scan base-package="org.springframework.security.saml"/>
    <context:component-scan base-package="org.mainco.subco.myproject.lti" /> 

    <mvc:annotation-driven />

    <context:property-placeholder location="classpath:core.properties,classpath:application.properties"/>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:MyprojectUserMessages"/>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"/>
        <property name="maxInMemorySize" value="10000000" />
    </bean>

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:order="3" p:defaultErrorView="error" />

</beans>

【问题讨论】:

    标签: spring exception-handling custom-error-pages


    【解决方案1】:

    尝试像这样在您的 web.xml 中使用:

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

    你在使用 Spring MVC 吗?如果是这样,请尝试在您的 mvc 应用程序上下文中配置异常解析器,如下所示:

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:order="3" p:defaultErrorView="error" />
    

    您的 error.jsp 必须位于您的 viewResolver 的路径中(例如 "WEB-INF/pages""WEB-INF/views" 等)。

    PS.:要使用 p:*,您必须在 bean 中添加 xmlns:p="http://www.springframework.org/schema/p",如下所示:

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
        <!-- your configs go here -->
    
    </beans>
    

    您还可以将 @ExceptionHandler 和/或 @ControllerAdvice 与 Spring MVC 一起使用。

    【讨论】:

    • 我将 web.xml 指令更改为“/WEB-INF/error.jsp”并添加了“" 到我的应用程序上下文文件,但仍然没有骰子。
    • @DaveA 你能分享你的应用吗?
    • 完成!我将 web.xml 文件和关联的上下文文件添加到我的问题中。
    • @DaveA 看到这个链接:goo.gl/cK1yu9。如您所见,我做了两种方法:使用控制器建议和 SimpleMappingExceptionResolver。您可以评论一个或另一个并进行测试。
    • 您好,我尝试了您的代码,但仍然无法调用我的错误页面。我注意到您的“ControllerErrorHandle”类没有方法,不确定这是否重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多