java - 如何使用modelAttribute在ajax(jquery)中提交spring表单

我是 Spring MVC 的新手。 我有这样的表格,

<form:form action="/myaction.htm" method="post" modelAttribute="myForm" id="formid">和一个返回 json 的 Controller

public @ResponseBody ResultObject doPost(@ModelAttribute("myForm") MyForm myForm){ System.out.println("myform.input"); }

我可以使用 $("#formid").submit(); 提交此内容我的 modelAttribute 工作正常,从 UI 中获取值。

我的问题是,如何以 jquery ajax 方式提交此表单? 我试过了,

$.ajax({
type:"post",
url:"/myaction.htm",
async: false,
dataType: "json",
success: function(){
alert("success");
}

});

表单已提交,但modelAttribute值为null,如何在提交时包含modelAttribute对象(表单正在使用的对象)?

最佳答案

您需要发布数据。我通常这样做的方式是使用以下内容。

var str = $("#myForm").serialize();

$.ajax({
    type:"post",
    data:str,
    url:"/myaction.htm",
    async: false,
    dataType: "json",
    success: function(){
       alert("success");
    }
});

https://stackoverflow.com/questions/13957581/

相关文章:

java - 如何使用 Spring Data Pagination 在一页中获取所有结果

spring - 注释驱动配置spring时如何为bean设置init-method?

hibernate - java.lang.VerifyError : class net. sf.

java - 什么是依赖注入(inject)和 Spring 框架?

java - 返回一个列表,我已经有一个rowmapper实现

json - 406 Spring MVC Json,根据请求 "accept"headers No

java - 自定义 validator 消息 : Throwing exception in im

java - @PostConstruct 注解和spring生命周期

java - Spring 3.0 设置和获取 session 属性

spring - 如何在 Spring 中通过 XML 定义 MySql 数据源 bean