0

From my JS file I am posting this JSON data.

myPayload[0].id=1&myPayload[0].name=Me&myPayload[0].pId=2&myPayload[0].pName=Dad

YUI Code:

var formElements = YAHOO.util.Connect.setForm("myFormId");
alert("New form elements:" + formElements); // Alerting POST data as shown above

YAHOO.util.Connect.resetFormState();

YAHOO.util.Connect.asyncRequest("POST", "/mycontroller/save", {
    cache : false,
    success : function(res) {
        alert(res.responseText);
    },
    failure : function(res) {
        alert(res.responseText);
    }
}, formElements);

Controller Code:

@RequestMapping(value = "/save", method=RequestMethod.POST)
@ResponseBody
public String saveData(ModelMap mm, @ModelAttribute("myPayload") MyBean bean,  BindingResult errors) {

    log.info("save:Called");
    List<MyDTO> lst = bean.getList();
    log.info("save:Number of records:" + lst.size());

    return "Successfully Updated.";
}

Bean Code:

public class MyBean {

    private List<MyDTO> myPayload = new  AutoPopulatingList<MyDTO>(MyDTO.class);

    public List<MyDTO> getList() {
        return myPayload;
    }

    public void setList(List<MyDTO> mList) {
        this.myPayload = mList;
    }

}

The list is still empty. The bean values are also empty. What am I doing wrong here?

Sujoy
  • 802
  • 11
  • 22
  • Did you specify your command name as "myPayload" in your form? – Yugang Zhou Jul 26 '13 at 04:57
  • Yes, in my formatter while creating elements I am giving the ids in that way. eg. id : "myPayload[" + index + "]." + oColumn.getKey(). I am getting the payload from the setForm API call in the JS file. I have mentioned that in my question. I am getting all the id:value map correctly in the JS before sending the request. But in the controller am not getting those. The payload is lost in the transit. – Sujoy Jul 26 '13 at 13:37

1 Answers1

0

Hmm... Does this work?

Adding modelAttribute="contactForm" in the form like

< form:form method="post" action="your url" modelAttribute="myPayload" >

Or use @RequestBody:

@RequestMapping(value = "/save", method=RequestMethod.POST)
@ResponseBody
public String saveData(ModelMap mm, @RequestBody MyBean bean,  BindingResult errors) {

By the way, could yui (I'm not familiar with js) turn myPayload[0].id=1&myPayload[0].name=Me&myPayload[0].pId=2&myPayload[0].pName=Dad to JSON? It's not like common JSON after all.

UPDATE

@ModelAttribute solution: Change all myPayload[x].foo=bar as list[x].foo=bar (if list is the field name in MyBean) and keep modelAttribute="myPayload" in the form tag.

@RequestBody solution:

Add jackson dependence if you use @RequestBody

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.12</version>
</dependency>
Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
  • 1. modelAttribute didn't work 2. @RequestBody is throwing some weird jackson baseclass not found exception. – Sujoy Jul 26 '13 at 18:51
  • @Sujoy does the "myPayload[0].id=1" string equals to < input name="myPayload[0].id" value="1"/> in YUI? I'm not familiar with this framework? and see the answer update for RequestBody issue. – Yugang Zhou Jul 27 '13 at 00:07
  • Yes it's exactly the same. For RequestBody issue, that jackson jar is already there in the classpath, it's working for other controllers of the same project. Only for this controller it's not. It's very weird. – Sujoy Jul 27 '13 at 17:56
  • Could you post RequestBody error stack and check my ModelAttribute sulution update? – Yugang Zhou Jul 28 '13 at 00:36
  • I am getting all my data in HttpRequest. "@RequestBody" won't work because I am using a list and in Spring MVC it has no way to convert to an AutoPopulatingList other than "@ModelAttribute" with a bean having AutoPopulatingList. – Sujoy Jul 29 '13 at 15:11
  • Still could not fix this issue. It's very strange. Doesn't look like a rocket science, but not working. – Sujoy Aug 11 '13 at 22:26
  • @user:400545 Can you please answer? Could you please look into this? I am getting the values in HttpServletRequest and not getting any other errors? – Sujoy Aug 15 '13 at 03:29
  • @Sujoy Really sorry for this, but I have no further solution other than the bold @ ModelAttribute solution in my update. I still speculate the data you post does not match MyBean's structure. – Yugang Zhou Aug 15 '13 at 04:04