简体   繁体   English

Spring MVC和Ajax

[英]Spring MVC and Ajax

I m trying to write spring mvc application with ajax and jquery and stuck at some point and not able to find solution since last 1 week..please help me out. 我试图写一个带有ajax和jquery的spring mvc应用程序并且卡在某个点上并且自从上一周以来无法找到解决方案。请帮助我。

i want send request for showing all my users in database using ajax,see how i done 我想发送请求使用ajax在数据库中显示我的所有用户,看看我是如何完成的

Show User List 显示用户列表

now javascript ajax request 现在javascript ajax请求

function doAjaxPost(id)
{ var submitId=id;
  if(submitId=="showUserList")
  {
        $.ajax({
                type: "POST",

                url: "showUserList.html",
                success: function(response){
                // we have the response

                    if(response!='')
                    {
                        //alert(response);
      //Not getting this                $('#listofUser').html(response);
                         $("#homeDiv").hide();
                     $("#userListDiv").show("5");
                    }                                                      
                },
                error: function(e){
                alert('Error: ' + e);
                }
                });
        }

    }

now see me controller 现在看我控制器

@RequestMapping(value="/showUserList",method = RequestMethod.POST)
    public @ResponseBody ModelMap showUserList()
    {
        System.out.println("Ajax Request For user List");
        List<UserDetails> userList=service.getAllUser();
        ModelMap model=new ModelMap();
        model.put("users", userList);
        return model;
    }

now see how im displaying the user list on jsp page 现在看看如何在jsp页面上显示用户列表


<div id="listofUser">
<c:if test="${!empty users}">

<table id="rounded-corner" summary="List Of System Users">

    <thead>
        <tr>      
            <th scope="col" class="rounded-select">Select</th>
            <th scope="col" class="rounded-id">ID</th>
            <th scope="col" class="rounded-fname">Name</th>

            <th scope="col" class="rounded-gender">Gender</th>
            <th scope="col" class="rounded-username">UserName</th>
            <!-- <th scope="col" class="rounded-password">Password</th> -->
            <th scope="col" class="rounded-usertype">Type</th>
            <th scope="col" class="rounded-userStatus">Status</th>
            <th scope="col" class="rounded-Email">Email</th>
            <th scope="col" class="rounded-address">Address</th>
            <th scope="col" class="rounded-phno">PhoneNo.</th>
        </tr>
    </thead>
     <form action="adminOperations.html" name="userListForm" onsubmit="return ConfirmOperation();"  method="post">   
    <tbody>

        <c:forEach items="${users}" var="users" varStatus="status">
        <tr>
            <td><input type="checkbox" value="${users.id}" id="select" name="select"/></td>
            <td><c:out value="${users.id}" /></td>
            <td><c:out value="${users.firstName}" />&nbsp;<c:out value="${users.lastName}" /></td>

            <td><c:out value="${users.gender}" /></td>
            <td><c:out value="${users.userName}" /></td>
            <%-- <td><c:out value="${users.userPassword}" /></td> --%>
            <td><c:out value="${users.userType}" /></td>
            <td><c:out value="${users.status}" /></td>
            <td><c:out value="${users.emailId}" /></td>
            <td><c:out value="${users.address}" /></td>

            <td><c:out value="${users.contactNo}" /></td>

        </tr>

    </c:forEach>
    </tbody>
    <tfoot>
        <tr>

            <td colspan="12" class="rounded-foot-center" align="center">

            <input type="submit" name="activate" id="activate" value="Activate" onClick="ButtonClicked(this);">&nbsp;&nbsp;
            <input type="submit" name="deactivate" id="deactivate" value="Deactivate" onClick="ButtonClicked(this);">&nbsp;&nbsp;
            <input type="submit" name="update" id="update" value="Update" onclick="ButtonClicked(this);">
            <input type="submit" name="delete" id="delete" value="Delete" onclick="ButtonClicked(this);">&nbsp;&nbsp;&nbsp;

            </td>
        </tr>
    </tfoot>
    </form> 
</table>

<br/>
</c:if>



<c:if test="${empty users}">

    There are currently no user found.

</c:if>

</div>

....................................................................... what im not getting is , when response come from controller(ModelMap object in my case) how would i pass model containing list of users to my jsp page so that it can display. .................................................. .....................我得到的是什么,当响应来自控制器(我的情况下是ModelMap对象)时,我如何将包含用户列表的模型传递给我jsp页面使它可以显示。 i tried 我试过了
$('#listofUser').html(response); $( '#listofUser')HTML(响应); but it is not working, i want model containing (model.put("users", userList);) users to be available from javascript to my display part.. please help me out..... thanks and regards, Hemant Singh 但它不起作用,我希望模型包含(model.put(“users”,userList);) 用户可以从javascript到我的显示部分..请帮帮我.....谢谢和问候,Hemant Singh

i am using spring mvc 我正在使用spring mvc

 public ModelAndView showUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
     return new ModelAndView("GiveAnyName","users", userList);
    }

Now you can get User List in JSP Page  

    List rows = (List) request.getAttribute("users");

@ResponseBody doesn't work like that, you should return a JSON and in order to display the content you can do it with jQuery but is a bit tricky, that's why I recommend to you AngularJS, forget JSTL. @ResponseBody不能那样工作,你应该返回一个JSON,为了显示你可以用jQuery做的内容,但是有点棘手,这就是为什么我向你推荐AngularJS,忘了JSTL。

Here there is a tutorial where SpringMVC, JSTL and asynchronous requests work together 这里有一个SpringMVC,JSTL和异步请求协同工作的教程

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

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