i wanted to know, how Lists are getting binded in Spring...and how to access them in Thymeleaf. With and without WrapperClass How could achieve adding single / multiple objects through an form to an ArrayList or normal Array..
I would be very thankful for an explanation.
Thats what i have tried: Form
<form action="#" th:action="@{/tests}" th:object="${features}" method="post">
<input type="text" th:field="*{featureArrayList[0].name}"/>
<input type="text" th:field="*{featureArrayList[1].name}"/>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
Wrapper Class
ArrayList<Feature> featureArrayList = new ArrayList<Feature>();
public FeatureWrapper() {
}
public ArrayList<Feature> getFeatureArrayList() {
return featureArrayList;
}
public void setFeatureArrayList(ArrayList<Feature> featureArrayList) {
this.featureArrayList = featureArrayList;
}
Model:
private String name;
public Feature(String name) {
this.name = name;
}
public Feature() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Controller
@GetMapping("/tests")
public String showForm(Model model)
{
FeatureWrapper featureWrapper = new FeatureWrapper();
model.addAttribute("features", featureWrapper);
return "Feature";
}
@PostMapping("/tests")
public String proceedForm(@ModelAttribute("features") FeatureWrapper features,Model model)
{
for(Feature feature: features.getFeatureArrayList())
{
System.out.println(feature.getName());
System.out.println(features.getFeatureArrayList().size());
}
return "Feature";
}
Edit1: Now its working with an Wrapper Class and using static Indexes... How would i achieve this without a wrapper class , i read much about the EL expression ${var.index} , but how is this working ? And how should i use this to dynamically add my Objects. Edit2: Now im able to add single objects , and multiple objects to the list, but im not using any EL Expression, so i wanted to know when would i need to use something like this:
<div th:each="feature,stat:*{featureArrayList}">
<input id="myInput" type="text" th:field="*{featureArrayList[__${stat.index}__].name}"/>
</div>