Pass data to spring boot controller using thymeleaf from javascript innerHtml










1














I will create table row in javascript , in table row I have some input field and now I want to access data from this fields using thymeleaf



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';











share|improve this question























  • what is the issue are you facing?
    – greengreyblue
    Nov 12 at 9:15










  • i want to send data from the table to my spring boot controller but can not resolve it
    – NILoy
    Nov 12 at 9:18











  • Edit the question and add the details and error are you seeing.
    – greengreyblue
    Nov 12 at 9:20










  • I have edited question description
    – NILoy
    Nov 12 at 9:26















1














I will create table row in javascript , in table row I have some input field and now I want to access data from this fields using thymeleaf



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';











share|improve this question























  • what is the issue are you facing?
    – greengreyblue
    Nov 12 at 9:15










  • i want to send data from the table to my spring boot controller but can not resolve it
    – NILoy
    Nov 12 at 9:18











  • Edit the question and add the details and error are you seeing.
    – greengreyblue
    Nov 12 at 9:20










  • I have edited question description
    – NILoy
    Nov 12 at 9:26













1












1








1







I will create table row in javascript , in table row I have some input field and now I want to access data from this fields using thymeleaf



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';











share|improve this question















I will create table row in javascript , in table row I have some input field and now I want to access data from this fields using thymeleaf



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';








javascript spring-boot thymeleaf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 9:22

























asked Nov 12 at 9:08









NILoy

84




84











  • what is the issue are you facing?
    – greengreyblue
    Nov 12 at 9:15










  • i want to send data from the table to my spring boot controller but can not resolve it
    – NILoy
    Nov 12 at 9:18











  • Edit the question and add the details and error are you seeing.
    – greengreyblue
    Nov 12 at 9:20










  • I have edited question description
    – NILoy
    Nov 12 at 9:26
















  • what is the issue are you facing?
    – greengreyblue
    Nov 12 at 9:15










  • i want to send data from the table to my spring boot controller but can not resolve it
    – NILoy
    Nov 12 at 9:18











  • Edit the question and add the details and error are you seeing.
    – greengreyblue
    Nov 12 at 9:20










  • I have edited question description
    – NILoy
    Nov 12 at 9:26















what is the issue are you facing?
– greengreyblue
Nov 12 at 9:15




what is the issue are you facing?
– greengreyblue
Nov 12 at 9:15












i want to send data from the table to my spring boot controller but can not resolve it
– NILoy
Nov 12 at 9:18





i want to send data from the table to my spring boot controller but can not resolve it
– NILoy
Nov 12 at 9:18













Edit the question and add the details and error are you seeing.
– greengreyblue
Nov 12 at 9:20




Edit the question and add the details and error are you seeing.
– greengreyblue
Nov 12 at 9:20












I have edited question description
– NILoy
Nov 12 at 9:26




I have edited question description
– NILoy
Nov 12 at 9:26












1 Answer
1






active

oldest

votes


















0














You need to add a name to each input and then on your controller, ask for those parameters. Let's assume you you expected url is /edit.



JS



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="" name="id">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea id="description" name="description"></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';



Controller



@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@RequestParam("id") String id,
@RequestParam("description") String description ...


Save (JS)



Now, since you don't have a form, if you want to submit your data, you will need one. In this case, we will do it using javascript.



function save() 

// First, let's create the form.
var form = document.createElement("form");
form.method = "POST";
form.action = "/edit";

// Now, let's add the inputs.
var id = document.getElementById('id');
var description = document.getElementBy('description');
form.appendChild(id);
form.appendChild(description);

// Submit the form.
document.body.appendChild(form);
form.submit();




Another, more easy way, would be to wrap your html code in a form. Of course, you would need to duplicate your code, since you would need both the edit and the delete form.



Hope it helps.






share|improve this answer






















  • in that case you need to apply controller function to javascript any how
    – NILoy
    Nov 13 at 4:32










  • After reading the comments on your question I finally understood your needs. I edited my question. Hope it helps. By the way, Thymeleaf is not needed for this, unless you wanted to map an object. If you wanted that, you would need to wrap everything with a form. There would be no escaping it.
    – Alain Cruz
    Nov 13 at 4:46











  • it's not working yet ,it there any dependency issue
    – NILoy
    Nov 13 at 9:02










  • Are you getting a 404 error or it simply doesn’t submit?
    – Alain Cruz
    Nov 13 at 10:33










  • I think I see my problem, I forgot to add the form to the body. I just edited my answer. Should work now.
    – Alain Cruz
    Nov 13 at 10:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258893%2fpass-data-to-spring-boot-controller-using-thymeleaf-from-javascript-innerhtml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to add a name to each input and then on your controller, ask for those parameters. Let's assume you you expected url is /edit.



