Passing List from Thymeleaf to Spring Controller

Passing List from Thymeleaf to Spring Controller

I've got the same issue in my Project Where the Hidden List value is already provided by Controller and then the same value needs to be passed further to controller.
I've got it resolved using @ModelAttribute annotation (I'm pasting same code of mine as per your Requirements) :---

1.) Create an object of the form (which needs to send further to controller after form submit) on the controller where you're sending the movies :-

    model.addAttribute("some_value", new YourDesiredJavaClass());

2.) Create Thymeleaf object on the form by the same name - "some_value" in this case :--

<form action="/movies" th:object="${some_value}" method="POST">
<input th:field="${movies}" type="hidden" ></input>
</form>

3.) get the List from Thymleaf To controller . :--

 @PostMapping("movies")
public String getMovies(@ModelAttribute("some_value") YourDesiredJavaClass requestClass, Model model) {

// further code ..

}


4.) Create the Request class with setter & getter methods . :---

  public class YourDesiredJavaClass{

private List<String> movies;

//setter & getter methods

}

How to pass a list from the view to the controller in Spring MVC with thymeleaf?

Spring MVC with Thymeleaf does indeed use model attributes to store data for the Thymeleaf pages. This guide shows how to load data from a form into the application. Try wrapping the ArrayList inside of a custom object and access the list via that object as a ModelAttribute rather than working with an ArrayList directly.

The saveEditPerson method should probably be named "saveEditPersons" since you are parsing a list of persons rather than just one.

Send list object from thymeleaf to controller

I solved this problem !
Set a size for ArrayList before bind it to thymeleaf !
I save my day. thanks Stackoverflow.

i fix my controller like this

@GetMapping("/luunhieuobject")
public String LoadNhieuObjectCungLuc(Model model) {
ListLoaiDoan listLoaiDoanAAA = new ListLoaiDoan();
ArrayList<LoaiDoan> LDD = new ArrayList<LoaiDoan>(10);
listLoaiDoanAAA.setLoaiDoans(LDD);
model.addAttribute("listLoaiDoan111", listLoaiDoanAAA);
return "/MHtrangchu/LuuNhieuObjCungLuc";
}

Spring MVC & Thymeleaf: pass object with list instance variable to controller

If you want to do it the same way you're doing pizza, it would look like this:

<th:block th:each="ingredient, status: ${pizza.ingredients}">
<input type="hidden" th:field="*{ingredients[__${status.index}__].id}" />
<input type="hidden" th:field="*{ingredients[__${status.index}__].name}" />
<input type="hidden" th:field="*{ingredients[__${status.index}__].type}" />
</th:block>

You can also look at using the @SessionAttributes attribute on your controller, and simply omit all the hidden fields. It will temporarily store the specified model attributes in the session and keep the value of the fields that are already specified.

How to post a list to controller in Thymeleaf

Invalid property 'testbean' of bean class [com.TestBean]

Use th:field="*{list[__${index}__].id} pattern without testbean definition. If you use * notation then it will reference to parent object.

java.lang.NumberFormatException: For input string: "null"

Add any variable like "rowStat" to the loop for keeping status index:

th:each="person,rowStat : ${testbean.list}"

and access it like th:field="*{list[__${rowStat.index}__].id}. So index will not be null and no problem converting the string to int.



Related Topics



Leave a reply



Submit