1.简介
本节主要知识点是spingboo响应页面的一些处理,上一节中类的注解用的是@RestController,这个注解主要是用于响应内容,一般用于前后端api比较多。
对于页面响应spring支持很多模板引擎。例如:
a,FreeMarker
b,Groovy
c,Thymeleaf (Spring 官网使用这个)
d,Velocity
e,JSP (貌似Spring Boot官方不推荐,STS创建的项目会在src/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如 SpringMVC 中的webapp目录)
下面我们以jsp页面为例来展示springBoot的页面响应
2.创建Java类
package com.example.demo;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ViewController {
//获取属性配置
@Value("${application.hello:hello world}")
private String hello = "hello world";
//直接用字符串返回页面
@RequestMapping(value = {
"/.","/index"})