2016-07-14 34 views
0

我在controller.xml定义的控制器如何调用春季控制器功能从JQuery的数据表AJAX

<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错误。

http://localhost:7001/userList POST 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: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

@Tharsan Sivakumar,我添加了控制器代码。 –

回答

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; 
}