描述你的问题
使用了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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> fly contextConfigLocation classpath*:/applicationContext.xml encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter /* org.springframework.web.context.ContextLoaderListener springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath*:/springmvc-servlet.xml 1 true springmvc / 404 /static/errors/404.html 500 /static/errors/50x.html
pom.xml
1.7.5 1.0.13 4.2.2.RELEASE 1.2.2 2.2.2 org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 true true org.eclipse.jetty jetty-maven-plugin 9.2.10.v20150310 1 8888 1 manual org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 UTF-8
贴上报错信息
启动
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'
贴上相关截图
使用jetty运行
使用tomcat运行,这是指定的404页面
已经尝试过哪些方法仍然没解决(附上相关链接)
除了({"/"})路径外,其他页面访问都是正常的。
此问题Google无果,不知道是不是Spring MVC 4.2.2.RELEASE 的问题
参考官方的实例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>
也能访问
查看tomcat的server.xml中该项目的Context path,比如<Context path="/myapp" .../> 那么url里头就是localhost:8080/myapp
把servlet中name为springmvc的url-pattern的/改成/*