java - DispatcherServlet 没有启动
问题描述
当我使用 Tomcat 启动我的应用程序时,只能使用 index.jsp,但是当我尝试从我的控制器(http://localhost:8080/home)获取请求时,它会响应 404 错误,例如 DispatcherServlet 没有启动 web.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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/dictionary</url-pattern>
</servlet-mapping>
</web-app>
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:annotation-config/>
<context:component-scan base-package = "com.example.dictionary" />
</beans>
控制器 - App.java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class App {
@GetMapping("/home")
public String getHome() {
return "home";
}
}
构建.gradle
plugins {
id 'java'
}
group 'com.temp'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.springframework:spring-core:5.1.9.RELEASE'
compile 'org.springframework:spring-context:5.1.9.RELEASE'
compile 'org.springframework:spring-beans:5.1.9.RELEASE'
compile 'javax.servlet:javax.servlet-api:4.0.1'
compile 'org.springframework:spring-webmvc:5.1.9.RELEASE'
}
解决方案
推荐阅读
- node.js - 回调在我创建索引时执行到早期
- karate - 当 karate-config.js 失败时,它会为每个功能打印错误
- c++ - 有没有办法将枚举类隐式转换为 std::byte?
- reactjs - 什么是标头以及如何从 REST API 获取数据?
- android - 如何不使用 Long TextView 将 ImageView 推离屏幕?
- rest - Sprint Data Rest 是否支持开箱即用的列查询?
- java - 在 Linux(i.MX 处理器)上运行 .jar 文件时 JMenuBar 损坏
- python - 将字典保存到 csv python
- ionic-framework - 如何在本地存储中设置和获取此表单的值
- java - 如何在java中获取windows进程的工作目录?