Issue
I have looked at different solutions but they didn't work for me. I want to pass data through my tags. But I think having multiple tags in form isn't allowed in Spring MVC. I have looked at multiple submit buttons but they are not passing my data correctly. When I use submit buttons they pass the first rows id instead of the one that's been clicked. Is there a way to do it with tags, if there is I would really appreciate if you guys can help me with it.
Here is my jsp form:
<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>
My 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;
}
Solution
You can do it via form. It is a workaround but it will work. Just create two form inside your for each loop rather than making one at the top. Inside your for Each loop just place a form for edit and another one for delete.
<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>
Just put these forms inside your for each loop rather than the single one that you are using.
Answered By - Silverfang
Answer Checked By - David Marino (JavaFixing Volunteer)