JS



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="" name="id">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea id="description" name="description"></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';



Controller



@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@RequestParam("id") String id,
@RequestParam("description") String description ...


Save (JS)



Now, since you don't have a form, if you want to submit your data, you will need one. In this case, we will do it using javascript.



function save() 

// First, let's create the form.
var form = document.createElement("form");
form.method = "POST";
form.action = "/edit";

// Now, let's add the inputs.
var id = document.getElementById('id');
var description = document.getElementBy('description');
form.appendChild(id);
form.appendChild(description);

// Submit the form.
document.body.appendChild(form);
form.submit();




Another, more easy way, would be to wrap your html code in a form. Of course, you would need to duplicate your code, since you would need both the edit and the delete form.



Hope it helps.






share|improve this answer






















  • in that case you need to apply controller function to javascript any how
    – NILoy
    Nov 13 at 4:32










  • After reading the comments on your question I finally understood your needs. I edited my question. Hope it helps. By the way, Thymeleaf is not needed for this, unless you wanted to map an object. If you wanted that, you would need to wrap everything with a form. There would be no escaping it.
    – Alain Cruz
    Nov 13 at 4:46











  • it's not working yet ,it there any dependency issue
    – NILoy
    Nov 13 at 9:02










  • Are you getting a 404 error or it simply doesn’t submit?
    – Alain Cruz
    Nov 13 at 10:33










  • I think I see my problem, I forgot to add the form to the body. I just edited my answer. Should work now.
    – Alain Cruz
    Nov 13 at 10:36















0














You need to add a name to each input and then on your controller, ask for those parameters. Let's assume you you expected url is /edit.



JS



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="" name="id">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea id="description" name="description"></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';



Controller



@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@RequestParam("id") String id,
@RequestParam("description") String description ...


Save (JS)



Now, since you don't have a form, if you want to submit your data, you will need one. In this case, we will do it using javascript.



function save() 

// First, let's create the form.
var form = document.createElement("form");
form.method = "POST";
form.action = "/edit";

// Now, let's add the inputs.
var id = document.getElementById('id');
var description = document.getElementBy('description');
form.appendChild(id);
form.appendChild(description);

// Submit the form.
document.body.appendChild(form);
form.submit();




Another, more easy way, would be to wrap your html code in a form. Of course, you would need to duplicate your code, since you would need both the edit and the delete form.



Hope it helps.






share|improve this answer






















  • in that case you need to apply controller function to javascript any how
    – NILoy
    Nov 13 at 4:32










  • After reading the comments on your question I finally understood your needs. I edited my question. Hope it helps. By the way, Thymeleaf is not needed for this, unless you wanted to map an object. If you wanted that, you would need to wrap everything with a form. There would be no escaping it.
    – Alain Cruz
    Nov 13 at 4:46











  • it's not working yet ,it there any dependency issue
    – NILoy
    Nov 13 at 9:02










  • Are you getting a 404 error or it simply doesn’t submit?
    – Alain Cruz
    Nov 13 at 10:33










  • I think I see my problem, I forgot to add the form to the body. I just edited my answer. Should work now.
    – Alain Cruz
    Nov 13 at 10:36













0












0








0






You need to add a name to each input and then on your controller, ask for those parameters. Let's assume you you expected url is /edit.



JS



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="" name="id">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea id="description" name="description"></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';



Controller



@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@RequestParam("id") String id,
@RequestParam("description") String description ...


Save (JS)



Now, since you don't have a form, if you want to submit your data, you will need one. In this case, we will do it using javascript.



function save() 

// First, let's create the form.
var form = document.createElement("form");
form.method = "POST";
form.action = "/edit";

// Now, let's add the inputs.
var id = document.getElementById('id');
var description = document.getElementBy('description');
form.appendChild(id);
form.appendChild(description);

// Submit the form.
document.body.appendChild(form);
form.submit();




Another, more easy way, would be to wrap your html code in a form. Of course, you would need to duplicate your code, since you would need both the edit and the delete form.



Hope it helps.






share|improve this answer














