运行在Tomcat中访问 Spring MVC 映射的 [/] 路径出现404,其他路径正常,Jetty全部可以访问

  1. 描述你的问题
    使用了Spring MVC 框架

使用jetty-maven-plugin插件运行可以运行正常访问({"/"})路径(就是http://localhost:8080/),然而maven打包放到tomcatROOT目录访问({"/"})出现404,启动日志有显示已经映射({"/"})路径

  1. 贴上相关代码

HomeController.java

@Controller
public class HomeController {

    @RequestMapping("/")
    public String index(){
        return "index";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="3.0">

    <display-name>fly</display-name>
    <!--  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/applicationContext.xml
        </param-value>
    </context-param>

    <!-- Character Encoding filter -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <!-- springmvc -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/static/errors/404.html</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/static/errors/50x.html</location>
    </error-page>

</web-app>

pom.xml

    <properties>
        <slf4j.version>1.7.5</slf4j.version>
        <logback.version>1.0.13</logback.version>
        <spring.version>4.2.2.RELEASE</spring.version>
        <shiro.version>1.2.2</shiro.version>
        <jackson.version>2.2.2</jackson.version>
    </properties>

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.10.v20150310</version>
                <configuration>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                    <stopPort>8888</stopPort>
                    <stopKey>1</stopKey>
                    <reload>manual</reload>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
  1. 贴上报错信息

启动

08-Feb-2016 18:01:04.732 INFO [localhost-startStop-1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.register Mapped "{[/]}" onto public java.lang.String me.zonghua.fly.HomeController.index()

访问

08-Feb-2016 17:45:14.136 WARNING [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'springmvc'
  1. 贴上相关截图
    使用jetty运行

clipboard.png

使用tomcat运行,这是指定的404页面
clipboard.png

  1. 已经尝试过哪些方法仍然没解决(附上相关链接)
    除了({"/"})路径外,其他页面访问都是正常的。

此问题Google无果,不知道是不是Spring MVC 4.2.2.RELEASE 的问题

阅读 10.4k
3 个回答

把servlet中name为springmvc的url-pattern的/改成/*

查看tomcat的server.xml中该项目的Context path,比如<Context path="/myapp" .../> 那么url里头就是localhost:8080/myapp

参考官方的实例web.xml要写成

<servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

不然不能匹配 [/] 路径

虽然另外一个工程中写成

<servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

也能访问

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
1 篇内容引用
推荐问题
宣传栏