
为什么404 路径这里多了一个点 tomcat配置就是/springmvc
阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程
你遇到的问题是,Spring MVC 中的请求映射(Request Mapping)写了一个 /,导致路径多了一个点,导致404错误。
这是因为 Spring MVC 的请求映射是相对于应用程序的根路径的。如果你在 Tomcat 中配置了 Spring MVC 的映射为 /springmvc,那么 Spring MVC 就会将所有请求映射到 /springmvc 下。
例如,如果你在 Request Mapping 中写了 /hello,那么 Spring MVC 会将请求映射到 /springmvc/hello。如果你想要映射到 /hello,而不是 /springmvc/hello,你需要在 Request Mapping 中写 /,这样 Spring MVC 就会将请求映射到应用程序的根路径下。
因此,解决这个问题的方法是,在 Request Mapping 中写 /,例如:
@RequestMapping("/")
public String hello() {
return "hello";
}
这样,Spring MVC 就会将请求映射到应用程序的根路径下,避免了多个点的问题。