Spring Boot集成Spring MVC:快速搭建Web应用

在Spring Boot项目中集成Spring MVC是一个非常常见的需求。Spring Boot已经内置了对Spring MVC的支持,因此集成过程相对简单。

1. 创建Spring Boot项目

你可以通过以下方式创建一个Spring Boot项目:

  • 使用Spring Initializr(start.spring.io)在线生成项目。

  • 使用IDE(如IntelliJ IDEA或Eclipse)的Spring Boot插件创建项目。

在创建项目时,确保添加了以下依赖:

  • Spring Web:这是集成Spring MVC的核心依赖。

如果使用Maven,pom.xml文件中会包含类似以下内容:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

如果使用Gradle,build.gradle文件中会包含类似以下内容:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

2. 创建Spring Boot启动类

Spring Boot的启动类是项目的核心入口,通常使用@SpringBootApplication注解。例如:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@SpringBootApplication是一个组合注解,它包括:

  • @SpringBootConfiguration:标记当前类为Spring Boot的配置类。

  • @EnableAutoConfiguration:启用Spring Boot的自动配置机制。

  • @ComponentScan:扫描当前包及其子中的包Spring注解(如@Controller@Service等)。

3. 创建Controller

Spring MVC的核心是Controller,用于处理HTTP请求。创建一个Controller类,并使用@Controller@RestController注解标记。例如:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Spring Boot with Spring MVC!";
    }
}
  • @RestController:表示这是一个Controller,且返回的内容直接作为响应体(无需视图解析)。

  • @GetMapping("/hello"):定义了一个GET请求的映射路径/hello

如果你需要返回HTML页面,可以使用@Controller注解,并结合视图解析器。例如:

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String home() {
        return "index"; // 返回视图名称,对应`src/main/resources/templates/index.html`
    }
}

4. 配置静态资源

Spring Boot默认会从src/main/resources/static目录加载静态资源(如HTML、CSS、JavaScript等)。你可以将静态文件放在该目录下,例如:

src/main/resources/static
│── index.html
│── css
│   ── style.css
│── js
│   └── script.js

访问静态资源时,可以直接通过URL访问,例如:http://localhost:8080/index.html

5. 配置视图解析器(可选)

如果你使用@Controller注解并需要返回HTML页面,需要配置视图解析器。Spring Boot默认使用Thymeleaf作为模板引擎,模板文件放在src/main/resources/templates目录下。例如:

<!-- src/main/resources/templates/index.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
    <h1 th:text="'Hello, ' + ${name} + '!'"></h1>
</body>
</html>

如果需要使用其他模板引擎(如Freemarker、Velocity等),可以通过添加相应的依赖并配置来实现。

6. 测试项目

启动Spring Boot应用后,访问http://localhost:8080/hello,应该会看到返回的字符串Hello, Spring Boot with Spring MVC!

如果使用了@Controller和视图解析器,访问http://localhost:8080/时,应该会看到对应的HTML页面。

7. 自定义配置(可选)

Spring Boot提供了许多自动配置,但你也可以根据需要进行自定义配置。例如:

  • 修改服务器端口:

    server:
      port: 8081
    
  • 自定义Spring MVC配置:

    package com.example.demo.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        // 在这里添加自定义配置,例如拦截器、视图解析器等
    }
    
posted @   软件职业规划  阅读(43)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示