简体   繁体   English

如何使用ajax和json将对象列表从spring mvc传递到jquery?

[英]How to pass list of objects from spring mvc to jquery using ajax and json?

can anyone please suggest me, how to pass and display List of objects from SpringMVC to jsp page to jquery using AJAX, JQuery , JSON. 任何人都可以建议我,如何使用AJAX,JQuery和JSON从SpringMVC传递对象并显示对象列表到jsp页面到jquery。

Here is my controller: 这是我的控制器:

@RequestMapping(value="/ViewUserList", method=RequestMethod.GET)
public @ResponseBody List<WizardTO> retriveUserList(@RequestParam(value = "Role") String Role) {
        WizardDAO dao = new WizardDAO();
        List<WizardTO> userList= dao.getUserList(Role);
        return userList;
        /*String str="returned from controller";
        return str;*/   // its working
    }

And Here is my jquery code (inside jsp file for request and response handling ) 这是我的jQuery代码(在jsp文件中用于请求和响应处理)

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script >  
    function doajax(){
        alert("in doajax");
       // jQuery("#list").jqGrid({
        $.ajax({    
            url: '\ViewUserList',
            type: 'get',
            data: "Role="+ $("#MyRole").val(),
            dataType: "json",
           /*   success:function(list){
                alert("success" +list);
            } , */
             success: function(data, textStatus, jqXHR) {
                 alert("success " +data +textStatus+jqXHR);
                 console.log(data.error);
                },
         //   error : alert("Error"),
         error: function(jqXHR, textStatus, errorThrown) {
             alert("Error "+"jqXHR "+jqXHR+" textStatus "+textStatus+" errorThrown "+errorThrown);
             console.log(textStatus, errorThrown);
            }   
/*          colNames:['UserID','FirstName', 'LastName', 'Role'], 
            colModel:[ 
                       {name:'UserID',index:'UserID', width:55}, 
                       {name:'FirstName',index:'FirstName', width:100}, 
                       {name:'LastName',index:'LastName', width:100}, 
                       {name:'Role',index:'Role', width:100}, 
                      ], 
                // rowNum:10, 
                 //rowList:[10,20,30], 
                 pager: '#UserListDiv', 
                 sortname: 'id', 
                 viewrecords: true, 
                 sortorder: "desc", 
                 caption:"Releted User List", 
                 editurl:"" 
                }).navGrid("#UserListDiv",
                        {edit:false,add:false,del:false});  */
        });
    }
</script>

this doajax() method is called when i click on particular href link,i also tried to use jquery grid to display received object, but i am doing some mistakes, 当我单击特定的href链接时,会调用此doajax()方法,我也尝试使用jquery网格显示接收到的对象,但是我在做一些错误,

I am new in springmvc and jquery;i just started working with spring-mvc jquery and ajax before some days. 我是springmvc和jquery的新手;几天前我才刚开始使用spring-mvc jquery和ajax。

i want to print table in particular division, i tried to return string from controller to jquery and it worked but i dont know how to return list of array from controller to jquery. 我想打印表中的特定分区,我试图将字符串从控制器返回到jquery,并且它起作用了,但是我不知道如何从控制器到jquery返回数组列表。

Can anyone please help me to for that issue...?? 谁能帮我解决这个问题...? (and i don't want to load other JSP page, i want to load data on same page from where i am sending request, in short i want to do that with help of ajax,json and jquery). (而且我不想加载其他JSP页面,我想从发送请求的位置在同一页面上加载数据,总之,我想借助ajax,json和jquery做到这一点)。

There are two things which i noticed and i commented it though: 我注意到了两件事,但我发表了评论:

  1. your ajax's url : "\\ViewUserList" is not using correct slash \\ . 您的ajax url : "\\ViewUserList"未使用正确的斜杠\\
  2. you have not posted the controller's annotation @Controller 您尚未发布控制器的注释@Controller

so you can add these two and see if this helps: 因此您可以将这两个添加在一起,看看是否有帮助:

Controller: 控制器:

@Controller
@RequestMapping(value="/ViewUserList", method=RequestMethod.GET)

jquery ajax: jQuery Ajax:

change this: 改变这个:

url: '\ViewUserList',

to this: 对此:

url: '/ViewUserList',

Check to see if this sorts it out. 检查是否可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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