当前位置: 首页 > 知识库问答 >
问题:

当服务器在Spring mvc中抛出404错误时返回空白页

彭展
2023-03-14

我正在尝试为spring中的404错误配置一个自定义页面。我的应用程序还配置了Spring Security性。

这是web.xml配置

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

浏览器通过正确的url访问404.jsp。但是当出现404错误时抛出404。jsp不会显示。而是显示一个空白页。

有关于如何解决此问题的建议吗?提前谢谢。

编辑

这就是我web.xml的样子。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/application-context.xml,
            /WEB-INF/applicationContext-security.xml
            /WEB-INF/webflow-context.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>
    <filter>
        <description>sitemesh filter</description>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    <!-- DO NOT define any filter mappings above to the spring security filter-->
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <context-param>
        <param-name>languages</param-name>
        <param-value>en,sv</param-value>
    </context-param>
    <filter>
        <filter-name>i18nFilter</filter-name>
        <filter-class>com.example.filter.InternationalizationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>i18nFilter</filter-name>
        <url-pattern>*.html</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>ajax</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>3</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ajax</servlet-name>
        <url-pattern>/ajax/*</url-pattern>
    </servlet-mapping>

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

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

更新了spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        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.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.xsd">

    <context:component-scan base-package="com.example.controller" />

    <mvc:annotation-driven />     

    <mvc:view-controller path="/index.html" view-name="home"/>

    <bean id="viewResolver"
             class="org.springframework.web.servlet.view.InternalResourceViewResolver"
             p:prefix="/WEB-INF/jsp/"
             p:suffix=".jsp"/>     

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:ApplicationResources</value>
            </list>
        </property>
    </bean>
</beans>

共有1个答案

步骏
2023-03-14

尝试使用此配置:

改变

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

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

并将其添加到您的Spring配置中

<mvc:view-controller path="/404.html" view-name="404"/>
 类似资料:
  • 我试图在流量为空时返回404,类似于这里:WebFlux Functional:如何检测空流量并返回404? 我主要担心的是,当你检查通量是否有元素时,它会发出那个值,而你却松开了它。当我尝试使用switch时,如果服务器响应为空,它就永远不会被调用(我暗暗认为这是因为单声道不是空的,只有主体是空的)。 是否有一种方法可以恢复hasElements中发出的元素,或者让switchIfEmpty只检

  • 试图调试这个简单的NodeJS socketIO服务器。 我在启动时不断收到以下错误消息。我看不出代码有什么问题。 TypeError:侦听器必须是服务器上Namespace.EventEmitter.AddListener(Events.js:130:11)处的TypeError()处的函数。(匿名函数)[如上](/users/foo/bin/node_modules/socket.io/lib

  • JSoup-1.8.1 尝试{ Document Document=Jsoup.connect(url.get(); 返回Document.getElementsByTag(“title”).text(); }catch(异常e){ System.out.println(e); 返回null; } org.jsoup.HttpStatusExc0019: HTTP错误获取URL。状态=404, U

  • 我正在使用java.net.HttpUrlConnection向我的服务器发出Http请求。我意识到如果服务器返回错误状态(例如 400)。HttpUrlConnection 将抛出与错误状态对应的 IOException。 我的问题是:如果服务器返回错误状态(4xx,5xx),HttpUrlConnection是否总是抛出IOException? 我看了一下HttpUrlConnection A

  • 在我的Spring Boot 2.2. x Web应用程序中,我需要拦截404错误,以便在呈现错误页面或返回带有错误信息的json之前执行一些自定义逻辑。 实现这一点的正确方法是什么? 目前,我正在尝试使用类,但我不知道使用(如果抛出了任何异常)处理哪个异常。。。 注意:我已经尝试拦截,但这似乎不起作用...(未触发处理程序)。 我还在<code>应用程序中启用了此功能。属性: 还有其他建议吗?

  • 我在Localhost中正确上传我的文件,但是当我在服务器上上传我的应用程序时,他们给我错误:上传文件后服务器错误。 这是我的控制器: 在我的本地主机中,文件保存在公用文件夹中。我想用public\u html或public\u html/image将我的文件保存在我的服务中。