简体   繁体   中英

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.

The below is the snippet from my JSP page, that successfully prints out all the Users that I have from my session variable. Also included in the UserList class is the userId that corresponds to the rowID in the database.

        <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.

    <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. 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.

Unfortunately the session variable always sets the userId to that of the last record.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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