Thymeleaf先后端传值 页面取值与js取值

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

    Thymeleaf 与 Javascripthtml

              Thymeleaf教程 (十二) 标签内,js中使用表达式前端

目的:
后端经过Model传值到前端
页面经过Model取值显示
js经过Model取值做为变量使用java

1.后台Controllerweb

@GetMapping("/message")
public String getMessage(Model model){
    model.addAttribute("message","This is your message");
    return "index";
}

注:向model中添加属性messageajax

2.页面经过Model取值spring

<p th:text="#{message}">default message</p>

3.js经过model取值json

<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 );
}
相关文章
相关标签/搜索