【问题标题】:Spring MVC : Multiple <a> tag in one formSpring MVC:一种形式的多个 <a> 标记
【发布时间】:2020-05-21 03:54:51
【问题描述】:

我查看了不同的解决方案,但它们对我不起作用。我想通过我的标签传递数据。但我认为在 Spring MVC 中不允许有多个标签。我查看了多个提交按钮,但它们没有正确传递我的数据。当我使用提交按钮时,它们传递第一行 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> 

我的控制器:

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

    }

【问题讨论】:

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


    【解决方案1】:

    您可以通过表单来完成。这是一种解决方法,但它会起作用。只需在每个循环中创建两个表单,而不是在顶部创建一个。在你的 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 each 循环中,而不是您正在使用的单个循环中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2012-01-05
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      相关资源
      最近更新 更多