java - HTML 中的第一个提交按钮重定向到不同的 URL

标签 java html thymeleaf

我是 Thymeleaf 新手,尝试删除第一条记录,但它将我重定向到 http://localhost:8080/findall?_method=delete 。尽管我能够删除所有记录,并且它们将我重定向到正确的网址 http://localhost:8080/delete/{id}(第一个记录除外)。

每当我尝试通过点击 postman 的“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");
    }

}

enter image description here

如果我点击删除按钮删除第一条记录“Ravi”,它将带我到 URL“http://localhost:8080/findall?_method=delete”并且不会删除该记录(我希望它带我到 URL“http://localhost:8080/delete/1”)。但如果我单击任何其他按钮,它工作正常并删除记录。

我还检查了浏览器上的 HTML 代码。它不会为每条记录生成相同的 HTML 代码。 这应该是一个非常简单的修复,虽然我在 stackoverflow 上没有找到太多关于 Thymeleaf 的内容。

最佳答案

method属性仅接受 GET or POST作为它的值。

您可以更改th:method值“post”,并更新 Controller 中的映射:

 @RequestMapping(method = RequestMethod.POST, value = "/delete/{id}")

更新 另请删除 <form>附上您的 table 。

关于java - HTML 中的第一个提交按钮重定向到不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45566915/

相关文章:

Java MySQL IN 子句中的特殊字符

python - 禁用特殊的 "class"属性处理

html - 为什么我的 inline-block 后面有空格?

java - Spring MVC + Thymeleaf - 正确的上下文路径处理

html - 选项卡内容布局显示在其他选项卡中

java - 什么时候会使用 Servlet init params?

java - 将javaString拆分为字符和数值的子字符串

java - 尝试调用虚拟方法“android.view.Menu”

javascript - 如何使每行颜色不同的html文本输入

spring - 显示登录用户 spring security