我很困惑为什么自动完成不起作用。下面是 .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上。文件
任何帮助是极大的赞赏。