【问题标题】:Spring MVC and AjaxSpring MVC 和 Ajax
【发布时间】:2012-10-12 07:49:36
【问题描述】:

我正在尝试使用 ajax 和 jquery 编写 spring mvc 应用程序,但自上周以来一直卡在某个时间点并且无法找到解决方案。请帮帮我。

我想发送请求以使用 ajax 在数据库中显示我的所有用户,看看我是怎么做的

显示用户列表

现在 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);
                }
                });
        }

    }

现在见我控制器

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

现在看看我是如何在 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>

.................................................. ……………………………………………………………………………… 我没有得到的是,当响应来自控制器时(在我的例子中是 ModelMap 对象) 我如何将包含用户列表的模型传递给我的 jsp 页面以便它可以显示。 我试过了
$('#listofUser').html(响应);但它不工作, 我希望包含 (model.put("users", userList);) users 的模型可从 javascript 用于我的显示部分.. 请帮帮我...... 谢谢并恭祝安康, 赫曼特·辛格

【问题讨论】:

标签: spring jquery spring-mvc


【解决方案1】:

我正在使用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");

【讨论】:

  • 如何在 spring mvc 中与 ajax 一起使用??
【解决方案2】:

@ResponseBody 不是这样工作的,你应该返回一个 JSON 来显示内容,你可以用 jQuery 来做,但有点棘手,这就是为什么我向你推荐 AngularJS,忘记 JSTL。

Here有一个教程,SpringMVC、JSTL和异步请求一起工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 2014-07-23
    • 2012-06-06
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多