繁体   English   中英

如何从JQuery数据表AJAX调用Spring控制器函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM