【问题标题】:Spring MVC ajax call a controller usingSpring MVC ajax 调用控制器使用
【发布时间】:2015-01-16 15:59:41
【问题描述】:
@RequestMapping(value = "downloadFIReport.do", method = RequestMethod.POST)
 public @ResponseBody String downloadFIBill(ModelMap model,@ModelAttribute("SpringWeb") FIReportSearchInput fiReportSearchInput)
{

//

}

以上是我的控制器代码。我想发送FIReportSearchInput fiReportSearchInput 作为输入而不使用<form:form/> 标记和使用jQuery 的Ajax 方法将其绑定到页面

$.ajax() 

如何做到这一点?

这里更新的是FIReportSearchInput的定义

public class FIReportSearchInput {
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date startDate;
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date endDate;

    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    public Date getEndDate() {
        return endDate;
    }
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }



}

【问题讨论】:

  • 这个 FIReportSearchInput 对象不是来自视图(JSP)吗??

标签: java jquery spring spring-mvc


【解决方案1】:

这是一个例子,你可以试试: 我想 FIReportSearchInput 有 id 和 name 属性。

$.ajax({
  url:"downloadFIReport.do",
  type:"post",
  dataType:"json",
  data:{id:$("#id").val(),name:$("#name").val()},
  success:function(){}
});

当spring mvc服务器捕捉到这个请求时,如果你的回答体是'FIReportSearchInput fiReportSearchInput'并且对象FIReportSearchInput有id和name属性,它会自动调用setter方法来构建一个新的FIReportSearchInput对象,命名为fiReportSearchInput。

【讨论】:

  • 两个字段都是日期类型。也可以处理吗?
  • 你最好将日期转换为字符串,因为javascript和java的格式可能不同。
猜你喜欢
  • 1970-01-01
  • 2018-02-24
  • 2015-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多