Announcement Announcement Module
Collapse
No announcement yet.
Spring, JSP and JQuery Ajax Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, JSP and JQuery Ajax

    I have a page with several tables displaying users with different roles. I am trying to update one of the tables after I add a user using jQuery post function. How do I do this?

    I have each table in a JSP fragment such as the following:

    Code:
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <table id="users1">
      <thead>
         <tr>
           <th>Name</th>
           <th>Age</th>
         <tr>
      </thead>
    
      <tbody>
        <c:forEach items="${users1Users}" var="user">
          <tr>
            <td>${user.name}</td>
            <td>${user.age}</td>
          </tr>
        </c:forEach>
      </tbody>
    </table>
    The JSP page includes each JSP Fragment (1 fragment per 1 table like above):

    Code:
    <body>
      <div>
        <div id="users1Tray>
           <jsp:include page="users1.jspf"/>
        </div>
        <div id="users1Tray>
           <jsp:include page="users2.jspf"/>
        </div>
        <div id="users1Tray>
           <jsp:include page="users3.jspf"/>
        </div>
      <div>
    </body>
    The JavaScript uses jQuery to make a post (Ajax) call to add a new user:

    Code:
    function handleAddUser1(usersName) {
      $.post('/usersAdmin/addUser1',
               { "name" : usersName },
               function(newListOfUsers1) {
                  // What do I do here?
               }
    }
    Finally here is the Controller code for the URL specified in the JavaScript post call:

    Code:
    @RequestMapping(value="/usersAdmin/addUser1", method=RequestMethod.POST)
    protected List<user> addUser1(@RequestParam("name") String name) {
    
      someService.addUser1(name);
      List<User> allUsers = someService.getAllUser1();
      return allUsers;
    }
    So my question is what do I do to "call" the JSP Fragment containing the users1 table so that it can be updated with the new list of users?

    I am using Spring 3.1.

  • #2
    Are you familiar with how to append, delete, and modify rows in an HTML table via jQuery? If you do, then all you need to do is put an id attribute on that users1 table. Then use jQuery to manipulate the contents of that table.

    Alternatively, instead of appending an id on that table, you can use the div id users1Tray, and use jQuery to traverse the path to your first table.

    If you're not familiar, I suggest a quick Google on how to do such.

    Comment


    • #3
      Spring, JSP and JQuery Ajax

      I can try it that way. I was hoping there was some "Spring" way of doing it such that I didn't need to really do anything. I thought returning a view name would do it but it doesn't seem to work.

      Comment


      • #4
        Hi...Thanks for the tutorial and the information provided about various technologies....!!!!

        Comment

        Working...
        X