繁体   English   中英

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

[英]why autocomplete does not work with JSON and SPRING MVC

我很困惑为什么自动完成功能不起作用。 这是下面的.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 query)被调用并正常工作。 所以我想问题出在jsp上。 文件

任何帮助是极大的赞赏。

[使用JSON填充列表]也许您应该查看@RequestMapping批注的produces属性。 它使用String []作为值。 从Spring 3.1.x版本开始可用。 我目前使用3.1.2,可以毫无问题地获得一些application/json

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

[使用JavaScript填充列表]可能是$ {pageContext。 请求。 contextPath}评估不正确。 您是否检查了JSP的生成代码,例如使用Firebug。

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

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

我添加了,现在一切正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM