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?

2
  • Did you specify your command name as "myPayload" in your form? Commented Jul 26, 2013 at 4: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. Commented Jul 26, 2013 at 13:37

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

8 Comments

1. modelAttribute didn't work 2. @RequestBody is throwing some weird jackson baseclass not found exception.
@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.
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.
Could you post RequestBody error stack and check my ModelAttribute sulution update?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.