以下是一个简单的Spring MVC示例,展示如何实现不使用JSP来渲染视图:

1. 添加依赖

springmvc不使用jsp实例,SpringMVC如何实现不使用JSP的实例教程  第1张

确保你的`pom.xml`文件中包含了Spring MVC的依赖。

```xml

org.springframework

spring-webmvc

5.3.10

org.springframework.boot

spring-boot-starter-thymeleaf

```

2. 配置Spring MVC

创建一个配置类,继承`WebMvcConfigurer`。

```java

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration

@EnableWebMvc

public class WebConfig implements WebMvcConfigurer {

@Override

public void configureViewResolvers(ViewResolverRegistry registry) {

registry.jsp();

}

}

```

3. 创建控制器

创建一个控制器类,其中包含一个处理请求的方法。

```java

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

@Controller

public class HelloController {

@GetMapping("