SpringMVC中使用List数据绑定,ajax怎么传输包含数组的Json数据?



 <tr id="table_zgzs_row">
        <td><input type="text" name="certsList[0].zgzsmc"
            id="certs[0].zgzsmc" style="width:100%;height:20px" /></td>
        <td><input type="text" name="certsList[0].zsbh00"
            id="certs[0].zsbh00" style="width:100%;height:20px" /></td>
        <td><input type="text" name="certsList[0].bfsj00"
            id="certs[0].bfsj00" style="width:100%;height:20px" /></td>
        <td><input type="text" name="certsList[0].bzjg00"
            id="certs[0].bzjg00" style="width:100%;height:20px" /></td>
    </tr>


 function saveExpert() {
    if (checkForm()) {
        if (confirm("请认真核对当前信息,一旦保存您将没有修改的权限!您是否确认要保存当前信息?")) {
            $.ajax({
                async : false,
                cache : false,
                type : "post",
                dataType : "json",
                dataType : "text",
                contentType : "application/json;charset=UTF-8",
                data : JSON.stringify($('#infoForm').serializeObject()),
                url : "insertExpert.htm",
                success : function(data) {
                    var d = $.parseJSON(data);
                    if (d.success) {
                        alert("注册信息成功!\n请用注册的用户登陆系统,上传代码证附件!");
                    } else {
                        alert("注册失败!");
                    }
                }
            });
        }
    }
}

java json javaweb Ajax

大灰狼 9 years, 3 months ago

用spring mvc3的注解@ResponseBody


 @ResponseBody
public List<String> xxxx() {
    ...
    List res = xxx
    return res;
}

加两个依赖


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

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.8.4</version>
        </dependency>

如果没配


 <mvc:annotation-driven />

还需配置


 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>

灼眼D逢坂大河 answered 9 years, 3 months ago

Your Answer