Thymeleaf前后端传值 页面取值与js取值
目的:
后端通过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取值
<p th:text="#{message}">default message</p>
3.js通过model取值
<script th:inline="javascript">
var message = [[${message}]];
console.log(message);
</script>
注: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.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import com.alibaba.fastjson.JSON;
import springboot.echarts.service.EchartService; @Slf4j
@Controller
public class EchartsController { @Autowired
private EchartService echartService; @GetMapping("/echart")
public String echart(Model model){
String optionStr = null;
// //禁用引用对象
optionStr = JSON.toJSONString(echartService.selectSelling(),
SerializerFeature.PrettyFormat,
SerializerFeature.DisableCircularReferenceDetect);
log.info(optionStr);
// modal.addObject("option",JSON.parseObject(optionStr));
//由于ECharts接收的option必须为JSON对象,optionStr为一个String对象,所以需要转为JSON对象
//数组对象
//model.addAttribute("option",JSON.parseArray(optionStr));
model.addAttribute("option",JSON.parseObject(optionStr));
return "echart";
} }

5.ajax调用请求时可以直接返回Json格式的字符串,但是在ajax中声明对象为JSON

//js调用java方法参考:https://www.cnblogs.com/shirandedan/p/7727326.html
var menuJson;
function getUserFunc(){
$.ajax({
type: 'GET',
url: "getUserAllFunction",
cache: false,
async : false,
// headers : {
// 'Content-Type' : 'application/json;charset=utf-8'
// },
// data: JSON.stringify(menuJson),
dataType: 'json',
success: function (result) {
console.log("获取用户所有功能成功");
console.log("result "+JSON.stringify(result));
menuJson = result;
},
error: function (result, XMLHttpRequest, textStatus, errorThrown) {
console.log("获取用户所有功能失败");
console.log("result "+JSON.stringify(result));
console.log("menuJson "+menuJson);
console.log("JSON.stringify(obj) "+JSON.stringify(menuJson));
// 状态码
console.log(XMLHttpRequest.status);
console.log(XMLHttpRequest.toLocaleString());
// 状态
console.log(XMLHttpRequest.readyState);
// 错误信息
console.log(textStatus);
}
});
return menuJson;
}
//Ajax全局变量赋值参考: https://blog.csdn.net/gzp444280620/article/details/70922224
menuJson = getUserFunc();

getUserAllFunction请求如下:
@GetMapping("/getUserAllFunction")
@ResponseBody
public String getUserAllFunction(HttpSession session){
List<UserFuncEntity> list = new ArrayList<>();
...//略
//若出现引用类型,需要强制去掉引用,js中不能识别引用
return JSON.toJSONString(menuList,SerializerFeature.DisableCircularReferenceDetect );
}
Thymeleaf前后端传值 页面取值与js取值的更多相关文章
- SpringMVC踩坑3——前后端传值问题
在前端页面点击修改,同时把需要修改的ID传到后端,后端根据ID去修改具体数据 这是前端代码 <a href="${pageContext.request.contextPath}/bo ...
- springmvc前后端传值
@pathvible 后端传值(rest风格) exp: @requestMapping("/view/{userId}") public String getiew(@Parth ...
- 前后端数据交互处理基于原生JS模板引擎开发
json数据错误处理,把json文件数据复制到----> https://www.bejson.com/ 在线解析json 这样能直观的了解到是否是json数据写错,在控制台打断点,那里错误打那 ...
- Thymeleaf前后端分页查询
分页查询是一个很常见的功能,对于分页也有很多封装好的轮子供我们使用. 比如使用mybatis做后端分页可以用Pagehelper这个插件,如果使用SpringDataJPA更方便,直接就内置的分页查询 ...
- springmvc前后端传值总结
1 前端向后端传参 1.1 普通方式传参 1.1.1 页面 参数需要解析成json对象:JSON.parse(JSON.stringify(query)) $.getJ ...
- struts2+ajax 前后端传值
摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...
- JQuery ajax 前后端传值介绍
https://jingyan.baidu.com/album/ca41422f0bf08e1eae99ed04.html?picindex=5 现在我们话不多说,开始仔细讲解一下我们ajax内部传递 ...
- json数组,前后端传值问题,与data时间转毫秒
从json数组到ArrayList Gson gson = new Gson(); Car cars = gson.fromJson(result,new TypeToken<ArrayList ...
- 前后端分离开发——模拟数据mock.js
mock.js 生成模拟数据,拦截ajax请求 <script type="text/javascript" src="http://libs.baidu.com/ ...
随机推荐
- JMM - Java内存模型
内存模型的作用是定义变量的访问规则.包含:实例字段.静态字段.构成数组对象的元素.不包括局部变量和方法参数等线程私有变量. JMM所有变量都在主存,每个线程都有自己的工作内存.线程的工作内存中保存了线 ...
- 远程桌面控制winsever,复制文件或者文件夹夹时出错提示“未指定的错误” 二(如何让远程电脑识别U盘)
一.背景: 要给远程服务器安装数据库,把安装复制到服务器,出现复制文件或者文件夹夹时出错提示“未指定的错误”:通过映射网络分享文件方法来解决,发现服务器访问网络出现错误,ping分享文件电脑的IP ...
- 【C/C++】C++11 Move, Forward
左值与右值 Lvalue:可以出现在 operator= 左边的 Rvalue:只能出现在operator= 右边的 ; int a = b; a = b; a = a + b; a + b = a; ...
- 网盘资源分享:你不知道的JavaScript(上)
链接:https://pan.baidu.com/s/1UEBetOr2Z94oEeu5VsQYXQ 提取码:etts 复制这段内容后打开百度网盘手机App,操作更方便哦
- javascript高级
数组及操作方法 数组就是一组数据的集合,javascript中,数组里面的数据可以是不同类型的. 定义数组的方法 //对象的实例创建 var aList = new Array(1,2,3); //直 ...
- strcpy_s和strcpy()
转自: https://www.cnblogs.com/hrhguanli/p/4570093.html strcpy_s和strcpy()函数功能几乎相同.strcpy函数.就象gets函数一样,它 ...
- Spring Boot(一)
(一)如何使用IDEA新建一个Spring Boot项目 https://www.cnblogs.com/wmyskxz/p/9010832.html
- python 面向对象 继承 派生 组合
具体参考博客:http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label12 一.面向对象 面向对象:对象与对象之间的相互交互,不可预 ...
- js操作对象
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化
Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化 2018-06-07 00:446470原创GoldenGate 作者: leo 本文链接:https://www.cndba ...