2013-09-23 4 views
0

자동 완성 스프링 mvc 및 jquery/json에 대한 예제를 작성합니다. 나는 봄과 최대 절전 모드를 사용하여 json에서 객체 값을 얻지 만 검색하지 않을 때는 작동하지 않습니다. 데이터베이스에서 가져 왔지만 자동 완성은 값 드롭 다운을 표시하지 않습니다. 제발 도와주세요.이 문제는 저에게 도움이됩니다.스프링 MVC 자동 완료가 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 
    ... 
    
  • 내 서비스 (UserServiceImpl.java)

    @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; 
        } 
    } 
    
  • 내 컨트롤러 (UserController) :

    @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; 
        } 
    
  • 내 J I는 방화 및 JSON 응답하여 테스트 자동 완성을

    <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> 
    

SP (user.jsp)의 성공을

Object { id=1, name="john", country="london", more...} 
Object { id=2, name="johanson", country="london", more...} 

값과 자동 완성으로 표시 할 드롭하지만 값을 갖는다. UserRepositoryImpl.java 파일에서

답변

0

쿼리 queryList = sessionFactory.getCurrentSession(). createQuery ("사용자의 u.country 선택 U WHERE u.country LIKE '% 이하로 쿼리를 변경 ... 제발 도와주세요 "+ query +"% ' ");

관련 문제