我在 Eclipse 中创建了一个 Spring 项目。问题是,该 URL 没有以我认为应该的方式得到解决。
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/files/*</url-pattern>
<url-pattern>/filestest/*</url-pattern>
</servlet-mapping>
这是我的 mvc-dispatcher-servlet.xml
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.github.myproject.controllers" />
</beans>
这是我的控制器文件:
@Controller
@RequestMapping("/filestest")
public class TestController {
@RequestMapping(value="", method=RequestMethod.GET)
public @ResponseBody String getBase() {
System.out.println("Base");
return "Base";
}
@RequestMapping(value="/", method=RequestMethod.GET)
public @ResponseBody String getRoot() {
System.out.println("Root");
return "Root";
}
@RequestMapping(value="abc", method=RequestMethod.GET)
public @ResponseBody String getABC() {
System.out.println("ABC");
return "ABC";
}
@RequestMapping(value = "/abc", method=RequestMethod.GET)
public @ResponseBody String getBaseAbc() {
System.out.println("Base ABC");
return "Base ABC";
}
@RequestMapping(value="/{pathVar}", method=RequestMethod.GET)
public @ResponseBody String getPathVar(@PathVariable(value="pathVar") String pathVar) {
System.out.println(pathVar);
return pathVar;
}
}
这是我得到的输出
http://mylocalhost.com/filestest/abc - 404
http://mylocalhost.com/filestest/ - 404
http://mylocalhost.com/filestest - 有效输出 - “基础”
根据我对Spring 文档的理解,web.xml 应该将所有 /filestest 请求路由到 DispatcherServlet -> 将请求路由到我的 Controller -> 然后应该匹配正确的方法。
有人可以帮我弄清楚为什么我在尝试部署和测试我的应用程序时收到 404 File not found 错误的 URL - http://mylocalhost.com/filestest/abc和http://mylocalhost.com/filestest/ ?