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

时间:2013-10-14 22:09:57

标签: java spring jsp spring-mvc jstl

我想在下面做。在我的JSP中,我创建了一个表格,其标题为“2013年10月”,“2013年11月”,....未来12个月。即到“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>

我正在使用Spring MVC,并希望使用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,

2 个答案:

答案 0 :(得分:0)

模型

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

控制器

@RequestMapping(method = RequestMethod.POST, value = "/test.do")
public String someController(@ModelAttribute Forecast  forecast ) {
  /* Do whatever with your  */
}

答案 1 :(得分:0)

约翰,谢谢你的回复。我做了一些研究,发现....可以绑定一个动态列表。我修改了我的控制器类,我更新了我的模型类,并添加了一个列表getter和setter方法,如上所述,我现在使用Spring框架的AutoPopulatingList修改列表并通过modelMap.addAttribute将其传递给JSP(“来自Controller方法的forecastMonthsBeanList“,forecastModel.getForecastMonthsBeanList())。然而,我的问题是在jsp方面,我有这个jQuery函数,它应该在每次单击时创建一个新行。即该行包含12个输入文本框/字段,并且在单击时它应该在新行中创建12个新文本框,但是,它不起作用。我不知道如何使这个jQuery函数与spring一起工作:单击“添加行”按钮时绑定。我的第一行有12个输入文本框,默认值从声明为模型类中的getter和setter的bean中指定为'0'。下面是我的jQuery函数和JSP代码。

<script type="text/javascript">
$("document").ready(function(){
$(".alternativeRow").btnAddRow({oddRowCSS:"oddRow",evenRowCSS:"evenRow"});
$(".delRow").btnDelRow();
});
</script>
    Oct 2013Nov 2013Dec 2013Jan 2014Feb 2014Mar 2014Apr 2014 2014年6月2014年7月2014年8月2014年9月                                                                                                                                                                                                                                                 -----