2016-07-14 54 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; 
} 
相關問題