spring-mvc - Spring MVC - HTTP 状态 404 - 源服务器没有找到目标资源的当前表示
问题描述
试图创建一个 Spring MVC hello world web 应用程序。它当前返回 HTTP 状态 404。404 表示它找不到文件,但我不知道为什么。我认为我的 servlet.xml 指向适当的文件夹(组件扫描)。我认为所有文件的名称都正确(没有拼写错误)。请参阅图片以了解错误。
servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="helloworld" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldController.java
package helloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorldController {
@RequestMapping("/")
public String showPage() {
return "hello-world";
}
}
你好世界.jsp
<!DOCTYPE html>
<html><body>Hello World!</body></html>
解决方案
RequestMapping 指向控制器中的“/”。尝试拨打http://localhost:8080/
推荐阅读
- python - Django:在 def post() 中返回与 PostListView 相同的数据
- java - 获取内存中的日志对象 - Logback
- arrays - 无法将值存储在数组中
- php - 对 url $_GET 提交中的 xss 攻击的担忧
- vim - 如何在 Vim 中正确缩进 HTML
- python - 使用 csv 文件时不进入 for 循环
- java - 响应中的 Set-Cookie 标头被浏览器(Cookie)丢弃
- javascript - 如何在不同的文件夹和子文件夹中使用 Grunt 动态重命名文件并使用版本号?
- javascript - OffScreenCanvas 和触摸事件
- python - 从 API 获取响应时出现 JsonDecodeError