我在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(nonymous function)@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:3582completed @ 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;
} }
你能帮忙吗?
答案 0 :(得分:0)
在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;
}