How to link dynamic list of fields in JSP

I am creating a JSP page for entering football results. I get a list of outstanding games, and I want to list them as follows:

team1 vs team4 [hidden field: game id] [input field for home goals] [input field for away goals] team2 vs team5 [hidden field: game id] [input field for home goals] [input field for away goals] 

I never know how many games will be listed. I am trying to figure out how to configure the binding so that the controller can access these fields after submitting the form.

Can someone please direct me in the right direction. I am using Spring MVC 3.1

+4
source share
1 answer

Spring can bind indexed properties , so you need to create a list of game information objects in your team, for example:

 public class Command { private List<Game> games = new ArrayList<Game>(); // setter, getter } public class Game { private int id; private int awayGoals; private int homeGoals; // setters, getters } 

In your controller:

 @RequestMapping(value = "/test", method = RequestMethod.POST) public String test(@ModelAttribute Command cmd) { // cmd.getGames() .... return "..."; } 

In your JSP, you will need to set the paths for the inputs, for example:

 games[0].id games[0].awayGoals games[0].homeGoals games[1].id games[1].awayGoals games[1].homeGoals games[2].id games[2].awayGoals games[2].homeGoals .... 

If I'm not mistaken, in Spring 3, auto-growth collections are now the default behavior for binding lists, but for lower versions you had to use an AutoPopulatingList instead of just an ArrayList (as a reference: Spring MVC and dynamic form data processing: AutoPopulationList ).

+4
source

All Articles