我试图在我的控制器中调用一个按钮,然后将一个变量传递给同一个方法,用这样的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" />
myController的
@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显示列表吗?任何帮助?
答案 0 :(得分: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)