0

我试图在我的控制器中调用一个方法 onclick 一个按钮并将一个变量传递给同一个方法,我正在使用这样的 ajax

<c:url var="searchUrl" value="/servlet/mycontroller/searchmethod" />

$(document).ready(function()
     {
 $('#submit_btn').click(function(){
var dt = $('#search_data').val();
$.ajax({
    type: "POST",
    dataType : "json",
    url : "${searchUrl}/" + dt

});
});
});

 <td width="32%" align="right"><label>
  <input type="text" name="transaction_id" id="search_data" class="fld_txt" />
</label></td>
<td width="15%" align="right"><label>
  <input type="button" class="button_grey" name="submit" id="submit_btn" value="Search" class="button" />

我的控制器

@RequestMapping(value = "/searchUrl/{dt}", method = RequestMethod.GET)
public List<Dto> searchJobList(WebRequest request, @PathVariable String dt, Model model) throws Throwable {
        System.out.println("Retrieve Id >> "+dt);
        List<Dto> list = Service.getJobSearchList(dt);
        return list;
} 

像这样收到以下错误

http://localhost:8080/Sample/servlet/mycontroller/searchmethod/123(dt var value)    

如何在控制器中调用我的搜索方法并将文本框值传递给它?我需要根据这个 dt 显示列表吗?有什么帮助吗??

4

1 回答 1

1

你需要改变request mapping这种方式

 @RequestMapping(value = "/servlet/mycontroller/searchmethod/{dt}", method = RequestMethod.GET)

searchUrl是java脚本变量。在控制器端,您需要映射actual URL.

所以你的最终代码看起来像

@RequestMapping(value = "/servlet/mycontroller/searchmethod/{dt}", method = RequestMethod.GET)
public List<Dto> searchJobList(WebRequest request, @PathVariable String dt, Model model) throws Throwable {
        System.out.println("Retrieve Id >> "+dt);
        List<Dto> list = Service.getJobSearchList(dt);
        return list;
} 

如评论中所述,您将 web.xml 映射为

  <servlet-mapping>
        <servlet-name>Controller</servlet-name>
         <url-pattern>/servlet/*</url-pattern>
        </servlet-mapping>

所以你应该如下添加请求映射(注意/servlet将由web.xml

 @RequestMapping(value = "/mycontroller/searchmethod/{dt}", method = RequestMethod.GET)
于 2013-05-09T10:08:47.397 回答