简体   繁体   English

将 javascript HashMap 发送到 Thymeleaf 字段构造函数

[英]Send javascript HashMap to Thymeleaf field constructor

I have a normal Java object that holds a parameter of type我有一个普通的 Java 对象,它包含一个类型的参数

private HashMap<String, String> lizt = new HashMap()

public ObjectTemp(HashMap<String, String> lizt ) {
    this.lizt = lizt ;
}

And a JavaScript map that contains the data以及包含数据的 JavaScript 地图

var map1 = new Map();

When the user clicks the button当用户点击按钮时

<form  action="#" class="ui_formz" th:action="@{/SomeMethod}" th:object="${object_temp}"  method="post">

<button class="btn btn-primary" onclick="dothis(${object_temp.lizt })" type="button "> Buy </button>

</form>

In HTML, it calls a function在 HTML 中,它调用一个函数

function dothis(argm){
    argm=map1;
}

However, it does not work.但是,它不起作用。 The constructor parameters do not receive or do anything.构造函数参数不接收也不做任何事情。 Anybody can explain why and what I am doing wrong?任何人都可以解释为什么以及我做错了什么?

For further explanation.作进一步说明。

Basically, it does not register the map from JavaScript.基本上,它不会从 JavaScript 注册地图。 It simply returns an object with an empty map它只是返回一个带有空映射的对象

this.lizt = {[]}

I want to receive an HashMap stored in JavaScript to Java.我想接收存储在 JavaScript 中的 HashMap 到 Java。 Usually, the data would be an integer or a string or others primitive and it would do the job by simply storing the variable in HTML.通常,数据将是整数或字符串或其他原语,它只需将变量存储在 HTML 中即可完成工作。

<input value="data_stored"></input>

This times it is an HashMap present in a JavaScript variable, not a string, so I don't know what to do.这次它是 JavaScript 变量中存在的 HashMap,而不是字符串,所以我不知道该怎么做。 I am not working with a string but a map.我不是在使用字符串,而是在使用地图。 Any solution?有什么解决办法吗?

Edit.编辑。 Ok so i found the solution好的,所以我找到了解决方案

kinda like this有点像这样

Javascript to Java using JSON 使用 JSON 的 Javascript 到 Java

the step are quite easy:这一步很简单:

  1. Javascript compiles bunch of data on current page and stores it in array. Javascript 编译当前页面上的一堆数据并将其存储在数组中。
  2. Array object encoded into JSON.编码为 JSON 的数组对象。
  3. Java code saves JSON. Java 代码保存 JSON。

to store data in array将数据存储在数组中

map1.set(key1,value1);

to save data in json:将数据保存在 json 中:

const objFromMap = Object.fromEntries(map1);
JSON.stringify(objFromMap);

to send data from JavaScript to Java将数据从 JavaScript 发送到 Java

in javascript在javascript中

function dothis(){

        var input1 = document.getElementById('str_res');
        input1.value = JSON.stringify(objFromMap);
        }

in hml thymleaf在 hml 百里香叶中

<form  action="#" class="ui_formz" th:action="@{/ControllerMethod1}" th:object="${result_name}"  method="post">
                 <input type="hidden" id="str_res" value="random"  th:field="${comanda_temp.rsep}">
                    <button class="btn btn-primary" onclick="dothis()"
                         type="button ">Submit</button>
                 </form>

in Java Standard SprinBoot code:在 Java 标准 SrinBoot 代码中:

Created new Object with only a String field called JsonHolder and his getter,setter and toString()仅使用名为 JsonHolder 的字符串字段及其 getter、setter 和 toString() 创建新对象

At the start added to the model with在开始时添加到模型中

        model.addAttribute("comanda_temp",new JsonHolder());

When clicked submit点击提交时

@RequestMapping(value = "/ControllerMethod1",method = RequestMethod.POST)
public String SubmitJsonResult(Model model, @ModelAttribute("comanda_tempx") JsonHolder cmd) {
    PrintPrettyJson(cmd.getString());

PrintPrettyJson is just a private method to print to the console using gson library for Json parsing and manipulation PrintPrettyJson 只是使用 gson 库打印到控制台的私有方法,用于 Json 解析和操作

Also this solution should work with array or others type of list stored in a javascript data.此外,此解决方案应与存储在 javascript 数据中的数组或其他类型的列表一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM