java - 如何使用 spring 将对象从 jsp 传递到 Controller

标签 java spring jsp

我有一个 User 类和一个 Project 类,其中有一个包含用户的数组列表。 我有一个项目页面,其中包含我的所有项目的列表,当我单击其中一个项目时,它会通过在 URL 中发送该项目的 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 和我希望将它们添加到我的 Controller 中的项目的 id?

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

我想通过单击 Controller 的添加按钮将用户的 ID 和项目发送到我的 Controller ,以便我可以在名为 addUserToProject() 的方法中使用它们

最佳答案

JSP

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

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

其次,设置添加按钮的属性name等于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发布到 Controller :

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");
        }
    });
}

最后, Controller 使用@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";
}

关于java - 如何使用 spring 将对象从 jsp 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576342/

相关文章:

java - 如何使用正则表达式解析 ping 结果

java - 如何从 BufferedReader 获取进度百分比?

java - 如何使用 Jackson 自动解析 Spring Boot 应用程序中的 JSON

java - 没有Struts标签的Struts 2文件上传

java - 如何管理临时文件的创建和删除

java - 从坐标获取随机位置

java - 杀死 java.lang.Process 而不杀死 subprocess()

java - 如何正确注册自定义 JsonSerializer?

java - 如何删除基本身份验证的弹出窗口

java - 将 CSS 文件包含到 JSP 中