如何在spring mvc

时间:2016-12-23 07:02:28

标签: javascript java json ajax spring-mvc

这里第一个ajax通过点击url 'getMergeRuleMaxCount'来成功击中我的控制器但是当我尝试点击这个url时'getMergingDatasets' 所以我的控制到达控制器,我可以通过控制器中的ajax发送数据,首先我无法进入控制器,它给了我一些例外情况,如下所述。 根据我的理解,在将多个数据发送到我的控制器时会出现问题。

  

我没有弄到我做错了什么,我应该如何在我的控制器中获取数据?

任何人都能让我知道可行的解决方案吗?

异常

WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.hcl.ae.model.MergeRule com.hcl.ae.controller.MergeRuleController.getMergingDatasets(javax.servlet.http.HttpServletRequest,com.hcl.ae.model.MergeRule) throws java.io.IOException

Merge.js

function mergingDataSetValidation(){
            var company = $("#company").val();
            if (company=="") {
                alert("Please select company");
            }
            var companyClass = $("#companyClass").val();
            var companyName = jQuery("#company").find("option[value='" + jQuery("#company").val() + "']").text();
            var className = jQuery("#companyClass").find("option[value='" + jQuery("#companyClass").val() + "']").text();
            $
                    .ajax({
                        type : "GET",
                        url : "getMergeRuleMaxCount",
                        success : function(data) {
                            var count = data;
                            var  selectedValue = "Merge_" + companyName + "_" + className + "_Rule_" + count;
                            $("#mergeRuleName").val(selectedValue);

                                    var parameter = {
                                            "companyId": company,
                                            "classId" : companyClass 
                                        };

                                    $
                                    .ajax({
                                        type : "GET",
                                        url : "getMergingDatasets",
                                        dataType: "json",
                                        contentType: "application/json; charset=utf-8",
                                        data : JSON.stringify(parameter),
                                        /*data : 'company=' + company  + '&class=' + companyClass,*/
                                        success : function(data) {
                                            alert("success");
                                            return;
                                        },
                                        error: function(error) {
                                            alert("Error occured while fetching merging datasets..!!")
                                        }   
                                    });

                        },
                        error: function(error) {
                            alert("Something went wrong..!!, please refresh the page and try again.")
                        }
                    });
        };

控制器

    @JsonView(Views.Public.class)
    @RequestMapping(value = "/getMergingDatasets",method = RequestMethod.GET)
    public @ResponseBody MergeRule getMergingDatasets(HttpServletRequest req, @RequestBody MergeRule parameter) throws IOException {

        System.out.println(parameter.getCompanyId());
        System.out.println(parameter.getClassId());

/*      System.out.println(req.getParameter("company"));
        System.out.println(req.getParameter("companyClass"));*/

        List<DropdownMenu> mergingDatasets = mergeRuleDAO.getMergingDataSets(parameter.getCompanyId(), parameter.getClassId());

        return (MergeRule) mergingDatasets;
    }

1 个答案:

答案 0 :(得分:0)

你的MergeRule必须容纳所有Ajax的参数。但是你的控制器使用get,所以你不能使用@RequestBody,你需要两个@RequestParam。 如:

@RequestMapping(value = "/getMergingDatasets",method = RequestMethod.GET)
    public @ResponseBody MergeRule getMergingDatasets(HttpServletRequest req, @RequestParam String companyId, @RequestParam String classId) throws IOException {
    System.out.println(companyId);
    System.out.println(classId);

    /*System.out.println(req.getParameter("company"));
     System.out.println(req.getParameter("companyClass"));*/

    List<DropdownMenu> mergingDatasets = mergeRuleDAO.getMergingDataSets(companyId, classId);

    return (MergeRule) mergingDatasets;
}