根据另一个下拉选择Spring MVC AJAX填充下拉列表

时间:2017-07-02 14:03:26

标签: javascript java ajax jsp spring-mvc

我有2个下拉列表:一个用于类别,一个用于子类别。根据我在第一个下拉列表中选择的类别,我希望第二个下拉列表能够动态填充所选类别的子类别。这就是我到目前为止所做的:

控制器填充第一个下拉列表:

@RequestMapping(value = "/post_project", method = RequestMethod.GET)
    public String postProjectPage(Model model) {
        Project project = new Project();
        List<Category> categoriesList = categoryService.getAllCategories();
        model.addAttribute("projectForm", project);
        model.addAttribute("categoriesList", categoriesList);
        return "post_project";
    }

JSP:

<form:form id="post_project" action="/post_project" method="post" modelAttribute="projectForm"> 
    <form:select class="form-control" id="selectCategory" path="category">
         <option value="">-Select-</option>
         <form:options items="${categoriesList}" itemValue="id" itemLabel="category_name"/>
    </form:select>

对于子类别,我有以下控制器:

@RequestMapping(value = "/post_project", method = RequestMethod.POST)
    public @ResponseBody  List<Subcategory> getAllSubcategories(@RequestParam(value="categoryId") int categoryId) {
        return categoryService.getAllSubcategories(categoryId);
    }

JSP:

  <form:select class="form-control" id="selectSubcat" path="subcategory">
       <option value="-1" label="-Select-"/>
    </form:select>

为了填充第二个下拉列表,我使用了AJAX,但我对此非常新,而且我不知道它是否正确。

("#selectCategory").change(function(){
        var categoryId = $(this).val();
        $.ajax({
            type: 'POST',
            url: "/post_project",
            data: {"categoryId" : categoryId},
            success: function(data){
                var slctSubcat=$('#selectSubcat'), option="";
                slctSubcat.empty();

                for(var i=0; i<data.length; i++){
                    option = option + "<option value='"+data[i].id + "'>"+data[i].subcateogory_name + "</option>";
                }
                slctSubcat.append(option);
            },
            error:function(){
                alert("error");
            }

        });
    });

当然它不起作用。当我选择一个cateogry时,第二个下拉列表中没有显示任何内容。我不知道该怎么做了,我已经尝试了一切。有人可以告诉我我做错了吗?

2 个答案:

答案 0 :(得分:1)

请求GET:

("#selectCategory").change(function(){
    var categoryId = $(this).val();
    $.ajax({
        type: 'GET',
        url: "/categories/" + categoryId,
        success: function(data){
            var slctSubcat=$('#selectSubcat'), option="";
            slctSubcat.empty();

            for(var i=0; i<data.length; i++){
                option = option + "<option value='"+data[i].id + "'>"+data[i].subcateogory_name + "</option>";
            }
            slctSubcat.append(option);
        },
        error:function(){
            alert("error");
        }

    });
});

服务器端控制器方法:

@RequestParam用于获取查询参数。所以,它就像..../post_project?categoryId=1

而不是@RequestParam使用@PathVariable,如下所示:

因此,要获得子类别,您@RequestMapping就像.../categories/1

一样
@RequestMapping(value = "/categories/{categoryId}", method = RequestMethod.GET)
public @ResponseBody  List<Subcategory> getAllSubcategories(@PathVariable("categoryId") int categoryId) {
    return categoryService.getAllSubcategories(categoryId);
}

答案 1 :(得分:0)

尝试添加contentType&amp;您的Ajax调用的dataType:

` ....
  $.ajax({
        type: 'POST',
        url: "/post_project",
        data: {"categoryId" : categoryId},
        contentType:"application/json; charset=utf-8"
        dataType:"json",
........` 

并将您的控制器更改为发布Json请求:

@RequestMapping(value = "/post_project", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)