Java 如何将多个输入文本字段数据捕获到列表中,并使用JSTL对其进行迭代?

Java 如何将多个输入文本字段数据捕获到列表中,并使用JSTL对其进行迭代?,java,spring,jsp,spring-mvc,jstl,Java,Spring,Jsp,Spring Mvc,Jstl,我正在努力做到以下几点。在我的JSP中,我为接下来的12个月创建了一个标题为“2013年10月”、“2013年11月”的表。i、 e.至“2014年9月” 这些月份和年份应为当前月份,例如,如果该月份为2013年11月,则系统应检测到该月份为2013年11月,并应增加12个月,直到明年2014年10月 有12个输入字段(每个月一个),它们位于一行中。 用户可以在此表中动态添加新行,并且可以在这些文本字段中输入月份的任何输入字段的数量值 <table border="1" class="at

我正在努力做到以下几点。在我的JSP中,我为接下来的12个月创建了一个标题为“2013年10月”、“2013年11月”的表。i、 e.至“2014年9月”

这些月份和年份应为当前月份,例如,如果该月份为2013年11月,则系统应检测到该月份为2013年11月,并应增加12个月,直到明年2014年10月

有12个输入字段(每个月一个),它们位于一行中。 用户可以在此表中动态添加新行,并且可以在这些文本字段中输入月份的任何输入字段的数量值

<table border="1" class="atable" align="center" width="85%">
<tr>
<th>LRN Required</th>
<th>Oct 2013</th>
<th>Nov 2013</th>
<th>Dec 2013</th>
<th>Jan 2014</th>
<th>Feb 2014</th>
<th>Mar 2014</th>
<th>Apr 2014</th>
<th>May 2014</th>
<th>June 2014</th>
<th>Jul 2014</th>
<th>Aug 2014</th>
<th>Sept 2014</th>

</tr>
<tr>
<td></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>

</tr>
</table>

需要LRN
2013年10月
2013年11月
2013年12月
2014年1月
2014年2月
2014年3月
2014年4月
2014年5月
2014年6月
2014年7月
2014年8月
2014年9月
我正在使用SpringMVC,并希望使用JSTL实现此功能

模型类:

public class Forecast {

private int id;
private int quantity;
private String lastUpdatedBy;
private String rateCenter;
.....
getters and setters here...

}

My controller class looks like below...

@Controller
@SessionAttributes("forecastModel")
public class ForecastController extends PasBaseController {

    @Autowired
    HttpServletRequest request;


    protected static Logger log = Logger.getLogger(ForecastController.class);

    private static final String FRCST_MODEL = "forecastModel";


    @RequestMapping(value="preforecast.do")
    public String setUpPreforecast(final Model model, HttpServletRequest req) 
    {

        User user = WebUtil.getSignInUser(req);
        ForecastModel forecastModel = new ForecastModel();

        log.debug("User Info.");
        log.debug(user.getUserId());
        log.debug(user.getFullName());
        log.debug(user.getPhone());

        Long id = (long) 15260;

        List<Long> userNpaList = null;
        List<String> userOcnList = null;

        try{
            if(user != null)
            {

                userNpaList = PasServicesUtils.getAllNpaAssocForUserId(id);
                userOcnList = PasServicesUtils.geAllOcnAssocForUserId(id);

                model.addAttribute("userNpaList", userNpaList);
                model.addAttribute("userOcnList", userOcnList);

                forecastModel.setUserNpaList(userNpaList);
                forecastModel.setUserOcnList(userOcnList);

                model.addAttribute("forecastModel", forecastModel);
            }
        }catch(Exception e){
            log.error("List is NULL");
        }

        log.debug("Exiting setUpPreforecast() method.");

        return PasConstants.PREFORECAST;
    }



