如何在JSP中绑定动态字段列表 [英] How to bind a dynamic list of fields in a JSP

查看:90
本文介绍了如何在JSP中绑定动态字段列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个用于输入足球比赛结果的JSP页面.我得到了未解决游戏的列表,我想这样列出它们:

I am building a JSP page for entering football game results. I get a list of unsettled games and I want to list them like this:

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 set up the binding so that the controller can access these fields after the form is submitted.

有人可以指导我朝着正确的方向前进吗?我正在使用Spring MVC 3.1

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

推荐答案

Spring可以

Spring can bind indexed properties, so you need to create a list of game info objects on your command, like:

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
}

在您的控制器中:

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

在您的JSP中,您将必须设置输入路径,例如:

In your JSP you will have to set the paths for the inputs like:

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 
....

如果我没记错的话,在Spring 3中, Spring MVC和动态表格数据的处理:AutoPopulatingList ).

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

这篇关于如何在JSP中绑定动态字段列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