1

我正在尝试在下面做。在我的 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,
4

2 回答 2

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  */
}
于 2013-10-17T14:17:20.800 回答
0

约翰,谢谢你的回复。我做了一些研究,发现....可以绑定动态列表。我修改了我的控制器类,我更新了我的模型类并添加了一个列表 getter 和 setter 方法,如上所述,我现在使用 Spring 框架的 AutoPopulatingList 来修改列表并通过 modelMap.addAttribute(" predictMonthsBeanList",forecastModel.getForecastMonthsBeanList()),来自 Controller 方法。但是,我的问题是,在 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>
2013年10月 2013年11月 2013年12月 2014年1月 2014年2月 2014年3月 2014年4月 2014年5月 2014年6月 2014年7月 2014年8月 2014年9月 -----
于 2013-10-18T00:36:49.987 回答