如何知道在.JSP文件中单击了哪个按钮

时间:2019-03-21 18:53:01

标签: java jsp servlets

因此,我有一个应用程序,在该应用程序中,我在一个视图上显示一个用户列表,除了用户列表之外,还有一些按钮可以重定向或执行操作。由于用户列表是通过java中的foreach加载到HTML中的,所以我不知道如何知道单击了哪个按钮。例如,如果更改了用户角色,然后单击了“保存更改”按钮,我怎么知道更改了特定用户的HTML中的哪个选择选项?

这是我的JSP文件代码:

<html>
    <body>
        <h2> Logged in as administrator:
            <% User user = (User) session.getAttribute("user"); %>
            <%=user.getUsername() %>
        </h2>

        <form method="post" action="login">
            <input type="submit" name="Logout" value="Logout">
        </form>

        <h3> Manage screen </h3>

        <ul>
            <% List<User> userList = (List<User>) session.getAttribute("userList");

            for (User u : userList) { %>
                <form method="post" action="manage">
                    Username: <input type="text" name="Username" value="<%=u.getUsername()%>" readonly> Role:
                    <select name="Role">
                        <%if (u.getRole().equals(Role.ADMIN)) { %>
                            <option value="Admin" selected> Admin </option>
                            <option value="User"> User </option>
                        <% } else { %>
                            <option value="Admin"> Admin </option>
                            <option value="User" selected> User </option>
                        <% } %>
                    </select>
                    <input onclick="" type="submit" name="SaveChanges" value="Save changes">
                    <input type="submit" name="OpenKweets" value="Open kweets">
                </form>
            <% } %>
        </ul>
    </body>
</html>

这是我的Servlet的代码:

if (request.getParameter("SaveChanges") != null) {
        String chosenRole = request.getParameter("Role");
        User user = this.userService.getUser(username);

        if (chosenRole.equals("Admin")) {
            user.setRole(Role.ADMIN);
            this.userService.updateUser(user);
        }
        else {
            user.setRole(Role.USER);
            this.userService.updateUser(user);
        }
        response.sendRedirect("manage.jsp");
 }
 if (request.getParameter("OpenKweets") != null) {
        List<Kweet> kweetList = userService.getKweets(username);
        session.setAttribute("kweetList", kweetList);
        session.setAttribute("kweetListFor", username);
        response.sendRedirect("manageOpenKweets.jsp");
        return;
 }

Here is a picture of how the page looks when loaded

0 个答案:

没有答案