我使用 Spring Mvc、Hibernate、Maven 创建了一个示例 crud 操作应用程序。我的所有操作都运行良好。我在更新方法上遇到了问题。我不知道我哪里出错了。我参考了这篇文章。 https://java2blog.com/spring-mvc-hibernate-mysql-crud-example/
添加、删除、显示方法工作正常。 这是我尝试更新时遇到的问题。 警告:已解决 [org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST”] 我不知道出了什么问题。请帮忙
控制器类:
package com.spring.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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.ResponseBody;
import com.spring.bean.Person;
import com.spring.service.PersonService;
@Controller
public class PersonController {
@Autowired
PersonService service;
@RequestMapping(value = "/getAllPersons", method = RequestMethod.GET, headers = "Accept=application/json")
public String getPerson(Model model) {
List<Person> listOfPersons = service.getAllPersons();
model.addAttribute("person", new Person());
model.addAttribute("listOfPersons", listOfPersons);
return "PersonDetails";
}
@RequestMapping(value = "/getPerson/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Person getPerson(@PathVariable int id) {
return service.getPerson(id);
}
@RequestMapping(value = "/addPerson", method = RequestMethod.POST, headers = "Accept=application/json")
public String addPerson(@ModelAttribute("person") Person person) {
if(person.getId()==0) {
service.addPerson(person);
}
else {
service.updatePerson(person);
}
return "redirect:/getAllPersons";
}
//@RequestMapping(value = "/updateCountry/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
@RequestMapping(value = "/updatePerson/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public String updatePerson(@PathVariable("id") int id,Model model) {
model.addAttribute("person", this.service.getPerson(id));
model.addAttribute("listOfPersons", this.service.getAllPersons());
return "PersonDetails";
}
@RequestMapping(value = "/deletePerson/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public String deletePerson(@PathVariable("id") int id) {
service.deletePerson(id);
return "redirect:/getAllPersons";
}
}
道类:
package com.spring.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.spring.bean.Person;
@Repository
public class PersonDao {
@Autowired
private SessionFactory sessionfactory;
public void setSessionFactory(SessionFactory sf) {
this.sessionfactory = sf;
}
public List<Person> getAllPersons() {
Session session=this.sessionfactory.getCurrentSession();
@SuppressWarnings("unchecked")
List<Person> persons= session.createQuery("from Person").list();
for(Person p:persons) {
System.out.print(p.getName()+" "+p.getCountry());
}
return persons;
}
public Person getPerson(int id) {
Session session=this.sessionfactory.getCurrentSession();
System.out.println("Here");
Person p=(Person)session.load(Person.class, new Integer(id));
System.out.println(p);
return p;
}
public Person addPerson(Person person) {
Session session=this.sessionfactory.getCurrentSession();
session.persist(person);
return person;
}
public void updatePerson(Person person) {
Session session=this.sessionfactory.getCurrentSession();
session.update(person);
}
public void deletePerson(int id) {
Session session=this.sessionfactory.getCurrentSession();
Person p=session.load(Person.class, new Integer(id));
if(null!= p) {
session.delete(p);
}
}
}
Jsp页面:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Person Details</title>
</head>
<body>
<form:form method="post" modelAttribute="person" action="addPerson">
<table>
<tr>
<th colspan="2">Add Person</th>
</tr>
<tr>
<form:hidden path="id" />
</tr>
<tr>
<td><form:label path="name">Name:</form:label></td>
<td><form:input path="name" size="30" maxlength="30"></form:input></td>
</tr>
<tr>
<td><form:label path="country">Country:</form:label></td>
<td><form:input path="country" size="30" maxlength="30"></form:input></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
class="blue-button" /></td>
</tr>
</table>
</form:form>
<hr>
<h3>Person List</h3>
<c:if test="${!empty listOfPersons}">
<table class="tg">
<tr>
<th width="80">Id</th>
<th width="120">Person Name</th>
<th width="120">Country</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listOfPersons}" var="person">
<tr>
<td>${person.id}</td>
<td>${person.name}</td>
<td>${person.country}</td>
<td><a href="<c:url value='updatePerson/${person.id}' />" >Edit</a></td>
<td><a href="<c:url value='deletePerson/${person.id}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
您的端点仅接受
GET
请求。您需要将其更改为POST
,如下所示:
@RequestMapping(value = "/updatePerson/{id}", method = RequestMethod.POST,
headers = "Accept=application/json")
public String updatePerson(@PathVariable("id") int id,Model model) {
model.addAttribute("person", this.service.getPerson(id));
model.addAttribute("listOfPersons", this.service.getAllPersons());
return "PersonDetails";
}
尽管如此,您应该考虑使用
PUT
进行更新请求,以便您遵循标准。您可以在 https://www.restapitutorial.com/lessons/httpmethods.html. 阅读更多内容
我在我的 sprint 安全 xml 文件中启用了 csrf,所以我只在表单中添加了一行:
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
这样我就可以提交具有模型属性的表单了。