目的:
后端通过Model传值到前端
页面通过Model取值显示
js通过Model取值作为变量使用
1.后台Controller
@GetMapping("/message")
public String getMessage(Model model){
model.addAttribute("message","This is your message");
return "index";
}
注:向model中添加属性message
2.页面通过Model取值
default message
3.js通过model取值
var message = [[${message}]];
console.log(message);
注:script标签中 th:inline 一定不能少,通常在取值的前后会加上不同的注释
4.如果前端需要接受的是一个JSON格式的对象,一定不能直接传JSON字符串
可以使用Fastjson将其转换为JSON对象package springboot.echarts.controller;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springfr