我是Thymeleaf的新手并试图删除第一条记录,但它将我重定向到http://localhost:8080/findall?_method=delete
。虽然我可以删除所有记录,但他们会将我重定向到正确的网址http://localhost:8080/delete/{id}
(第一个除外)。
每当我尝试通过点击" http://localhost:8080/delete/ {id}"来删除记录时从邮递员,我能够做到这一点。因此,这必须使用.html文件。
这是display.jsp文件的代码:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>Registration Table
</head>
<body>
<form>
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Password</th>
<th>Delete</th>
</tr>
<tr th:each="list : ${list}">
<td th:text="${list.id}">ID</td>
<td th:text="${list.userName}">Name</td>
<td th:text="${list.userPassword}">Password</td>
<!-- <td><a th:href="@{/delete/{id}}">Edit</a></td> -->
<td>
<form th:action="@{'/delete/{id}'(id=${list.id})}" th:method="delete">
<input type="submit" value="Delete"/>
</form>
</td>
</tr>
</table>
</form>
</body>
</html>
这里是controller.java
package com.spring.controller;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.xml.ws.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.couchbase.client.deps.io.netty.handler.codec.http.HttpResponse;
import com.spring.model.Customer;
import com.spring.repository.CustomerRepo;
@RestController
public class Controller extends HttpServlet {
@Autowired
CustomerRepo rp;
@RequestMapping(method = RequestMethod.GET, value = "/findall")
@ResponseBody
public ModelAndView findall() {
List<Customer> list = (List<Customer>) rp.findAll();
ModelAndView mv = new ModelAndView();
mv.setViewName("display");
mv.addObject("list", list);
for (Customer element : list) {
System.out.println("Element usernane: " + element.getUserName());
System.out.println("Element password: " + element.getUserPassword());
}
mv.addObject("list", list);
return mv;
}
@RequestMapping(method = RequestMethod.GET, value = "/display")
@ResponseBody
public List<Customer> display() {
return (List<Customer>) rp.findAll();
}
@RequestMapping("/")
public ModelAndView home() {
System.out.println("display home");
ModelAndView mv = new ModelAndView("home");
return mv;
}
@RequestMapping(method = RequestMethod.DELETE, value = "/delete/{id}")
public ModelAndView deleteCourse(@PathVariable Long id) {
System.out.println("deleting" + id);
rp.delete(id);
ModelAndView mv = new ModelAndView("redirect:home");
return new ModelAndView("redirect:home");
}
}
如果我点击删除按钮删除第一条记录&#34; Ravi&#34;它会带我到URL&#34; http://localhost:8080/findall?_method=delete&#34;并且不会删除记录(我希望它带我去网址&#34; http://localhost:8080/delete/1&#34;)。但是如果我点击任何其他按钮它工作正常并删除记录。
我还检查了浏览器上的HTML代码。它不会为每条记录生成相同的HTML代码。 它应该是一个非常简单的修复,虽然我没有在stackoverflow上找到关于Thymeleaf的更多内容。
答案 0 :(得分:2)
method
属性仅接受GET or POST作为其值。
您可以将th:method
值更改为&#34;发布&#34;,然后在控制器中更新您的地图:
@RequestMapping(method = RequestMethod.POST, value = "/delete/{id}")
<强>更新强>
另外,请删除附表中的<form>
。
答案 1 :(得分:0)
我在HTML中使用了两种形式。从HTML代码中删除外部<form>
标记解决了该问题。