如何使用spring mvc 3绑定表单bean中的列表

时间:2013-06-18 10:23:48

标签: spring spring-mvc

我从列表中的数据库填充了一组数据。

List<ManageJob> contentList = jdbcTemplate.query(jobListQuery, new RowMapper() { 
public ManageJob mapRow(ResultSet rs, int rowNum) throws SQLException { 
ManageJob job = new ManageJob(); 
job.setContentId(rs.getString(1)); 
job.setContentName(rs.getString(2)); 
job.setContentPath(rs.getString(3)); 
return job; 
} 
});

form.setContentList(contentList); 

我在jsp中的不同文本框中显示它们。

<c:forEach items="${ManageJobForm.ContentList}" var="ContentList" >
<label for="${ContentList.contentId}">${ContentList.contentName}</label>
<input type="text" class="textboxStyle" name="${ContentList.contentId}"       value="${ContentList.contentPath}" /> 
</br></br> 

我会修改内容路径并保存它们。现在他们应该使用表单bean。但是表单bean列表正在抛出空指针。

FormBean(ManageJobForm)就像,

private List<ManageJob> ContentList; // with setter getter

请建议我如何在 spring mvc 3中将这些fileds值输入formbean。


在数据库列表中填充的数据1在DAO层中发生。 DAO注入服务层,最后服务注入控制器。请通过DAO,服务和控制器填写List。

JobDAOImpl.java,

    public List<Content> getJobContentList(String source, String storeType, String  contentType, int langId){

    List<Content> contentList;

    String contentListSql = "SELECT CON.CONTENT_ID, CON.CONTENT_NAME, CON.CONTENT_PATH FROM CONTENT CON " +
        "WHERE CON.STORE = ? AND CON.LANG_ID = ? AND CON.INSTANCE_TYPE = ? AND CON.CONTENT_TYPE = ?";

    contentList = jdbcTemplate.query(contentListSql, 
             new Object[] { storeType, langId, source, contentType}, 
          new RowMapper(){ 

            public Content mapRow(ResultSet rs, int rowNum) throws SQLException { 
                Content job = new Content(); 
                job.setContentId(rs.getString(1)); 
                job.setContentName(rs.getString(2)); 
                job.setContentPath(rs.getString(3)); 
                return job; 
            } 
          });


    return contentList;
}

JobServiceImpl.java,

    @Autowired
    public JobDAO dao; 

    public List<Content> getJobContents(String jobName, String source, String contentType){

    String[] strArray = jobName.split(" ");
    String storeType = strArray[0];

    int langId = 0;

    List<Content> dataList;

    if(storeType != null && storeType.equalsIgnoreCase("US")){
        langId = -1;

        dataList = dao.getJobContentList(source, storeType, contentType, langId);

    }else if (storeType != null && storeType.equalsIgnoreCase("CA")){
        langId = 1;

        dataList = dao.getJobContentList(source, storeType, contentType, langId);

    }

    return dataList;
}

ManageJobsController.java,

    @Autowired
public JobService jobService;

    List<List> creativeContentList = jobService.getJobContents(bean.getJobName(), instance, "Creative");

    bean.setCreativeContentList(creativeContentList.get(0));


    model.addAttribute("jobName", bean.getJobName());

现在我的CreativeContentList准备好了数据,我可以在jsp页面中迭代它以获取值。所以在ConfigContent.jsp中,

<form:form method="POST" action="ConfigureJobController.do" commandName="ManageJobForm" >
<div class="subTopic hide" id="tab2">
    <c:forEach items="${ManageJobForm.creativeContentList}" var="creativeContentList" >

        <label for="${creativeContentList.contentId}">${creativeContentList.contentName}</label>

        <input type="text" class="textboxStyle" name="${creativeContentList.contentId}"                                 value="${creativeContentList.contentPath}" /> 

        </br></br> 
    </c:forEach>        
</div>
<input type="Submit" class="buttonStyle" name="saveBtn" value="Save" />

现在我在内容路径中进行一些修改并尝试保存它。但是当我从控制器访问formbean的creativeContentList时,列表为空。检查JobConfigController.java,

@Controller
@RequestMapping(value="/ConfigureJobController.do")
public class JobConfigController {

@RequestMapping(params="saveBtn", method=RequestMethod.POST)
public String saveConfiguration(ModelMap model, @ModelAttribute(value="ManageJobForm") ManageJobForm                            bean, BindingResult result){

    List<Content> creativeSiteContent = bean. getCreativeContentList ();

    //Getting null list here
    if(null != creativeSiteContent && creativeSiteContent.size() > 0){
        for (Content cont : creativeSiteContent) {  

            System.out.println("Hiiiiiiiiiiiiiiiiii in config controller " + cont.getContentId());
        }
    }

    return "ConfigContent";

}

如何使用新输入值填充formbean中的CreativeContentList,以便我可以再次将它们保存在db中。有没有其他方法来处理这种情况?

请帮帮我。

0 个答案:

没有答案