【问题标题】:Spring MVC,Jquery Autocomplete not workingSpring MVC,Jquery自动完成不起作用
【发布时间】:2012-08-24 18:49:33
【问题描述】:

我正在尝试使用 Spring MVC 和 Jquery 实现自动完成功能。员工列表来自 DB,但来自 jquery;控制器的功能没有被调用。 请看下面的代码。

empDetails.jsp

 `link rel="stylesheet" type="text/css" ` `href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
 `

 `<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 `

 `<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
  `

 `<script type="text/javascript">
 `

$(document).ready(function() {


$(document).ready(function() {

$("#empName").autocomplete({

source: '${pageContext.request.contextPath}/getEmpList'

});

});

</script>



<form:input path="empName" cssClass="input-xlarge" id="empName" />

控制器类

@RequestMapping(value = "/getEmpList", method = RequestMethod.GET, headers = "Accept=*/*")

public @ResponseBody List<String> getEmpNameList(@RequestParam("term") String query) {

List<String> empList = empDetailsService.getEmpNames(query);

return empList;

}

请帮忙。

谢谢

【问题讨论】:

  • 您的 Controller CLASS 中有 RequestMapping 注解吗?

标签: jquery spring model-view-controller autocomplete annotations


【解决方案1】:

我想你忘记了自动完成的 url 中的控制器映射

我希望你的控制器类看起来像这样

 @Controller
 @RequestMapping("/myController")
 public EmpController {
    ...
    @RequestMapping(value = "/getEmpList"  method = RequestMethod.GET, headers = "Accept=*/*")
    public @ResponseBody List<String> getEmpNameList(@RequestParam("term") String query) {
        List<String> empList = empDetailsService.getEmpNames(query);
        return empList;
   }
 }

那么你必须使用这个网址myController/getEmpList${pageContext.request.contextPath}/myController//getEmpList

如果这不能解决问题,则使用 firebug 之类的工具查看客户端发送的内容和服务器返回的内容。

【讨论】:

  • 试图在控制器上添加RequestMapping,但没有用。当我在文本框中输入任何字母时,它根本没有击中控制器;我怎样才能看到请求和响应。我从网上的一些例子中参考了这段代码,这个例子正在工作,但不知道为什么它不是。请帮忙。谢谢
  • 查看请求的工具是 firebug(firefox 插件)。它有网络标签,显示流量 (getfirebug.com/network)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2015-06-08
  • 2014-04-28
  • 2011-01-05
相关资源
最近更新 更多