0

I have a normal Java object that holds a parameter of type

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

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

And a JavaScript map that contains the data

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

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. It simply returns an object with an empty map

this.lizt = {[]}

I want to receive an HashMap stored in JavaScript to 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.

<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. I am not working with a string but a map. Any solution?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
b s
  • 1
  • 2
  • The question does not give us a full picture of what the code is doing, or what "_does not work_" actually means here (e.g. specific error messages). Having said that, take a look at [How to pass arguments to javascript function call when using onClick in Thymeleaf](https://stackoverflow.com/q/55726362/12567365). – andrewJames Jul 06 '22 at 23:28
  • You can take the [tour] and read [ask] for more guidance on how to write a good question. The guidelines for providing a [mre] may also help, in this specific case. – andrewJames Jul 06 '22 at 23:29
  • That is for String Data not for HashMap data. I Already knew how to send String variables in that way. – b s Jul 07 '22 at 09:00
  • Take a look at [JavaScript inlining](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#javascript-inlining). There are various questions and answers on SO with more examples (here is [one](https://stackoverflow.com/q/50160484/12567365)). Maybe you already know about this, also? – andrewJames Jul 07 '22 at 12:49

1 Answers1

0

Edit. Ok so i found the solution

kinda like this

Javascript to Java using JSON

the step are quite easy:

  1. Javascript compiles bunch of data on current page and stores it in array.
  2. Array object encoded into JSON.
  3. Java code saves JSON.

to store data in array

map1.set(key1,value1);

to save data in json:

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

to send data from JavaScript to Java

in javascript

function dothis(){

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

in hml thymleaf

<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:

Created new Object with only a String field called JsonHolder and his getter,setter and 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

Also this solution should work with array or others type of list stored in a javascript data.

b s
  • 1
  • 2