Я довольно новичок в спящем и Spring MVC. Я пробовал hibernate и Spring MVC в демонстрационном веб-приложении. Проблема в том, что когда я go редактирую страницу и нажимаю кнопку «Сохранить» после нажатия кнопки «Сохранить», я получаю «HTTP status 400 -Bad Request». Буду очень признателен, если вы, ребята, сможете мне помочь.
Это мой индекс. jsp: (Там, где есть столбец действия, в котором есть кнопка редактирования и удаления)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>CUSTOMER MANAGER</title>
</head>
<body>
<div align="center">
<h2>Customer Manager</h2>
<form method="get" action="search">
<input type="text" name="keyword" />
<input type="submit" value="Search" />
</form>
<br>
<form action="new">
<button type="submit" >New Customer</button>
</form>
<br>
<hr>
<br>
<table border="1" cellpadding="5">
<tr>
<th>ID</th>
<th>Name</th>
<th>E-mail</th>
<th>Address</th>
<th>Action</th>
</tr>
<c:forEach items="${listCustomer}" var="customer">
<tr>
<td>${customer.id}</td>
<td>${customer.name}</td>
<td>${customer.email}</td>
<td>${customer.address}</td>
<td>
<form action="edit/${customer.id}" method="post">
<button type="submit" >Edit</button>
</form>
<form action="delete/${customer.id}" method="post">
<button type="submit">Delete</button>
</form>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
Вот мой edit_customer. jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Edit Customer</title>
</head>
<body>
<div align="center">
<h2>Edit Customer</h2>
<form:form action="save" method="post" modelAttribute="customer">
<table border="0" cellpadding="5">
<tr>
<td>ID:</td>
<td>${ customer.id }</td>
<form:hidden path = "id"></form:hidden>
</tr>
<tr>
<td>Name: </td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Email: </td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>Address: </td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Save"></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>
Вот мой CustomerController. java:
package net.codejava.customer;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class CustomerController {
@Autowired
private CustomerService service;
@RequestMapping("/")
public ModelAndView home(){
ModelAndView mav = new ModelAndView("index");
List<Customer> listCustomer = service.listAll();
mav.addObject("listCustomer", listCustomer);
return mav;
}
@RequestMapping("/new")
public String newCustomerForm(Map<String, Object> model) {
model.put("customer", new Customer());
return "new_customer";
}
@RequestMapping(value = "/save", method = RequestMethod.POST )
public String saveCustomer(@ModelAttribute("customer") Customer customer) {
service.save(customer);
return "redirect:/";
}
@RequestMapping(value="/edit/{id}" , method = RequestMethod.POST )
public ModelAndView editCustomer(@PathVariable(value="id") Long id) {
ModelAndView mav = new ModelAndView("edit_customer");
Customer customer = service.get(id);
mav.addObject("customer", customer);
return mav;
}
@RequestMapping(value="/delete/{id}" , method = RequestMethod.POST )
public String deleteCustomerForm(@PathVariable(value="id") Long id) {
service.delete(id);
return "redirect:/";
}
@RequestMapping("/search")
public ModelAndView search(@RequestParam String keyword) {
ModelAndView mav = new ModelAndView("search");
List<Customer> listCustomer = service.search(keyword);
mav.addObject(listCustomer);
return mav;
}
}
Вот мой CustomerService. java
package net.codejava.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class CustomerService {
@Autowired CustomerRepository repo;
public void save(Customer customer) {
repo.save(customer);
}
public List<Customer> listAll() {
return (List<Customer>) repo.findAll();
}
public void delete(Long id) {
repo.deleteById(id);
}
public Customer get(Long id) {
Optional<Customer> result = repo.findById(id);
return result.get();
}
public List<Customer> search(String keyword) {
return repo.search(keyword);
}
}
Вот мой класс сущности Customer. java
package net.codejava.customer;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String address;
public Customer() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}