简体   繁体   English

HTTP 状态 400 - 错误请求 - 休眠

[英]HTTP Status 400 – Bad Request - Hibernate

I'm pretty new to hibernate and Spring MVC.我对 hibernate 和 Spring MVC 很陌生。 I was trying hibernate and Spring MVC on a Demo web application.我在 Demo web 应用程序上尝试 hibernate 和 Spring MVC。 The problem is that when I go to edit page and hit save button after I hit save I get "HTTP status 400 -Bad Request".问题是,当我进入编辑页面并在点击保存后点击保存按钮时,我得到“HTTP 状态 400 - 错误请求”。 I would really appreciate it if you guys can help me.如果你们能帮助我,我将不胜感激。

This is my index.jsp: (Where there is an action column which has edit and delete button)这是我的 index.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" />  &nbsp; 
        <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>
                &nbsp;&nbsp;&nbsp;
                <form action="delete/${customer.id}" method="post">
                    <button type="submit">Delete</button>
                </form>
            </td>
        </tr>
        </c:forEach>
    </table>
</div>   
</body>
</html>

Here is my edit_customer.jsp:这是我的 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>

Here is my CustomerController.java:这是我的 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;
    }



}

Here is my CustomerService.java这是我的 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);
    }

}

Here is my entity class Customer.java这是我的实体类 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;
    }

}

Mysql port should be 3306 instead of 3308 (persistence.xml) Mysql 端口应该是 3306 而不是 3308 (persistence.xml)

Your CustomerRepository interface must be marked with @Repository您的 CustomerRepository 接口必须标有@Repository

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>

I'm sorry for wasting your time on this but I have solved my issue.很抱歉在此浪费您的时间,但我已经解决了我的问题。 The cause of it was my mapping.原因是我的映射。 I should have done /edit/save in my requestmapping.我应该在我的请求映射中完成 /edit/save 。 But I really appreciate you guys helping me.但我真的很感谢你们帮助我。 Thanks a lot非常感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM