追加 觉得有意义在最前面显示:
1.在使用thymeleaf的时候重复修改页面会有缓存,在application.properties中添加
spring.thymeleaf.cache=false
2.设置主页,根目录下的显示的页面。
src/main/resources/static/index.html
index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greet">here</a></p>
</body>
</html>
设置完之后根目录会自动映射到index.html
返回页面(thymeleaf)
pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
返回视图使用的注解不是@RestController而是@Controller
src/main/resources下建立static和templates两个文件夹
static默认存放js、css等静态资源
templates存放的是页面
都是默认的路径,其中返回的页面类型缺省都是.html后缀
所以此处写页面不需要在application.properties中进行配置或者覆盖设置。
Controller代码:
package com.zhu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@RequestMapping("/greet")
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name,
Model model) {
model.addAttribute("name", name);
return "greets";
}
}
在templates中的greets.html
src/main/resources/templates/greets.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
启动项目
浏览器输入
返回页面:
返回页面
Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。
详细使用参见:
http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#integrating-thymeleaf-with-spring
返回JSON
Create a resource representation class
创建你的实体类
src/main/java/com/zhu/entity/Greeting.java
package com.zhu.entity;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
As you see in steps below, Spring uses the Jackson JSON library to automatically marshal instances of type Greeting into JSON.
SpringBoot自动的使用Jackson来转换JSON
通过Spring的方式建立了一个restful服务,通过@RestController注解默认以GET的请求方式进入方法体内。
GreetingController.java
package com.zhu.controller;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zhu.entity.Greeting;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
run application
输入浏览器网址
返回一个JSON:
{“id”:1,”content”:”Hello, World!”}
URL传入参数“name”
返回结果:
{“id”:2,”content”:”Hello, zhangsan!”}