首页 > 解决方案 > Springboot:控制器类没有被触发

问题描述

出于学习目的,我从 Eclipse 中的一个简单的 Spring Boot 应用程序开始。我能够在 http://localhost:8080 成功运行它。接下来我添加了一个控制器。但我看到控制器永远不会被触发。任何帮助将不胜感激。我用来访问我的控制器的 URL 是:http://localhost:8080/conference/greeting

我的项目结构:

项目结构

pom.xml 依赖项:

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>   
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

源代码:

    @SpringBootApplication
    public class ConferenceApplication extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(ConferenceApplication.class, args);
        }
    
    }
    @Controller
    public class GreetingController {
    
        @GetMapping("greeting")
        public String greeting (Map<String, Object> model) {
            model.put("message", "Hello Lok");
            return "greeting";
        }
    }

应用程序属性

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp

问候.jsp:

    <!DOCTYPE html>
    <html lang="en">
       <head>
           <meta charset="UTF-8">
           <title>Greeting</title>
       </head>
       <body>
           <h1>${message}</h1>
       </body>
    </html>

标签: spring-boot

解决方案


在 application.properties 中设置server.servlet.context-path=/conference或放控制器类注解@RequestMapping("conference")


推荐阅读