springmvc启动报错, No mapping found for HTTP request with URI

发布于 2022-09-05 21:40:49 字数 5713 浏览 15 评论 0

我搭建springmvc工程,我想拦截所有的请求,在web.xml中这样配置:

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

  <display-name>spring-mvc-demo</display-name>

  <!-- 加载springMVC配置 -->
  <servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

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

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

</web-app>

spring-config.xml配置

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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"
    default-autowire="byName">

    <!-- 加载属性文件 -->
    <bean class="com.jd.springmvc.demo.common.spring.CustomizedPropertyConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath*:${global.config.important}</value>
                <value>classpath*:${global.config.diffInEnvs}</value>
                <!--<value>classpath:auth.properties</value>-->
                <value>classpath:common.properties</value>
            </list>
        </property>
    </bean>
    <!-- 自动注入ApplicationContext -->
    <bean class="com.jd.springmvc.demo.common.spring.ApplicationContextUtil"/>
    
    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.jd.springmvc.demo.web" />

    <!-- 导入分类配置 -->
    <import resource="classpath*:spring/spring-config-mvc.xml" />
    <import resource="classpath*:spring/spring-config-service.xml" />
    <import resource="classpath*:spring/spring-config-dao.xml" />
    <!--<import resource="classpath*:spring/spring-config-rpc.xml" />-->
    <!--<import resource="classpath*:spring/spring-config-jsf.xml" />-->
    <!--<import resource="classpath*:spring/spring-config-signin.xml"/>-->
    <!--<import resource="classpath*:spring/spring-config-jmq.xml"/>-->
    <!--<import resource="classpath*:spring/spring-config-redis.xml"/>-->
</beans>

spring-config-mvc.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    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-3.2.xsd"
    default-autowire="byName">
    
    <!-- 添加MVC注解模式 -->
    <mvc:annotation-driven/>
    <!-- 映射静态资源 -->
    <mvc:resources location="/WEB-INF/statics/" mapping="/resource/**"/>
    <!--<mvc:resources location="/WEB-INF/publish/" mapping="/publish/**"/>-->
    <!--<mvc:default-servlet-handler/>-->


    <!-- 视图解析器配置 -->
    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/vm/" />
        <property name="configLocation" value="classpath:velocity.properties" />
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm" />
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>
    <!--&lt;!&ndash; 文件上传 &ndash;&gt;-->
    <!--<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
        <!--<property name="defaultEncoding" value="UTF-8"/>-->
    <!--</bean>-->
</beans>

启动的时候,报错:

No mapping found for HTTP request with URI [/resourceNotFound] in DispatcherServlet with name 'springServlet'
请问是什么原因?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2022-09-12 21:40:50
<error-page>
    <error-code>404</error-code>
    <location>/resourceNotFound</location>
  </error-page>

你给配的404页面不存在吧,指向一个已存在的静态页面,或controller中配上/resourceNotFound试试

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文