首页 > 解决方案 > 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'
}

标签: javaspringspring-mvcservlets

解决方案


推荐阅读