Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用spring将对象从jsp传递到控制器_Java_Spring_Jsp - Fatal编程技术网

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

Java 如何使用spring将对象从jsp传递到控制器,java,spring,jsp,Java,Spring,Jsp,我有一个类用户和一个类项目,其中有一个包含用户的arraylist。 我有一个包含我所有项目列表的项目页面,当我点击其中一个项目时,它会通过在url中发送该项目的id将我带到该项目的页面 在我的详细项目页面上,我想添加我创建的用户。 用户显示在表格中的模式上,并带有一个添加按钮 <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog">

我有一个类用户和一个类项目,其中有一个包含用户的arraylist。 我有一个包含我所有项目列表的项目页面,当我点击其中一个项目时,它会通过在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>

&时代;
用户列表


照片 名字 姓氏 作用 电子邮件 ${u.getFirstname()} ${u.getLastname()} ${u.getFunction()} ${u.getEmail()} 添加
我的问题是如何将用户id和项目id添加到控制器中

我知道我可以通过url传递id,但我不想打开新页面


我想通过单击“添加”按钮将用户和项目的ID发送到我的控制器,这样我就可以在渲染页面中使用名为AddUserTopProject()的方法中的ID。如果不想打开新url,您只有两个选项,一个是执行普通表单post,另一个是使用Ajax

在呈现的页面中,您只有两个选项,一个是正常表单post,另一个是如果不想打开新url,则使用Ajax

JSP

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

<input type="hidden" id="currentProjectId" value="12">
发出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:

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

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

<input type="hidden" id="currentProjectId" value="12">
发出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:

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

你能提供你的控制器代码吗?可能会有帮助。请提供控制器代码好吗?可能会有帮助。像这样:。然后得到值:var currentprojectd=$('#currentprojectd').val();我如何检查AJAX脚本是否到达我的控制器,因为我已经尝试了您的解决方案,但似乎无法使其工作。右键单击->检查元素以查看来自选项卡控制台的消息。您还可以看到来自网络选项卡的ajax请求。请参阅:请发送错误。另外,请确保您已将@RequestParam(“userId”)中的空格删除到@RequestParam(“userId”)。这是我现在遇到的错误。如下所示:。然后得到值:var currentprojectd=$('#currentprojectd').val();我如何检查AJAX脚本是否到达我的控制器,因为我已经尝试了您的解决方案,但似乎无法使其工作。右键单击->检查元素以查看来自选项卡控制台的消息。您还可以看到来自网络选项卡的ajax请求。请参阅:请发送错误。另外,请确保您已将@RequestParam(“userId”)中的空格删除到@RequestParam(“userId”),这是我现在遇到的错误。