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?

4
  • 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. Commented Jul 6, 2022 at 23:28
  • You can take the tour and read How to Ask for more guidance on how to write a good question. The guidelines for providing a minimal reproducible example may also help, in this specific case. Commented Jul 6, 2022 at 23:29
  • That is for String Data not for HashMap data. I Already knew how to send String variables in that way. Commented Jul 7, 2022 at 9:00
  • Take a look at JavaScript inlining. There are various questions and answers on SO with more examples (here is one). Maybe you already know about this, also? Commented Jul 7, 2022 at 12:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.