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/ ...
随机推荐
- flutter安装教程(win7)
本文是在安装flutter的时候,所遇到的问题参考的各个文档整理集合.此次是在win7上安装的问题记录.因为当初安装的时候针对win7的文档比较少,而且各个文档的解释比较散,本人遇到问题也是找了很久才 ...
- 获得ztree的所有子节点id
var treeNodeIds="";//获得所有树节点的id function aa(){ var treeObj = $.fn.zTree.getZTreeObj(" ...
- python 调用js代码
Python2 安装pyv8 pip install-egit://github.com/brokenseal/PyV8-OS-X#egg=pyv8 from pyv8 import PyV8 c ...
- c++中各类型数据所占字节数(一)
转自: https://blog.csdn.net/hi_baymax/article/details/82415896 和机器字长及编译器有关系: 所以,int,long int,short int ...
- jieba库与词云的使用——以孙子兵法为例
1.打开cmd安装jieba库和 matplotlib. 2.打开python,输入代码.代码如下: from wordcloud import WordCloud import matplotlib ...
- 怎么让html中 还是显示<button> 而不进行编译
toTXT(str) { var RexStr = /\<|\>|\"|\'|\&| | /g str = str.replace(RexStr, ...
- tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决
现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...
- Lapack求解线性方程组
可参见这两个页面: 1. http://www.culatools.com/dense/lapack/ 2. http://www.netlib.org/lapack/lug/node1.html 根 ...
- css属性应用bug大杂烩(后续继续更新)
一.Flex布局使用时的坑: 1.常见的左右分布的flex布局中,左侧给定宽度,右侧占满剩余空间,但当右侧中文字内容很多时,会挤占左侧空间,时左侧不能按照定宽显示. <style> .fa ...
- socket架构
套接字基本概念 Socket是应用层与Tcp/ip协议族通信的中间软件抽象层,它是一组接口.在设计模式中,socket其实就是一个门面模式,它把复杂的tcp/ip协议族隐藏在socket接口后面,对用 ...