【问题标题】:Pass model and a string using ajax to Spring MVC使用 ajax 将模型和字符串传递给 Spring MVC
【发布时间】:2018-11-02 10:41:24
【问题描述】:

您好,我需要使用 AJAX 将完整模型和一个字符串从 html 传递到 Spring 控制器。我使用下面的代码 sn-p 但它不起作用。

var str = $("#resourceManagement").serialize();
        var agreementId = ${agreementId};
        var tempCnltName=$modal.find("input[data-type='cnltName']").val();
        $.ajax({
            type:"POST",
            data: {str, tempCnltName}, 
            url: "${AGREEMENT_BASE_URL}/checkDuplicateConsultantOnline",
            async: false,
            dataType: "json",
            success: function (data, status, xhr) {

                message =  data.errorMsg;
            },
            error: function () {

            }
        });

问题是,如果我单独传递模型 (str) 或单独传递字符串 (tempCnltName),我可以在控制器中获取它,但我无法将两者放在一起。

我的控制器如下:

@RequestMapping(value = "/app/agreement/checkDuplicateConsultantOnline", method = RequestMethod.POST)
public @ResponseBody AjaxResponse checkDuplicateConsultantOnline(
        @ModelAttribute("consultantBidModel") ConsultantBidModel model,
        @RequestParam(value = "tempCnltName", required = false) String cnltName,
        HttpServletRequest request,
        HttpSession session) throws Exception {

    final Set<String> allCnltNames = new HashSet<>();
    String errMessage = "";
    if (model.getLeadingCnltName() != null) {
        allCnltNames.add(model.getLeadingCnltName().toLowerCase());
    }
    if (model.getJointVentureConsultants() != null) {
        for (ConsultantBidListItem entry : model.getJointVentureConsultants()) {
            if (!allCnltNames.add(entry.getCnltName().toLowerCase())) {
                errMessage = "Each consultant can only appear once.";
            }
        }
    }
    if (model.getSubConsultants() != null) {
        for (ConsultantBidListItem entry : model.getSubConsultants()) {
            if (!allCnltNames.add(entry.getCnltName().toLowerCase())) {
                errMessage = "Each consultant can only appear once.";
            }
        }
    }
    AjaxResponse response = new AjaxResponse();
    if (errMessage != null) {
        response.setSuccess(true);
        response.setResponseObject(errMessage);
        response.setErrorMsg(errMessage);
    }
    return response;
}

【问题讨论】:

    标签: java html ajax spring jsp


    【解决方案1】:

    在服务器端,您已经准备好接收模型(@ModelAttribute)和附加 URL 参数(@RequestParam

    在客户端上,将 URL 参数附加到 URL。假设 str 是您的模型,tempCnltName 是您要提交给服务器的字符串:

    $.ajax({
        type:"POST",
        data: str,
        url: "${AGREEMENT_BASE_URL}/checkDuplicateConsultantOnline?tempCnltName=" + tempCnltName,
    ...
    

    【讨论】:

      【解决方案2】:

      试试

      var strVal = $("#resourceManagement").serialize();
          var agreementId = ${agreementId};
          var tempCnltNameVal=$modal.find("input[data-type='cnltName']").val();
          $.ajax({
              type:"POST",
              data: {str: strVal, tempCnltName: tempCnltNameVal}, 
              url: "${AGREEMENT_BASE_URL}/checkDuplicateConsultantOnline",
              async: false,
              dataType: "json",
              success: function (data, status, xhr) {
      
                  message =  data.errorMsg;
              },
              error: function () {
      
              }
          });
      

      可能是格式错误的 json 造成了麻烦

      【讨论】:

      • 感谢帮助,但这不起作用。我刚刚更新了我的帖子以显示我的控制器代码。我认为 strVal 没有映射到我的模型。
      【解决方案3】:

      上述的另一种方式,将字符串添加到模型中:

      var strVal = "consulName=" + tempCnltName + "&";strVal = strVal + $("#resourceManagement").serialize();
      

      然后模型可以有一个新的参数consulName,我们可以在Controller中获取值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-26
        • 2015-01-12
        • 2016-07-18
        • 2011-12-16
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多