英文:
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 = ["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(tag);
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 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.
<input type="submit" value="Submit" th:onsubmit="submitForm(event)">
main.js
function submitForm(event){
event.preventDefault();
venueTagInput.value = JSON.stringify(tags);
console.log("sumitting...");
}
答案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);
<input type="text" class="form-control tag" id="1" name="tags[0]" value="">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论