在spring mvc中动态添加表单元素

时间:2013-11-26 10:31:04

标签: javascript jquery ajax spring jsp

我在ajaxically加载表单元素时遇到问题。

ResourcePipeline.jsp:

    <head>
        $(document).ready(function() {
            var resourcePosition = 0;
            $('#addResourceButton').click(function() {
                resourcePosition++;
         alert(resourcePosition);
                $.get("appendResourceView.page", { fieldId: resourcePosition},
                    function(data){
                        $('#submitRow').before(data);
                        alert(resourcePosition+ "after sending the url");
                });
            });
        });
    </head>

<body>
 <input type="button" id="addResourceButton" value="+" />
</body>

我的控制员:

@RequestMapping(method = RequestMethod.GET, value="/appendResourceView.page")
protected String appendResourceField(@RequestParam Integer fieldId, ModelMap model )
{   
    List<Resource> resources = new ArrayList<Resource>();
    Resource resource = new Resource();
    resources.add(resource);        
    Demand demand = new Demand();   
    demand.setResources(resources);

    Map map = new HashMap();        

    map.put("demand", demand);      



    model.addAttribute("resourceNumber", fieldId);
    model.addAttribute("map", map);



    return "resourceInsert.view";
}

jsp是一个通过AJAX调用

resourceInsert.jsp:

<c:set var="resourceForDemand" value="${map.demand}"></c:set>

 <form:input type="text" class="dater"  path="resourceForDemand.resources[${resourceNumber}].need_From_Date" required="true" />

当我尝试触发AJAX部分时,我得到“既不是BindingResult也不是bean名称'resourceForDemand'的普通目标对象'可用作请求属性”作为错误。需要帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

Spring正在寻找名为resourceForDemand但未找到的模型对象。您是否有理由在ModelMap内使用嵌套地图?您可能最好使用控制器方法的次要重构并直接在模型中公开Demand对象:

@RequestMapping(method = RequestMethod.GET, value="/appendResourceView.page")
protected String appendResourceField(@RequestParam Integer fieldId, ModelMap model )
{   
    List<Resource> resources = new ArrayList<Resource>();
    Resource resource = new Resource();
    resources.add(resource);        
    Demand demand = new Demand();   
    demand.setResources(resources);


    model.addAttribute("resourceNumber", fieldId);
    model.addAttribute("resourceForDemand", demand);



    return "resourceInsert.view";
}