spring-mvc - Error404:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示
问题描述
我是springboot的新手,正在尝试使用tomcat 9.0.53将控制器连接到网页。尽管与服务器的连接成功,但是我无法在服务器上显示视图,这给了我这个错误。错误 404:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。
这是我的控制器类:
package com.emusicstore.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class homeController {
@RequestMapping("/home")
public String home()
{
return "home";
}
}
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Dispatcher_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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.emusicstore"/>
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
请帮我找出根本问题。
解决方案
确保你检查你的tomcat日志它通常给出了关于问题的想法,现在看起来将名称从' Dispatcher_Servlet.xml '更改为' dispatcher-servlet.xml '还检查dispatcher-servlet.xml是否放置在WEB中-INF 文件夹 之后
如果您使用 Maven,那么:
1)Maven清洁
2)Maven安装
3)再次运行项目
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name> //make sure the servlet name is correct i.e Case sensitive
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
推荐阅读
- download - 量角器和硒服务器 - 如何在 chrome 中下载文件并检查它是否已下载
- logging - 在 Logback 中覆盖从 root 继承的 Logger 的 appender
- apache-flink - IOException:状态的大小大于最大允许的内存支持状态
- html - 具有动态值 angular5 的 HTML5 圆形标记属性
- html - 如何使用 ASP.NET Core 2.1 作为普通 HTML/JavaScript 网页的后端?
- javascript - 使用 Angular CLI 和 Angular 5 在运行时动态加载新模块
- c# - 在 OnCreate 之外设置 EditText.Text
- angularjs - Jasmine 测试的延迟加载模块。Angularjs
- html - 让元素在不同的时间闪耀
- android - 回收器视图适配器不工作