    @RequestMapping(value = {PasConstants.FORECAST})
    public String continueFromPreforecast(@ModelAttribute("forecastModel") ForecastModel forecastModel, Errors errors, HttpServletRequest request, final ModelMap modelMap) throws Exception
    {

    User user = WebUtil.getSignInUser(request);


    modelMap.addAttribute("ocn", forecastModel.getOcn());
    modelMap.addAttribute("phone", user.getPhone());
    modelMap.addAttribute("fax", user.getFax());

    modelMap.addAttribute("email", user.getEmail());

    modelMap.addAttribute("forecastRptDt", PasDate.displayDayMonthYearFormat2(new PasDate()));

    modelMap.addAttribute("npa", forecastModel.getNpa());



    List<String> rateCntrAbbrList;
    rateCntrAbbrList = PasServicesUtils.getAllRtCntrsAssocForNpa(forecastModel.getNpa());



    if(rateCntrAbbrList != null && rateCntrAbbrList.size() > 0)
    {
        modelMap.addAttribute("rateCntrAbbrList", rateCntrAbbrList);
    }

   //modelMap.addAttribute("rateCtr", forecastModel.getRtCntrId().
   //validateForecastData(forecastModel, errors, user);
   if (errors.hasErrors())
   {
       return PasConstants.PREFORECAST;

   }

    return PasConstants.FORECAST;
}

@RequestMapping(value = {PasConstants.FORECAST_SUCCESS}, method = RequestMethod.POST)
public String submitForecast(@ModelAttribute("forecastModel") ForecastModel forecastModel, Errors errors, HttpServletRequest request, final ModelMap modelMap) throws Exception
{

/* I am trying to access the months related data i.e. quantiy that user entered through the text fields in the above table. */

    ForecastServiceClient.createForecast(forecastModel);

    return PasConstants.FORECAST_SUCCESS;
}

My question is, how to capture user entered data into a list and pass it to the the controller class?

Do i need to create a separate class for 12 months and a year? 
Do I need to access them using a public List<String> getMonthsAndYear() {..} inside my 'Forecast' class since these months and a current year will be a part of this class only.

How do i iterate through list of months inside the JSP using JSTL? I do not want to use scriplet here.

Please, help me how to approach this problem so that the data entered by the user into input fields can be posted to the controller method for further processing.

Thanks,
公共类预测{
私有int-id;
私人整数数量;
私有字符串LastUpdateBy;
私有字符串速率中心;
.....
这里的接球手和接球手。。。
}
我的控制器类如下所示。。。
@控制器
@会期贡献(“预测模型”)
公共类ForecastController扩展了PasBaseController{
@自动连线
HttpServletRequest请求;
受保护的静态记录器log=Logger.getLogger(ForecastController.class);
私有静态最终字符串FRCST_MODEL=“forecastModel”;
@RequestMapping(value=“preforecast.do”)
公共字符串setUpPreforecast(最终模型,HttpServletRequest-req)
{
用户=WebUtil.getSignInUser(req);
ForecastModel ForecastModel=新ForecastModel();
调试(“用户信息”);
log.debug(user.getUserId());
log.debug(user.getFullName());
log.debug(user.getPhone());
长id=(长)15260;
List userNpaList=null;
List userOcnList=null;
试一试{
如果(用户!=null)
{
userNpaList=passersvicesutils.getAllNpaAssocForUserId(id);
userOcnList=passersvicesutils.geAllOcnAssocForUserId(id);
addAttribute(“userNpaList”,userNpaList);
addAttribute(“userOcnList”,userOcnList);
forecastModel.setUserNpaList(userNpaList);
forecastModel.setUserOcnList(userOcnList);
addAttribute(“forecastModel”,forecastModel);
}
}捕获(例外e){
log.error(“列表为空”);
}
debug(“正在退出setUpPreforecast()方法”);
返回PasConstants.PREFORECAST;
}
@RequestMapping(值={PasConstants.FORECAST})
公共字符串continueFrompreCast(@ModelAttribute(“forecastModel”)forecastModel forecastModel,Errors Errors,HttpServletRequest请求,final ModelMap ModelMap ModelMap)引发异常
{
User=WebUtil.getSignInUser(请求);
addAttribute(“ocn”,forecastModel.getOcn());
modelMap.addAttribute(“phone”,user.getPhone());
modelMap.addAttribute(“fax”,user.getFax());
modelMap.addAttribute(“email”,user.getEmail());
modelMap.addAttribute(“forecastRptDt”,PasDate.displayDayMonthYearFormat2(new PasDate());
addAttribute(“npa”,forecastModel.getNpa());
列表比率trabbr列表;
rateCntrAbbrList=PasServicesUtils.getAllRTCntrassocforNPA(forecastModel.getNpa());
if(rateCntrAbbrList!=null&&rateCntrAbbrList.size()>0)
{
addAttribute(“rateCntrAbbrList”,rateCntrAbbrList);
}
//modelMap.addAttribute(“rateCtr”,forecastModel.getRtCntrId()。
//验证预测数据(预测模型、错误、用户);
if(errors.hasErrors())
{
返回PasConstants.PREFORECAST;
}
返回PasConstants.FORECAST;
}
@RequestMapping(值={PasConstants.FORECAST_SUCCESS},方法=RequestMethod.POST)
公共字符串submitForecast(@modeldattribute(“forecastModel”)forecastModel forecastModel,Errors Errors,HttpServletRequest请求,final ModelMap ModelMap)引发异常
{
/*我试图访问与月份相关的数据,即用户通过上表中的文本字段输入的数量*/
createForecast(forecastModel);
返回PasConstants.FORECAST\u SUCCESS;
}
我的问题是,如何将用户输入的数据捕获到列表中并将其传递给控制器类?
我是否需要创建一个为期12个月和一年的单独课程?
我是否需要在我的“Forecast”类中使用公共列表getMonthsAndYear(){..}访问它们,因为这些月份和当前年份将仅是该类的一部分。
如何使用JSTL遍历JSP中的月份列表?我不想在这里使用scriplet。
请帮助我如何处理此问题,以便用户输入到输入字段中的数据可以发布到控制器方法以进行进一步处理。
谢谢
型号

public class Forecast {
      ...     
      private List<String> sections;
      /* getters/setters */
    }

John,谢谢你的回复。我做了一些研究,发现…可以绑定一个动态列表。我修改了我的控制器类,更新了我的模型类,并添加了一个列表getter和setter方法,正如你上面提到的,而且,我现在正在使用spring framework的AutoPopulationGlist修改列表,并通过modelMap.addAttribute将其传递给JSP(“ForecastMonthBeanList”,forecastModel.GetForecastMonthBeanList())然而,我的问题是,在jsp方面,我有一个jQuery函数,它应该在每次单击它时创建一个新行。即,该行包含12个输入文本框/字段,一次单击它应该在新行中创建12个新文本框,但它不起作用。我不确定如何使这个jQuery函数单击“添加行”按钮时使用spring:bind。我的第一行有12个输入文本框,其中默认值从模型类中声明为getter和setter的bean分配为“0”。下面是我的jQuery函数和JSP代码

<script type="text/javascript">
$("document").ready(function(){
$(".alternativeRow").btnAddRow({oddRowCSS:"oddRow",evenRowCSS:"evenRow"});
$(".delRow").btnDelRow();
});
</script>

$(“文档”).ready(函数(){
$(“.alternativeRow”).btnAddRow({oddRowCSS:“oddRow”,evenRowCSS:“evenRow”
<script type="text/javascript">
$("document").ready(function(){
$(".alternativeRow").btnAddRow({oddRowCSS:"oddRow",evenRowCSS:"evenRow"});
$(".delRow").btnDelRow();
});
</script>