简体   繁体   English

如何通过设置会话变量

[英]How do I set a session variable via <c:set within a link in JSTL/JSP?

I'm having problems in knowing what row has been selected when using the <c:forEach tag in JSTL/JSP page. 我在使用JSTL / JSP页面中的<c:forEach标记时不知道选择了哪一行时遇到了问题。

The below is the snippet from my JSP page, that successfully prints out all the Users that I have from my session variable. 以下是我的JSP页面中的代码段,该页面成功打印出了我的session变量中的所有Users。 Also included in the UserList class is the userId that corresponds to the rowID in the database. UserList类中还包括与数据库中的rowID相对应的userId。

        <tbody>
            <c:forEach items="${userList}" var="user">
                <tr>      
                    <td>${user.firstName}</td>
                    <td>${user.lastName}</td>
                    <td>${user.email}</td>
                    <td><a href="#UploadModal" data-toggle="modal"><c:set var="selectedUser" value="${user.userId}" scope="request" />Upload a File</a></td>
                </tr>
            </c:forEach>
        </tbody>

The modal link is a pop-up modal from twitter Bootstrap. 模式链接是来自Twitter Bootstrap的弹出模式。

    <div id="UploadModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Upload Document for User <c:out value="${requestScope.selectedUser}"></c:out></h3>
        </div>
        <div class="modal-body">
            <form method="post" action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data">
                <table border="0">
                    <tr><td>File:</td>
                        <td><input type="file" name="photo" size="50"/></td>
                    </tr>
                    <tr><td colspan="2"><input type="submit" value="Save"></td></tr>
                </table>
            </form>
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        </div>
    </div>

What I wanted to do was that the rows of all users are displayed. 我想做的是显示所有用户的行。 There's a link next to each one, that when you click it pops up the modal form, and then the UserId is included in the session variable and also displayed. 每个链接旁边都有一个链接,单击该链接时会弹出模式形式,然后UserId包含在会话变量中并显示。 When it comes to submitting the form back to the servlet, I can send the file and any other details that I want, along with the UserId that was actually selected. 在将表单提交回servlet时,我可以发送文件和所需的任何其他详细信息,以及实际选择的UserId。

Unfortunately the session variable always sets the userId to that of the last record. 不幸的是,会话变量始终将userId设置为最后一条记录的userId。

I'm probably doing a lot of things wrong, but I'm just confused as to how else I'd get to know what record is clicked on in the table of the web page? 我可能做错了很多事情,但是我对如何知道网页表中单击的记录感到困惑。

Any help is much appreciated.. 任何帮助深表感谢..

Of courrse in this loop you are resetting the value of the var selectedUser each iteration of the loop. 在此循环中,您需要在循环的每次迭代中重置var selectedUser的值。

Try setting the id of the <a href to something like user+${user.userId} and then use jquery or javascript to listen for the click and then obtain the id of the link that is clicked. 尝试将<a hrefid设置为类似于user+${user.userId} ,然后使用jquery或javascript侦听单击,然后获取被单击的链接的id

You can then use this value to set a parameter on the form (using jquery or javascript) which will then be posted with the other data. 然后,您可以使用此值在form上设置parameter (使用jquery或javascript),然后将其与其他数据一起发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM