将JavaScript字符串数组POST到Spring Boot/Thymeleaf表单对象中。

huangapple go评论67阅读模式
英文:

POST a JavaScript array of Strings into Spring Boot/Thymeleaf form Object

问题

I am using Spring Boot and Thymeleaf with some vanilla JavaScript. In my JavaScript, I have an array of strings as below:

var tags = ["ramen", "japanese"];

I want to use JavaScript to dynamically add/remove items in this array before it gets submitted to the backend. The user can add 0..* tags.

Data gets submitted to the backend only via HTML <form> submits. There is also a model attribute which captures these details from the HTML form into Spring called VenueForm.

<form id="tag-form" class="form" method="POST" th:object="${venueForm}" th:action="/venue/tag">

VenueForm.java

public class VenueForm {
    private List<String> tags;
    private Object otherStuff;
    ...
    // Getters and Setters
}

VenueController.java

@Controller
@RequestMapping("/venue/tag")
public class VenueController {

    @PostMapping
    public String createVenue(@ModelAttribute VenueForm venueForm){
       List<String> tags = venueForm.getTags();
       System.out.println(tags);
       return "tags";
    }
}

How do I assign the JavaScript array tags to the Thymeleaf form List field tags?

Usually the input comes from th:field="${tags}" in an input field but because it's an array, I am not sure how to achieve this. Any help would be much appreciated. Please note jQuery is not an option, only vanilla JS.

I have tried to stop the HTML form submit using JS, then assigning the input field using JS but that did not work.

<input type="submit" value="Submit" th:onsubmit="submitForm(event)">

main.js

function submitForm(event){
    event.preventDefault();
    venueTagInput.value = JSON.stringify(tags);
    console.log("submitting...");
}
英文:

I am using Spring Boot and Thymeleaf with some vanilla JavaScript. In my JavaScript, I have an array of strings as below:

var tags = [&quot;ramen&quot;, &quot;japanese&quot;];

I want to use JavaScript to dynamically add/remove items in this array before it gets submitted to the backend. The user can add 0..* tags.

Data gets submitted to the backend only via HTML &lt;form&gt; submits. There is also a model attribute which captures these details from the HTML form into Spring called VenueForm.

&lt;form id=&quot;tag-form&quot; class=&quot;form&quot; method=&quot;POST&quot; th:object=&quot;${venueForm} th:action=&quot;/venue/tag&quot;&gt;

VenueForm.java

public class VenueForm {
    private List&lt;String&gt; tags;
    private Object otherStuff;
    ...
    // Getters and Setters
}

VenueController.java

@Controller
@RequestMapping(&quot;/venue/tag&quot;)
public class VenueController {

    @PostMapping
    public String createVenue(@ModelAttribute VenueForm venueForm){
       List&lt;String&gt; tags = venueForm.getTags();
       System.out.printLn(tag);
       return &quot;tags&quot;
    }
}

How do I assign the JavaScript array tags to the Thymeleaf form List field tags?

Usually the input comes from th:field=&quot;*{tags}&quot; in an input field but because its an array I am not sure how to achieve this. Any help would be much appreciated. Please note jQuery is not an option, only vanilla JS.

I have tried to stop the HTML form submit using JS, then assigning the input field using JS but that did not work.

&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; th:onsubmit=&quot;submitForm(event)&quot;&gt;

main.js

function submitForm(event){
    event.preventDefault();
    venueTagInput.value = JSON.stringify(tags);
    console.log(&quot;sumitting...&quot;);
}

答案1

得分: 0

这个现有的帖子帮助我弄清楚了这一点(使用HTML发送数组)!Thymeleaf只是服务器端的模板引擎,它渲染成基本的HTML。在表单中发送一个元素数组不需要做任何Thymeleaf特定的事情。您只需按照基本的HTML方法使用输入字段上的name属性。您可以使用JavaScript创建新的DOM元素,并确保对于每个元素,索引都会递增。控制器端不需要进行任何更改。Spring可以识别name="tags[0]"为tags.get(0)。

<input type="text" class="form-control tag" id="1" name="tags[0]" value="">
英文:

This existing post has helped me figure this out (Send Array using HTML)! Thymeleaf is just a templating engine on the server side, it renders into basic HTML. There is no need to do anything Thymeleaf specific to send an array of elements in the form. You have to follow the basic HTML way using the name attribute on an input field. You can use Javascript to create new DOM elements and making sure that for each element the index is incremented. No changes are needed on the controller side. Spring can work out that name="tags[0]" is tags.get(0);

&lt;input type=&quot;text&quot; class=&quot;form-control tag&quot; id=&quot;1&quot; name=&quot;tags[0]&quot; value=&quot;&quot;&gt;

huangapple
  • 本文由 发表于 2023年1月9日 01:24:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定