0

我正在尝试使用 JQuery 和 Spring 2.5.6 显示自动完成列表,但我正在将 json 发送到前端,但我无法显示它。

 $(function() {
    $("#globalSearch").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "${pageContext.request.contextPath}/autoSearch.htm",
                data: {
                    term : request.term
                },
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    // this is the alert output its displaying:{"list":["ernst","ernst&nbsp"]} 
                    alert(JSON.stringify(data, null, 4));
                    response($.map(data, function(item) {
                        //its not alerting anything here
                        alert(JSON.stringify(item, null, 4));
                        return{
                            value: item.list
                        };
                    }));
                }
            });
        }
    });
});

这是我的弹簧控制器代码:

@RequestMapping(method = RequestMethod.GET, value = "/autoSearch.htm")
public ModelAndView autoSearch(
        @RequestParam(value = "term", required = false) String term
       ) throws ParseException, IOException {

    if (logger.isDebugEnabled()) {
        logger.debug("inventoryHandler called");
    }

    Map<String, Object> model = new HashMap<String, Object>();
    int i = 0;
    model.put("list", getBaseModel().getSearchServiceBean().autoCompleter(term));
    return new ModelAndView("jsonView", model);
}

谁能告诉我如何显示自动完成列表。

提前致谢,

最好的问候,拉贾。

4

3 回答 3

0

在 spring 3.x 之后,您将使用 @ResponseBody 注释,例如:

public @ResponseBody List<String> autocomplete(@RequestParam(value = "term", required = false) String term) {
    //Go get your results for the autocomplete term
    List<String> termResults = ...;

    return termResults;
}

在我看来,您的响应以错误的内容类型发送回客户端,并被解释为 text/html 而不是 JSON。您可以设置响应内容类型并从控制器中写入 JSON 字符串吗?

String json = "{\"list\":[\"ernst\",\"ernst&nbsp\"]}";
response.setContentType("application/json");
PrintWriter writer = response.getWriter();
response.setContentLength(json.length());
writer.write(json);
于 2012-07-31T09:26:27.740 回答
0

最后我找到了下面显示的解决方案。

 $(document).ready(function() {
    $("#globalSearch").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "${pageContext.request.contextPath}/autoSearch.htm",
                data: {
                    globalSearch : request.term
                },
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    response($.map(data.list, function(value) {
                        return{
                            label: value,
                            value: value
                        };
                    }));
                }
            });
        }
    });
});

在输入字段中,名称和 ID 必须相同

 <input type="text" name="globalSearch" id="globalSearch"/>
于 2012-07-31T09:35:23.383 回答
0

@RequestMapping(method = RequestMethod.GET, value = "/autoSearch.htm") public ModelAndView autoSearch(@RequestParam(value = "term", required = false) String term) 抛出 ParseException, IOException {

    if (logger.isDebugEnabled()) {
        logger.debug("inventoryHandler called");
    }

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("list", getBaseModel().getSearchServiceBean().searchAutoComplete(term));
    return new ModelAndView("jsonView", model);
}

并且在 views.properties 文件中的视图应该设置为

jsonView.(class)=org.springframework.web.servlet.view.json.JsonView

并且在 javascript 中,自动完成将通过使用以下代码来工作:

        $("#searchEmail").autocomplete({
            source: function(request, response) {
                ajaxCall(request, response);
            },
            select: function(event, ui) {
                $("#searchEmail").val(ui.item.value);
                $("#emailSearch-form").submit();
            }
        });

        function ajaxCall(request, response) {
            $.ajax({
                url: "${pageContext.request.contextPath}/autoSearch.htm",
                data: {
                    term : request.term
                },
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    response($.map(data.list, function(item) {
                        return{
                            label: item,
                            value: item
                        };
                    }));
                }
            });
        }
于 2013-01-25T10:26:32.527 回答