springboot整合视图
时间: 2023-10-22 10:10:46 浏览: 57
在Spring Boot中,我们可以使用Thymeleaf、FreeMarker、JSP等视图模板引擎来渲染页面。
这里以Thymeleaf为例,首先需要在pom.xml中添加thymeleaf依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
然后在application.properties中配置视图前缀和后缀:
```
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
```
这里的配置表示Thymeleaf会在classpath:/templates/目录下查找.html后缀的模板文件。
最后,在Controller中设置返回的视图名称即可:
```
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "C知道");
return "hello";
}
}
```
这里的返回值"hello"表示使用名为"hello.html"的模板文件来渲染页面。模板文件中可以使用Thymeleaf提供的语法来动态展示数据。