java - Spring MVC : Multiple <a> tag in one form

标签 java spring-mvc jsp model-view-controller

我研究过不同的解决方案,但它们对我不起作用。我想通过我的标签传递数据。但我认为 Spring MVC 中不允许在表单中使用多个标签。我查看了多个提交按钮,但它们没有正确传递我的数据。当我使用提交按钮时,它们传递第一行 ID,而不是已单击的行 ID。有没有办法用标签来做到这一点,如果有的话,如果你们能帮助我,我将非常感激。

这是我的jsp表单:

   <form action="editOrDelete" method="get" id="disp">
            <table id="rehber" align="center"  >
                <thead>
                    <tr bgcolor="#333">
                        <th  style="width: 0%;"><font color="#fff">ID</font></th>
                        <th  style="width: 0%;"><font color="#fff">NAME</font></th>
                        <th  style="width: 0%;"><font color="#fff">EMAIL</font></th>
                        <th  style="width: 100%;"><font color="#fff">ACTION</font></th>    
                    </tr>
                </thead>
                <TBody>
                    <c:forEach items="${data}" var="list">
                    <tr>
                        <td><input readonly name="id" id="id" value="<c:out value="${list.id}"/>"></td>
                        <td><input readonly name="name" id="name" value="<c:out value="${list.name}"/>"></td>
                        <td><input readonly name="email" id="email" value="<c:out value="${list.email}"/>"></td>
                        <td>

                            <a href="editOrDelete/edit/${list.id}" style="text-decoration: none; background:#333;" class="edit"   >Edit</a>
                            <a href="editOrDelete/delete/${list.id}" style="text-decoration: none; background: rgb(163, 2, 2);" class="edit"   >Delete</a>

                        </td>
                    </tr>
                    </c:forEach>
                </TBody>
            </table>
        </form> 

我的 Controller :

@RequestMapping(value="/editOrDelete/delete/{id}" )
    public String deleteData(HttpServletRequest request, HttpServletResponse response, @PathVariable(value="id") String id) {
        //get the id of the chosen input

        String path="";

        try{
            //String id= request.getParameter("hiddenDelete");
            String idStr = id;

            //connect to database
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3308/idk?autoReconnect=true&useSSL=false", "root", "");
            Statement stat=(Statement) conn.createStatement();

            //delete the given id from the database
            String sql= "DELETE FROM smth WHERE id='" +idStr + "'" ;
            stat.executeUpdate(sql);
            path = displayTable(request, response);
        }catch(Exception e) {}
        return path;

    }
@RequestMapping(value="/editOrDelete/edit/{id}" )
    public String fillTheTextBoxes(HttpServletRequest request, HttpServletResponse response,@PathVariable(value="id") String id) {
        String path="";
        try{
            //get the id from the url
            //String id= request.getParameter("hiddenEdit");
            String idStr =id;


            //set a variable for editServlet
            request.getSession().setAttribute("editId", idStr);

            //connect to database
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3308/idk?autoReconnect=true&useSSL=false", "root", "");

            //get the chosen id's attributes
            Statement stat=(Statement) conn.createStatement();
            String sql= "SELECT * FROM smth WHERE id='" +idStr + "'" ;
            ResultSet rs = stat.executeQuery(sql);
            String nameTextBox="";
            String emailTextBox="";
            while(rs.next()) {
                nameTextBox= rs.getString("name");
                emailTextBox= rs.getString("email");

            }

            //fill in the input box
            request.getSession().setAttribute("nameText", nameTextBox);
            request.getSession().setAttribute("emailText", emailTextBox);
            path = displayTable(request, response);
        }catch(Exception e) {}
        return path;

    }

最佳答案

您可以通过表格来完成。这是一个解决方法,但它会起作用。只需在每个循环中创建两个表单,而不是在顶部创建一个表单。在 for Each 循环中,只需放置一个用于编辑的表单和另一个用于删除的表单。

<form action="editOrDelete/edit/${list.id}"  method="get">
<input type="submit" value="Delete">
</form>

<form action="editOrDelete/delete/${list.id}" method="get">
    <input type="submit" value="Edit">
</form>

只需将这些表单放入 for every 循环中,而不是您正在使用的单个循环中。

关于java - Spring MVC : Multiple <a> tag in one form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60072330/

相关文章:

java - 实现 MVC 时,应用程序的哪一部分应该调用 DAO 中的方法?

scala - 将@Autowired of spring 与 scala 一起使用

image - Spring MVC 图像 Controller ,用于在 JSP 中显示图像字节

java - 将文件保存到在同一 Tomcat 实例中运行的另一个 Web 应用程序(不工作)

java - Double.parseDouble ("100d") 如何没有错误

java - 为什么 ExceptionHandlerExceptionResolver 会进行重定向而不是简单的响应?

java - <h :CommandButton> action attribute not working in JSF 1. 2

java - Spring mvc - jsp MySQLSyntaxErrorException

java - Hamcrest assertThat 在移动项目时不起作用

java - 不理解 Java 中的 Echo e2=e1