1、因为springboot不赞成使用jsp页面,因此考虑spring boot推荐的Thymeleaf模版
添加模版
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
application.properties添加如下配置:(这个不配置也可以的,如果不配置,会到resources/templates下去找同名的模板或者页面)
# 定位模板的目录
spring.mvc.view.prefix=classpath:/templates/
# 给返回的页面添加后缀名
spring.mvc.view.suffix=.html
2、设计一个controller代码
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Auther: Matthew
* @Date: 2018/12/4 09:12
* @Description:
*/
@Controller
@RequestMapping(value = "/oa")
public class Handle {
/**
* 存入数据模型,并返回跳转的页面
* @param model
* @return String
*/
@RequestMapping(value = "/login")
public String index(Model model) {
System.out.print("进来了");
model.addAttribute("title", "the first spring boot");
model.addAttribute("name", "楚苓大妈");
return "index";
}
}
模板使用demo
<h1 id="title">${title}</h1>
模板中的各类语法自己搜索,这里不做介绍
注意,spring boot默认开启了静态文件的配置,任何放在static文件夹下的资源都是静态文件。引用静态文件时以/或者前缀不加任何定位符,都会去static文件夹下查找。
2,实际项目中,现在没有使用模板了。都是使用的静态界面html
和模板差不多
# 定位页面的目录到static/下
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
controller当然也是需要的,和之前一样:
@Controller//这里只能用这个
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.html页面的,则其显示success.
2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
3)如果需要返回json或者xml或者自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解