Добавление, удаление, методы отображения работают нормально.
Вот в чем проблема, с которой я столкнулся получаю, когда пытаюсь обновить.
ВНИМАНИЕ: решено [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
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
getAllPersons() {
Session session=this.sessionfactory.getCurrentSession();
@SuppressWarnings("unchecked")
List 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);
}
}
}
Код: Выделить всё
Person Details
Add Person
Name:
Country:
Person List
Id
Person Name
Country
Edit
Delete
${person.id}
${person.name}
${person.country}
[url=Delete[/url]
Подробнее здесь: https://stackoverflow.com/questions/703 ... ion-reques
Мобильная версия