开发者

Getting null value submitting my form to a new window using java script

function openWindowWithGraph(selectedItems){
            var i=0;
            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", "http://localhost:8080/cmonitor/app/solarPlant/showChart");
            form.setAttribute("target", "formresult");

            alert(selectedItems[0].solarPanel.id);
            for(i=0;i<selectedItems.length;i++){
                var hiddenField = document.createElement("input");              
                hiddenField.setAttribute("name", "solarPanelIds["+i+"]");
                hiddenField.setAttribute("value", selectedItems[i].solarPanel.id);
                form.appendChild(hiddenField);
            }

            document.body.appendChild(form);
            window.open('','formresult','width=1000,height=1000,toolbar=no ');
            alert("hello");
          开发者_运维技巧  form.submit();
        }

here is my spring controller

@RequestMapping(value="/showChart",method=RequestMethod.POST)
    public ModelAndView showChart(@RequestParam(required=false) Long[] selectedItemIds){
        System.out.println(selectedItemIds);  //getting null in controller
        return new ModelAndView("noHeader/solarPlant/chart");
    }

any kind of help will be greatly appreciated Thanks in Advance


In my testing, you cannot use '[' or such in a form name. Although it sends it (URL-escaped) in the POST request, at least PHP wouldn't accept the escaped or unescaped form.

Changing this line:

hiddenField.setAttribute("name", "solarPanelIds["+i+"]");

to

hiddenField.setAttribute("name", "solarPanelIds"+i);

works (as long as you update the server side variable).

You might also wish to consider using the technique of submitting a form asynchronously via a hidden iframe, if you are not tied to having a popup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