根据Spring MVC和JQuery中的另一个下拉列表填充下拉列表

时间:2017-11-16 19:58:04

标签: javascript java jquery spring spring-mvc

我使用spring mcv成功填充了第一个下拉列表,但填充了第二个下拉列表我认为sprig和jquery之间存在一些不一致,遗漏了一些东西。

在根据第一个下拉列表更改jquery函数填充第二个下拉列表时,我收到错误:

java.lang.IllegalArgumentException:找不到类型为:class java.util.ArrayList

的返回值的转换器

注意:TaskDetail类包含所有getter和setter。

任何帮助都将不胜感激,谢谢。

编辑:代码没有问题,但我忘了将杰克森添加到项目中。

这是我的代码:

DAO课程:

@SuppressWarnings("unchecked")
@Override
public HashMap<Integer, String> findAllTasksForDropdown() {
    HashMap<Integer, String> tasks = new HashMap<Integer, String>();

    Criteria criteria = createEntityCriteria()
            .addOrder(Order.asc("taskId"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);//To avoid duplicates.
    List<Task> list = criteria.list();
    Iterator<Task> itr = list.iterator();
    while(itr.hasNext())
    {
        Task task = itr.next();
        tasks.put(task.getTaskId(), task.getTaskName());
    } 

    return tasks;
}

@SuppressWarnings("unchecked")
@Override
public List<TaskDetail> findAllTaskDetailForDropdown(Integer taskId) {
    Criteria criteria = createEntityCriteria()
            .add(Restrictions.eqOrIsNull("taskId", taskId))
            .addOrder(Order.asc("taskDetailId"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);//To avoid duplicates.
    List<TaskDetail> taskDetails = criteria.list();

    return taskDetails;
}

控制器:

@RequestMapping(value="/taskDetailListDD/{taskId}", method = RequestMethod.GET)
public @ResponseBody List<TaskDetail> initTaskDetailList(@PathVariable("taskId") int taskId){
    List<TaskDetail> taskDetailList = taskDetailService.findAllTaskDetailForDropdown(taskId);
    return taskDetailList;
}

@RequestMapping(value="/taskListDD", method = RequestMethod.GET)
public void initTaskList(ModelMap model){
    HashMap<Integer, String> taskList = taskService.findAllTasksForDropdown();
    model.addAttribute("taskListDD", taskList);
}

JSP:

<div class="well lead">Task Info Registration Form</div>
    <form:form method="POST" modelAttribute="taskInfo" class="form-horizontal">

        <div class="row">
            <div class="form-group col-md-12">
                <label class="col-md-3 control-lable" for="taskId">Task Id</label>
                <div class="col-md-7">

                    <form:select id="selectTaskListDD" path='taskListDD'> 
                        <option value="">-Select-</option> 
                        <form:options items="${taskListDD}"/>
                    </form:select>

                    <div class="has-error">
                        <form:errors path="taskId" class="help-inline"/>
                    </div>
                </div>
            </div>
        </div>

        <script>
            $("#selectTaskListDD").change(function(){
                var taskId = $("#selectTaskListDD").val();
                $.ajax({ //ERROR IS THROWN HERE
                    type: 'GET',
                    url: "/taskDetailListDD/" + taskId,
                    success: function(data){
                        var slctTaskDetailDD=$('#selectTaskDetailDD'), option="";
                        slctTaskDetailDD.empty();

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

                });
            });
        </script>

        <div class="row">
            <div class="form-group col-md-12">
                <label class="col-md-3 control-lable" for="taskDetailId">Task Detail Id</label>
                <div class="col-md-7">

                    <form:select class="form-control" id="selectTaskDetailDD" path="taskDetailListDD">
                       <option value="-" label="-Select-"/>
                    </form:select>

                    <div class="has-error">
                        <form:errors path="taskDetailId" class="help-inline"/>
                    </div>
                </div>
            </div>
        </div></form:form>

0 个答案:

没有答案