为什么自动完成不适用于JSON和SPRING MVC

时间:2012-09-09 13:50:47

标签: java jquery json spring

我很困惑为什么自动完成功能不起作用。以下是.jsp代码中的表单:

<form:form method="post" action="save.html" modelAttribute="word">
    <table>
        <tr>
            <th>German word</th>
            <td><form:input path="german" id="german" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Save" /></td>
        </tr>
    </table>
    <br />
</form:form>

这里是javascript函数(在相同的.jsp文件中)

$(document).ready(function() { 
$( "#german" ).autocomplete({
    source: '${pageContext. request. contextPath}/get_word_list.html'
});    

});

这是控制器的相关部分:

@Autowired
private WordService wordService;

@RequestMapping(value = "/goToDictionary", method = RequestMethod.GET)
public ModelAndView index() {

    Word word = new Word();
    return new ModelAndView("dictionary", "word", word);
}

@RequestMapping(value = "/get_word_list", method = RequestMethod.GET, headers = "Accept=*/*")
public @ResponseBody
List<String> getCountryList(@RequestParam("term") String query) {
    System.out.println(query);
    return getMatch(query);
}

public List<String> getMatch(String query) {
    query = query.toLowerCase();
    List<String> matched = new ArrayList<String>();
    for (Word v : wordService.getAllWord()) {
        if (v.getGerman().toLowerCase().startsWith(query)) {
            matched.add(v.getGerman());
        }
    }
    return matched;
}

我确信getMatch(String查询)被调用并正常工作。 所以我猜问题出现在jsp上。文件

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

[为JSON填写清单] 也许您应该查看produces注释的@RequestMapping属性。它需要String []作为值。自Spring 3.1.x版本开始提供。我目前使用3.1.2,我可以毫无问题地得到一些application/json

当然,您应该在国家/地区列表的数据提供者中添加产品。

[用于填写列表的JavaScript] 也许是部分$ {pageContext。请求。 contextPath}未正确计算。您是否检查了JSP的生成代码,例如与Firebug。

答案 1 :(得分:0)

好的,我找到了解决方案。它位于spring-servlet.xml文件中。它没有用,因为我没有添加它。

xsi:schemaLocation="ttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

我补充说,现在一切正常