我正在使用Spring和jquery进行自动完成搜索。 问题是当我开始输入时,当我输入用户名的第一个字母时,它没有显示任何结果。 在我的控制台中,我有这个错误:
Failed to load resource: the server responded with a status of 404 ()
$%7BpageContext.request.contextPath%7D/search?term=t
controller.java
@RequestMapping(value = "/search", method = RequestMethod.GET)
@ResponseBody
public List <User> findUserByUsername(HttpServletRequest request){
return userRepository.findUserByUsername(request.getParameter("term"));
}
UserRepository.java
@Repository
public interface UserRepository extends CrudRepository<User, Long>{
@Query("SELECT u.username FROM User u WHERE u.username LIKE CONCAT('%',:username,'%')")
public List<User> findUserByUsername(@Param("username") String username);
}
的index.html
<input type="text" class="form-control" id="search" placeholder="Search"/>
search.js
$(document).ready(function() {
$('#search').autocomplete({
source: '${pageContext.request.contextPath}/search'
});
});
答案 0 :(得分:0)
尝试更改source: '${pageContext.request.contextPath}/search'
source: '${pageContext.request.contextPath}' + '/search'