0

从我的 JS 文件中,我发布了这个 JSON 数据。

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

YUI代码:

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);

控制器代码:

@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.";
}

豆码:

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;
    }

}

该列表仍然是空的。bean 值也是空的。我在这里做错了什么?

4

1 回答 1

0

嗯...这行得通吗?

modelAttribute="contactForm"以类似的形式添加

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

或使用@RequestBody

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

顺便问一下,yui(我对js不熟悉)可以转myPayload[0].id=1&myPayload[0].name=Me&myPayload[0].pId=2&myPayload[0].pName=Dad成JSON吗?毕竟它不像普通的 JSON。

更新

@ModelAttribute解决方法:全部改成(如果list是MyBean中的字段名)并保留myPayload[x].foo=bar在form标签中。list[x].foo=barmodelAttribute="myPayload"

@RequestBody解决方案:

如果您使用,请添加杰克逊依赖@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>
于 2013-07-26T14:00:40.463 回答