You need to add a name to each input and then on your controller, ask for those parameters. Let's assume you you expected url is /edit.



JS



function myCreateFunction2() 
var table = document.getElementById("mdlTable2");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = "Item";
cell2.innerHTML = '<input id="item" type="number" value="" name="id">';
cell3.innerHTML = "UOM";
cell4.innerHTML = '<textarea id="description" name="description"></textarea>';
cell5.innerHTML = '<button type="submit" value="save" class="btn btn-success btn-sm" onclick="save()">Save</button></form>';
cell6.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onclick="myDeleteFunction2()">Remove</button>';



Controller



@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@RequestParam("id") String id,
@RequestParam("description") String description ...


Save (JS)



Now, since you don't have a form, if you want to submit your data, you will need one. In this case, we will do it using javascript.



function save() 

// First, let's create the form.
var form = document.createElement("form");
form.method = "POST";
form.action = "/edit";

// Now, let's add the inputs.
var id = document.getElementById('id');
var description = document.getElementBy('description');
form.appendChild(id);
form.appendChild(description);

// Submit the form.
document.body.appendChild(form);
form.submit();




Another, more easy way, would be to wrap your html code in a form. Of course, you would need to duplicate your code, since you would need both the edit and the delete form.



Hope it helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 10:35

























answered Nov 12 at 13:33









Alain Cruz

1,7111818




1,7111818











  • in that case you need to apply controller function to javascript any how
    – NILoy
    Nov 13 at 4:32










  • After reading the comments on your question I finally understood your needs. I edited my question. Hope it helps. By the way, Thymeleaf is not needed for this, unless you wanted to map an object. If you wanted that, you would need to wrap everything with a form. There would be no escaping it.
    – Alain Cruz
    Nov 13 at 4:46











  • it's not working yet ,it there any dependency issue
    – NILoy
    Nov 13 at 9:02










  • Are you getting a 404 error or it simply doesn’t submit?
    – Alain Cruz
    Nov 13 at 10:33










  • I think I see my problem, I forgot to add the form to the body. I just edited my answer. Should work now.
    – Alain Cruz
    Nov 13 at 10:36
















  • in that case you need to apply controller function to javascript any how
    – NILoy
    Nov 13 at 4:32










  • After reading the comments on your question I finally understood your needs. I edited my question. Hope it helps. By the way, Thymeleaf is not needed for this, unless you wanted to map an object. If you wanted that, you would need to wrap everything with a form. There would be no escaping it.
    – Alain Cruz
    Nov 13 at 4:46











  • it's not working yet ,it there any dependency issue
    – NILoy
    Nov 13 at 9:02










  • Are you getting a 404 error or it simply doesn’t submit?
    – Alain Cruz
    Nov 13 at 10:33










  • I think I see my problem, I forgot to add the form to the body. I just edited my answer. Should work now.
    – Alain Cruz
    Nov 13 at 10:36















in that case you need to apply controller function to javascript any how
– NILoy
Nov 13 at 4:32




in that case you need to apply controller function to javascript any how
– NILoy
Nov 13 at 4:32












After reading the comments on your question I finally understood your needs. I edited my question. Hope it helps. By the way, Thymeleaf is not needed for this, unless you wanted to map an object. If you wanted that, you would need to wrap everything with a form. There would be no escaping it.
– Alain Cruz
Nov 13 at 4:46





After reading the comments on your question I finally understood your needs. I edited my question. Hope it helps. By the way, Thymeleaf is not needed for this, unless you wanted to map an object. If you wanted that, you would need to wrap everything with a form. There would be no escaping it.
– Alain Cruz
Nov 13 at 4:46













it's not working yet ,it there any dependency issue
– NILoy
Nov 13 at 9:02




it's not working yet ,it there any dependency issue
– NILoy
Nov 13 at 9:02












Are you getting a 404 error or it simply doesn’t submit?
– Alain Cruz
Nov 13 at 10:33




Are you getting a 404 error or it simply doesn’t submit?
– Alain Cruz
Nov 13 at 10:33












I think I see my problem, I forgot to add the form to the body. I just edited my answer. Should work now.
– Alain Cruz
Nov 13 at 10:36




I think I see my problem, I forgot to add the form to the body. I just edited my answer. Should work now.
– Alain Cruz
Nov 13 at 10:36

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258893%2fpass-data-to-spring-boot-controller-using-thymeleaf-from-javascript-innerhtml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto