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/ ...
随机推荐
- go安装和开发工具安装
go适合做什么 l 服务端开发 l 分布式系统 l 网络编程 l 区块链开发 l 内存KV数据库,例如boltDB.levelDB l 云平台 一 go安装 官网:https://golang.goo ...
- 关于PHP的mkdir函数
问题:dedecms5.7 php5.6 我想项目根目录下的uploads文件夹下动态创建一个文件夹/uploads/imgs $path = '/uploads/imgs'; mkdir($path ...
- Linux中python3,django,redis以及mariab的安装
1. Linux中python3,django,redis以及mariab的安装 2. CentOS下编译安装python3 编译安装python3.6的步骤 1.下载python3源码包 wget ...
- C# linq操作是否延迟对照表
·Select - Select选择:延迟 ·Where - Where查询:延迟 ·OrderBy - 按指定表达式对集合正序排序:延迟 ·OrderByDescending - 按指定表达式对集合 ...
- ReactJS之遍历对象的方法
const obj = { channel: “wevmvmklskdosll12k;0”, index:0 }; Object.keys(obj).map(key => console.log ...
- CentOS 7系统上添加netcdf库
这里提供两种方法来安装 netcdf: 一是 yum 安装,yum 安装时采用的是系统自带的 gfortran 编译器,版本较旧: 二是采用源码来 Makefile 编译安装的方式,它可以下载最新版的 ...
- C# MethodInvoker委托的使用
一.MethodInvoker是什么? MethodInvoker 表示一个委托,该委托可以执行托管代码中声明为void且不接受任何参数的任何方法.在对控件的 invoke 方法进行调用时或需要一个简 ...
- YML文件中ipv6地址输入格式
关于YML文件格式可以百度,这里只说ipv6:yml文件是注重格式的不能用tab键代替空格 ipv4 :10.1.202.9 ipv6: 2001:202::6e4:f32b:c19c:4760 端口 ...
- rabbitmq的问题Failed to start bean 'listenerContainer'
第一次使用mq发送消息,启动报错了,网上看了好多也没找到几个相关论坛,好几个置顶的都是发帖后不说明解决方案的.因此在这里记录一下. 原因: 因为消息监听的地址在mq服务中没有对应的q, 解决: 需要手 ...
- apk文件二维码微信无法识别 APP在微信中二维码扫描无法下载的解决方案
现在微信分享的功能很多,从分享的链接下载apk安卓包是很正常的,但是微信不让下载apk包,只能通过浏览器来下载,但是这要给用户一个提示吧,不然用户不知道 下面我们来实现,引导用户通过浏览器来下载apk ...