我是一名正在研究 Spring MVC 框架的学生。如果这是一个不好的问题,请提前道歉
我使用maven部署到tomcat7 我了解了 springMVC 的工作流程,我将尝试解释它。请纠正我的任何误解
客户端请求将首先访问 web.xml
在 web.xml 中,URL 模式绑定(bind)到 servletName(或 DispatcherServlet),并将其映射到(默认情况下)另一个名为“[servletName]-servlet.xml”的 xml(我也可以实例化)如果我想要它有另一个名字的话)
在[servletName]-servlet.xml中,它会将URL的前缀和后缀连接起来,使其成为一个文件路径(例如/WEB-INF/jsp/+ hello + .jsp)来定位正确的 jspFile 在客户端浏览器中呈现
Controller 文件(声明为 @Controller)充当后端并使用 @RequestMapping(URL) 进行调用
基于此流程,我输入“localhost:8080/hello.jsp”(还尝试了下面提到的其他 URL),但它仍然返回 HTTP404
tomcat7运行正常,就是找不到jsp页面
我尝试将 web.xml 中的“url-patterns”编辑为“/”或“.jsp”或“hello.jsp”或“hello”,但无济于事
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>to do list</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package = "com.tutorialspoint" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Hello Spring MVC</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
HelloController.java
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
这是我的文件层次结构
这就是错误
最佳答案
尝试将此代码配置用于 servlet。您是否尝试访问 http://localhost:8080/hello
?
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
关于java - Spring MVC - 找不到 URL 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57188017/