• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to pass a list from the view to the controller in Spring MVC with thymeleaf?

I have an html page to edit a list of persons.
When the page is opened the controller gets the list of persons from the db and binds to the view.
That works fine.

Then the user edits the data and clicks save.
Now I need to submit the edited data of each person to the controller so that the controller can save the edits.

I'm trying to do that using @ModelAttribute ArrayList<Person> as shown below but it's not working.
The arraylist comes empty.

How do I do for the arraylist to come filled with all of the persons objects from the form?

View


Controller
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing first, you need to bind your modelattribute to a th:object in your form as this: ModelAttribute("personList") then in your form your have this th:object="${personList}".
Number 2: You will need a name attribute on your form in other to let thymeleaf bind your form input to your object attributes.
Number 3: Since you are submitting a list of object, the object needs to be indexed in order to be submitted as list.
Your new form will look like this;


 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i used your code verbatim in a local example, but i'm getting nothing in the controller for @ModelAttribute ArrayList<Person> personList

wherein lies my misunderstanding?

 
Bartender
Posts: 2085
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John Camp,
You may want to show us your controller and Thymleaf.
 
John Camp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THYMELEAF

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<form action="editpersons" method="post" th:object="${personList}">
<th:block th:each="person : ${personBind}">
 
   Name:
   <input type="text"
th:name="${personList[__${personStat.index}__].name}"
th:value="${person.name}" />
</br>
</br>
 
   Age:
   <input type="text"
th:name="${personList[__${personStat.index}__].age}"
th:value="${person.age}" />
</br>
</br>

</th:block>
</br>
</br> <input type="submit" name="btnSaveEdit" value="Save" />
</form>

CONTROLLER

@RequestMapping(value = "/editpersons", params = "btnSaveEdit", method = RequestMethod.POST)
   public String saveEditPersons(@ModelAttribute ArrayList<Person> personList){
System.out.println("inside edit code personsList size = " + personList.size());
for (Person p : personList) {
System.out.println("person is " + p.getName());
}
               return "index";
   }

MODEL

@Entity
@Table(name = "person")
public class Person {

@Id
@GeneratedValue(strategy =  GenerationType.IDENTITY)
@Column(name="id")
private int id;


@Column(name = "personList")
private ArrayList<Person>  personList;

@Column(name = "name")
private String  name;

@Column(name = "age")
private int age;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public ArrayList<Person> getPersonList() {
return personList;
}

public void setPersonList(ArrayList<Person> personList) {
this.personList = personList;
}


}
 
Himai Minh
Bartender
Posts: 2085
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should it be:
 ?
 
John Camp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but that doesn't work either.
 
Himai Minh
Bartender
Posts: 2085
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe, you can post your code on your github for people to take a look.
 
John Camp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not sure how to do that. do you have some 'steps' you can send me?
 
John Camp
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually, i just googled it.  my file is zipped and can be found at

https://github.com/jncamp/johns-java

let me know if there is a problem getting it.

thanks!!
 
Himai Minh
Bartender
Posts: 2085
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, John, I look at your code and there is quiet some work to do.
First , we need to build a correct JPA entity:
Entity


We also need a service like this:





Under templates, put this add.html:



Maybe , under resources, add this data.sql:


Add these to your pom to use h2 database for test purpose for now:


Your application.properties should be:



Reference:
https://howtodoinjava.com/spring-boot2/crud-application-thymeleaf/
 
John Camp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WOW!! lots of effort here!!!  is there a way to ping you a contribution for this?
 
Himai Minh
Bartender
Posts: 2085
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, you can make the PersonRepository to be CrudRepository.
The good thing is the Person will be returned as Optional. If this person does not exist, you can create it.






//Controller
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic