0

这是我的jsp片段

<form:form>    
<form:input path = "state"  id = "state"/>
</form:form>
<script>
$(document).ready(function() { 
    $("#state").autocomplete({         
        source: '${pageContext. request. contextPath}/getStatelist.htm'    
        }); 
});
</script>

这是应该返回状态列表的控制器代码

@RequestMapping(value = "/getStatelist.htm", method = RequestMethod.GET, headers = "Accept=*/*")
    public @ResponseBody List<String> getStateList(@RequestParam("term") String query) {

          ApplicationContext context = 
                     new ClassPathXmlApplicationContext("Beans.xml");

          MasterJDBCTemplate dao = 
                  (MasterJDBCTemplate)context.getBean("masterJDBCTemplate");
           System.out.println(query);
           System.out.println("Controller called");

           List<String> fans = dao.getStateList(query);

           return fans;
    }

运行代码并输入文本字段状态后,控制器被调用,我在控制台上打印了正确的结果。示例运行如下所示。

g
Controller called
select state_desc from mst_state where state_desc like 'G%'
[GUJRAT, GOA]
gu
Controller called
select state_desc from mst_state where state_desc like 'GU%'
[GUJRAT]
guj
Controller called
select state_desc from mst_state where state_desc like 'GUJ%'
[GUJRAT]

但是我在前端看不到任何东西。我错过了什么?可能是什么原因 ??

4

1 回答 1

0

根据 jQuery UI 文档,从 REST 服务返回的数据应该是 JSON。

参考:http ://api.jqueryui.com/autocomplete/#option-source

为了适应这种情况,不要从“ * / * ”(即headers="Accept= * / *")中指定 Accept Header 属性,而是指定“ application/json ”(即headers="Accept=application/json"

于 2013-10-09T10:50:41.427 回答