Springboot如何和springmvc一起使用
时间: 2023-05-30 19:07:31 浏览: 162
Spring Boot 可以和 Spring MVC 一起使用,通过添加依赖和配置,实现快速搭建 Web 应用。
首先,在 Maven 或 Gradle 的项目中添加 Spring MVC 和 Spring Boot 的依赖:
```xml
<!--Spring MVC-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
<!--Spring Boot-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
</dependencies>
```
接着,在 Spring Boot 的配置文件中,可以配置 Spring MVC 的相关参数,例如视图解析器、静态资源映射等。
```yaml
spring:
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
resources:
static-locations: classpath:/static/
```
最后,在 Spring Boot 的启动类中,添加 `@EnableWebMvc` 注解来启用 Spring MVC。
```java
@SpringBootApplication
@EnableWebMvc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
这样,就可以在 Spring Boot 中使用 Spring MVC 了。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)