参考: Thymeleaf前后端传值 页面取值与js取值

    Thymeleaf 与 Javascript

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取值的更多相关文章

  1. SpringMVC踩坑3——前后端传值问题

    在前端页面点击修改,同时把需要修改的ID传到后端,后端根据ID去修改具体数据 这是前端代码 <a href="${pageContext.request.contextPath}/bo ...

  2. springmvc前后端传值

    @pathvible 后端传值(rest风格) exp: @requestMapping("/view/{userId}") public String getiew(@Parth ...

  3. 前后端数据交互处理基于原生JS模板引擎开发

    json数据错误处理,把json文件数据复制到----> https://www.bejson.com/ 在线解析json 这样能直观的了解到是否是json数据写错,在控制台打断点,那里错误打那 ...

  4. Thymeleaf前后端分页查询

    分页查询是一个很常见的功能,对于分页也有很多封装好的轮子供我们使用. 比如使用mybatis做后端分页可以用Pagehelper这个插件,如果使用SpringDataJPA更方便,直接就内置的分页查询 ...

  5. springmvc前后端传值总结

    1      前端向后端传参 1.1    普通方式传参 1.1.1         页面 参数需要解析成json对象:JSON.parse(JSON.stringify(query)) $.getJ ...

  6. struts2+ajax 前后端传值

    摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...

  7. JQuery ajax 前后端传值介绍

    https://jingyan.baidu.com/album/ca41422f0bf08e1eae99ed04.html?picindex=5 现在我们话不多说,开始仔细讲解一下我们ajax内部传递 ...

  8. json数组,前后端传值问题,与data时间转毫秒

    从json数组到ArrayList Gson gson = new Gson(); Car cars = gson.fromJson(result,new TypeToken<ArrayList ...

  9. 前后端分离开发——模拟数据mock.js

    mock.js 生成模拟数据,拦截ajax请求 <script type="text/javascript" src="http://libs.baidu.com/ ...

随机推荐

  1. 关于requestAnimationFrame与setInterval的一点差异

    requestAnimationFrame与setInterval都可以实现循环触发事件,但是setInterval是基于时间的,而requestAnimationFrame是基于帧数的,在我的一次开 ...

  2. Fragment调用startActivityForResult导致的回调Activity无法获取正确的requestId的问题

    今天遇到了一个问题 从Fragment内调用了startActivityForResult方法设置了requestId是1 但是在Activity内的onActivityResult的回调内 获得的r ...

  3. hbase之认识

    进入HBase客户端命令操作界面    $ bin/hbase shell 查看帮助命令        hbase(main):001:0> help 查看当前数据库中有哪些表        h ...

  4. Matlab -- Portfolio

    1.创建空 p = Portfolio; 2.需要了解 均值,方差,协方差实现 X为矩阵 均值 = mean(X): 中位数 = median(X): 方差 = var(X): 标准差 = std(X ...

  5. spring-boot log

    最近也在研究项目

  6. gulp打开gbk编码的html文件乱码

    先上图,好忧伤:

  7. ANSI/ISO C 关键字(汇总)

    ANSI/ISO C 关键字 汇总: auto  break  case  char  const  continue  default  do  double  else  enum  extern ...

  8. Eclipse手动添加web.xml

    当创建web工程时,没有自动创建web.xml 这时候就需要手动添加web.xml 该怎么做呢 右键项目,点击java EE Tools 其中点击Genertate Deployment Descri ...

  9. Loadrunner与idea编写加密的java Vusers脚本总结

    Loadrunner与idea编写加密的java Vusers脚本总结 准备工作:   jdk版本的选择:       Loadrunner11 使用版本jdk1.6 32位(如果使用1.7的Load ...

  10. 新建项目找不到android studio:appcompat v7:27.+包。

    1.我们在build.gradle(project)中添加maven中的google库: allprojects { repositories { jcenter() maven { url &quo ...