0

私は ajax と jquery を使用して spring mvc アプリケーションを作成しようとしていますが、ある時点で立ち往生しており、最後の 1 週間から解決策を見つけることができません..助けてください。

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(response); を試してみました しかし、それは機能していません。(model.put("users", userList);)ユーザーを含むモデルが、javascript から私の表示部分に利用できるようにしたい..助けてください..... ありがとう、Hemant Singh

4

2 に答える 2

2

私は春の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");
于 2012-10-15T08:39:18.820 に答える