Jquery Spring MVC Autocomplete无法从json获取值

Jquery Spring MVC Autocomplete无法从json获取值,jquery,json,jsp,spring-mvc,autocomplete,Jquery,Json,Jsp,Spring Mvc,Autocomplete,我编写了一个关于autocomplete spring mvc和jquery/json的示例。我使用spring和hibernate从json获取对象值,但当我搜索它时,它不起作用。我已从数据库中获取,但自动完成不列出值下拉列表,请任何人帮助我解决此问题 我的用户 private Integer id; private String name; private String country; public User() { } public User(String name, String

我编写了一个关于autocomplete spring mvc和jquery/json的示例。我使用spring和hibernate从json获取对象值,但当我搜索它时,它不起作用。我已从数据库中获取,但自动完成不列出值下拉列表,请任何人帮助我解决此问题

  • 我的用户

    private Integer id;
    private String name;
    private String country;
    public User() {
    } 
    public User(String name, String country) {
    this.name;
    this.country;
    }
    // getter and setter
    ...
    
  • 我的服务(userserviceinpl.java)


    它有下拉列表,但值不显示在自动完成中。请帮助我…

    在UserRepositoryImpl.java文件中更改查询,如下所示

    Query queryList=sessionFactory.getCurrentSession().createQuery(“从用户u中选择u.country,其中u.country类似“%”+Query+“%”)

    @Repository
    public class UserRepositoryImpl implements UserRepository {
        @Autowired
        private SessionFactory sessionFactory;
    
        @SuppressWarnings("unchecked")
        public List<String> getCountryList(String query) {
    
            List<String> countries = new ArrayList<String>();
    
            Query queryList = sessionFactory.getCurrentSession().createQuery("FROM user u WHERE u.country LIKE '%"+query+"%'");
            countries = queryList.list();
            query = query.toLowerCase();
    
            return countries;
        }
    }
    
        @Controller
        public class UserController {
    
        @Autowired
        private UserService userService;
    
        @RequestMapping(value = "/index", method = RequestMethod.GET)
        public ModelAndView index() {
            User userForm = new User();
            return new ModelAndView("user", "userForm", userForm);
        }
    
        @RequestMapping(value = "/get_country_list", method = RequestMethod.GET, headers="Accept=*/*")
        public @ResponseBody List<String> getCountryList(@RequestParam("term") String query) {
            List<String> countryList = userService.getCountryList(query);   
            return countryList;
        }
    
    <body>
    <h2>Spring MVC Autocomplete with JQuery &amp; JSON example</h2>
    <form:form method="post" action="save.html" modelAttribute="userForm">
    <table>
    <tr>
        <th>Name</th>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <th>Country</th>
        <td><form:input path="country" id="country" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Save" />
            <input type="reset" value="Reset" />
        </td>
    </tr>
    </table>
    <br />
    
    </form:form>
    
    <script type="text/javascript">
         function split(val) {
            return val.split(/,\s*/);
         }
         function extractLast(term) {
            return split(term).pop();
         }
    
        $(document).ready(function) { 
            $( "#country" ).autocomplete({
                source: function (request, response) {
                $.getJSON("${pageContext. request. contextPath}/get_country_list.html", {
                    term: extractLast(request.term)
                }, response);
            },
            search: function () {
                // custom minLength
                var term = extractLast(this.value);
                if (term.length < 1) {
                    return false;
                }
            },
            focus: function () {
            // prevent value inserted on focus
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(", ");
                return false;
            }
            });
      });
    </script>
    
    </body>
    
    Object { id=1, name="john", country="london", more...}
    Object { id=2, name="johanson", country="london", more...}