运行在Tomcat中访问 Spring MVC 映射的 [/] 路径出现404-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

运行在Tomcat中访问 Spring MVC 映射的 [/] 路径出现404

使用了Spring MVC 框架
使用jetty-maven-plugin插件运行可以运行正常访问({"/"})路径(就是http://localhost:8080/),然而maven打包放到tomcat的ROOT目录访问({"/"})出现404,启动日志有显示已经映射({"/"})路径
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>

报错信息
启动
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'

展开
收起
蛮大人123 2016-03-10 18:05:49 3838 0
1 条回答
写回答
取消 提交回答
  • 蛮大人123
    我说我不帅他们就打我,还说我虚伪

    参考官方的实例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>

    也能访问

    2019-07-17 18:58:06
    赞同 展开评论 打赏
问答排行榜
最热
最新
相关电子书
更多
电商网站需求分析和架构设计Spring Boot2.6入门
立即下载
云上Docker的Spring Cloud微服务应用实践分享
立即下载
Spring Boot 2.6.0电商网站开发实战
立即下载