我刚接触Spring,尝试学习基本的Crud操作,但是我坚持使用Delete操作,我的实体看起来像下面

public class Alien {
@Id
int aid;
String aname;
public int getAid() {
    return aid;
}
public void setAid(int aid) {
    this.aid = aid;
}
public String getAname() {
    return aname;
}
public void setAname(String aname) {
    this.aname = aname;
}


我的home.jsp文件如下所示

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="addAlien">
<input type="text" name="aid"><br>
<input type="text" name="aname"><br>
<input type="submit"><br>
</form>

<form action="deleteAlien">
<input type="text" name="aid"><br>
<input type="submit"><br>
</form>
</body>
</html>


和控制器看起来像以下我要删除操作中的提交按钮,我想删除基于id的条目

public class HomeController {
@Autowired
Alienrepo alienrepo;

@RequestMapping("/")
public String home() {
  return "home.jsp";
  }

@RequestMapping("/addAlien")
public String addAlien(Alien alien) {
    alienrepo.save(alien);
    return "home.jsp";

}
@RequestMapping("/deleteAlien")
public String deleteAlien(Integer id) {
    alienrepo.deleteById(id);
    return "home.jsp";
}
}


我想念的是什么?

最佳答案

您对HTTP请求的理解还不完整。您没有在任何API中配置HTTP方法,因此所有API均具有默认方法GET。您需要配置要在请求中发送的参数。喜欢 :

@RequestMapping("/deleteAlien/{id}")
public String deleteAlien(@PathVariable("id") Integer id) {
    alienrepo.deleteById(id);
    return "home.jsp";
}


您应该先阅读有关HTTP的RestControllers。

09-11 12:11
查看更多