如何使用spring将对象从jsp传递给控制器

时间:2017-11-30 14:56:01

标签: java spring jsp

我有一个类User和一个类Project,它有一个用户的arraylist。 我有一个项目页面,其中包含我所有项目的列表,当我点击一个项目时,它会通过在网址中发送该项目的ID来转到该项目的页面。

在我的详细项目页面上,我想添加我创建的用户。 用户显示在表格中的模态上,并带有一个按钮来添加它们。

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"></h4>
                </div>
                <div class="modal-body">
                    <h1 class="text-center">UserList</h1>
                    <br><br>
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Photo</th>
                                <th>Firstname</th>
                                <th>Lastname</th>
                                <th>Function</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach var="u" items="${users}">
                                <tr>
                                    <td><img src="${u.getPhoto()}"
                                             alt="Alternate Text" class="img-responsive" /></td>
                                    <td>${u.getFirstname()}</td>
                                    <td>${u.getLastname()}</td>
                                    <td>${u.getFunction()}</td>
                                    <td>${u.getEmail()}</td>
                                    <td><Button type="Button" class="btn btn-default">Add</button></td>
                                </tr> 
                            </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

我的问题是如何发送用户的ID和项目的ID,我想将它们添加到我的控制器中?

我知道我可以通过网址传递ID,但我不想打开新页面。

我想通过点击我的控制器的添加按钮将用户和项目的ID发送到我的控制器,这样我就可以在我的方法中使用那些名为addUserToProject()的文件

2 个答案:

答案 0 :(得分:0)

从呈现的页面中,您只有两个选项,您可以执行普通的表单发布,也可以使用Ajax,如果您不想打开新的URL。

答案 1 :(得分:0)

<强> JSP

首先,使用选定的项目ID创建隐藏输入,以便稍后获取:

<input type="hidden" id="currentProjectId" value="12">

其次,将添加按钮的属性名称设置为 userId

<td><Button type="Button" name="${u.getId()}" class="btn btn-default">Add</button></td>

<强>使用Javascript:

为每个“添加按钮”定义onclick linstener并从按钮获取当前用户ID:

   $(".btn").click(function(event) {
         event.preventDefault();

         var currentProjectId= $('#currentProjectId').val();
         var userId = $(this).attr("name");

        addUser(currentProjectId, userId);

    });

制作 ajax请求并将ID发布到控制器:

function addUser(currentProjectId, selectedUserId) {

    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : "/addUserToProject",
        data :{ projectId: currentProjectId, userId: selectedUserId}, 
        dataType : 'json',
        timeout : 100000,
        success : function(data) {
            console.log("SUCCESS: ", data);
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });
}

最后,控制器使用 @RequestParam:

接受ID
@RequestMapping(value = "/addUserToProject", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
String Submit(@RequestParam("projectId") Integer projectId ,@RequestParam("userId ") Integer userId ) {

   //insert user

    return "ok";
}
相关问题