![](/img/trans.png)
[英]How to pass data from Spring controller to JQuery $.post() call
[英]How to call Spring controller function from JQuery Data tables AJAX
我在controller.xml中定義了一個控制器
<bean name="/userController" class="UserController">
我有UserData.jsp,它呈現了如下所示的動態表。
<table id="usermaintenancetable">
<thead>
<tr>
<th width="10%">User Id</th>
<th width="5%">Password</th>
<th width="5%">Level</th>
</tr>
</thead>
</table>
在datatable.js文件中,我必須訪問控制器以使用Ajax獲取數據。
$(document).ready (function() {
$('#usermaintenancetable').DataTable( {
//How to call controller from here using ajax call
});
});
我已經編寫了如下的ajax函數。
"ajax" : {
"url" : "/userController",
"dataSrc" : "users",
"type" : "POST"
}
但是我越來越低於ajax錯誤。
POST http:// localhost:7001 / userList 404(未找到)發送@ jquery.js:10261ajax @ jquery.js:9750ra @ jquery.dataTables.js:781ga @ jquery.dataTables.js:1065(匿名函數)@ jquery .dataTables.js:2016each @ jquery.js:370each @ jquery.js:137m @ jquery.dataTables.js:1831h.fn.DataTable @ jquery.dataTables.js:3853(匿名函數)@ datatable.js:3fire @ jquery .js:3232fireWith @ jquery.js:3362ready @ jquery.js:3582完成@ jquery.js:3617
編輯:添加了控制器代碼
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class UserController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req,
HttpServletResponse resp) throws Exception {
String action = ServletRequestUtils.getStringParameter(req, "action", "view");
System.out.println("Action: " + action);
if("list".equalsIgnoreCase(action)) {
//added some logic to get data and set it to table
}
return null;
} }
您能幫上忙嗎?
在ajax調用中指定
contentType: 'application/json' and dataType: json
您可以如下所示重寫控制器
@RequestMapping(value="userController", method=RequestMethod.GET
produces="application/json")
public @ResponseBody String userFunciton(HttpServletRequest req,
HttpServletResponse resp)) {
String action = ServletRequestUtils.getStringParameter(req, "action", "view");
System.out.println("Action: " + action);
if("list".equalsIgnoreCase(action)) {
//added some logic to get data and set it to table
}
return null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.