0

I'm pretty new to hibernate and Spring MVC. I was trying hibernate and Spring MVC on a Demo web application. 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". 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)

<%@ 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:

<%@ 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:

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

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

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;
    }

}
  • Can you add logging around `service.save(customer);`? There might be an issue. Also check your `"redirect:/"`, is it valid? – Boris Feb 07 '20 at 14:04
  • @Boris Yes, I have used "redirect:/" at my new customer add page too so there is no problem there. But I will look at logging. I'm sorry I may respond slowly because I have never used logging before. – Busra Sunar Feb 07 '20 at 14:27
  • Please push your project to gihub and share it here. It will be easier to assist you – AchillesVan Feb 07 '20 at 14:45
  • @Boris I'm sorry I couldn't do the logging, can you give me an example – Busra Sunar Feb 10 '20 at 06:30
  • @Achille_vanhoutte https://github.com/BusraSunar/Hibernate here is the link to my GitHub repository. – Busra Sunar Feb 10 '20 at 06:41
  • Just add `System.out.println("saving customer " + customer);` before `service.save(customer);` and `System.out.println("saved customer");` after. – Boris Feb 10 '20 at 10:15
  • @Boris oh that's what you meant lol I'm sorry. Well, I did the logging and the problem is that it's not even going into the save method. – Busra Sunar Feb 10 '20 at 12:25
  • Can you print all HTTP endpoints as per this [answer](https://stackoverflow.com/a/43543204/3301492)? We have to find out if the `save` endpoint gets created. – Boris Feb 10 '20 at 13:01
  • can you post the stacktrace if any? – Brooklyn99 Feb 10 '20 at 20:21
  • @Boris, at last, I have done printing the endpoints. I will post it as an answer because it is too long for the comments. – Busra Sunar Feb 11 '20 at 08:17
  • @SantosshKumhar after I get the bad request there is nothing in my console. So there is no error I can post from my console. – Busra Sunar Feb 11 '20 at 08:26
  • it looks like you are posting something else in the edit action, but your controller is mapped to wait only an `Long id`. Is the delete action working ? can you cross check in your browser which post request are u sending when pressing edit? – AntJavaDev Feb 11 '20 at 08:41
  • @AntJavaDev thanks for your answer but I have solved it. – Busra Sunar Feb 11 '20 at 09:25

2 Answers2

0

Mysql port should be 3306 instead of 3308 (persistence.xml)

Your CustomerRepository interface must be marked with @Repository

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • I'm using wamp server so I use 3308 port. And i still get 400 Bad request when hitting save on edit page – Busra Sunar Feb 10 '20 at 12:18
  • I have no knowlegde of wamp. I assume your DB is Mysql according to your persistence.xml file. Anyways, it worked for me with Mysql and Tomcat 8. https://github.com/Georges73/hibernate – AchillesVan Feb 10 '20 at 12:25
  • I looked at your GitHub repository but I don't see a lot of difference I think you changed persistence.xml but that didn't change anything for me. Well, thanks a lot I really appreciate it. – Busra Sunar Feb 10 '20 at 12:33
0

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. But I really appreciate you guys helping me. Thanks a lot