Spring mvc DELETE WITH经典POST(@PostMapping)无效

时间:2018-02-13 09:23:27

标签: java spring spring-mvc thymeleaf

我正在使用经典方法作为表单类型测试使用spring mvc的快速crud

错误就是这个

org.springframework.dao.EmptyResultDataAccessException: No class 
com.project.springinventory.entity.Customers entity with id 0 exists!
        at 
org.springframework.data.jpa.repository.support.SimpleJpaRepository.delete(SimpleJpaRepository.java:154) ~[spring-data-jpa-1.11.10.RELEASE.jar:na]

看起来对象没有收到任何东西

检查了一切,看起来不错,我不知道该怎么做

你可以看到我正在接收id并从DB获取所有字段 有没有一种方法我可以使用这种方法而不改变它? (使用表格)

Click here to view image

查看

<table class="table table-bordered">
	<tr>
		<th><span>Id</span></th>
		<th><span>Course</span></th>
		<th><span>Name</span></th>
		<th><span>Last Name</span></th>
		<th><span>Address</span></th>
		<th><span>Actions</span></th>
	</tr>
	<tr th:each="customer:${customers}">
		<th><span th:text="${customer.id}"></span></th>
		<th><span th:text="${customer.firstname}"></span></th>
		<th><span th:text="${customer.lastname}"></span></th>
		<th><span th:text="${customer.course}"></span></th>
		<th><span th:text="${customer.address}"></span></th>
		<th>
			<form th:action="@{/showCustomer}" th:object="${customers}" method="post">
				<input type="submit" name="btnInsert" class="btn btn-primary" value="Add" />
			</form>
			<form th:action="@{/UpdateCustomer}" th:object="${customers}" method="post">
				<button class="btn btn-warning">
					<span class="glyphicon glyphicon-pencil" aria-hidden="true">Edit</span>
				</button>
			</form>
			<form th:action="@{/DeleteCustomer}" th:object="${customers}" method="post">
				<input type="submit" name="btnInsert" class="btn btn-primary" value="Delete" />
			</form>
    	</th>
	</tr>
	<form action="#" th:action="@{/customermanagement}"	th:object="${CustomersAdd}" method="post">
		<tr>
			<td><input type="text" placeholder="Course"
				th:field="*{course}" /></td>
			<td><input type="text" placeholder="Name"
				th:field="*{firstname}" /></td>
			<td><input type="text" placeholder="Last Name"
				th:field="*{lastname}" /></td>
			<td><input type="text" placeholder="Address"
				th:field="*{address}" /></td>
			<td><input type="submit" name="btnInsert"
				class="btn btn-primary" value="Add" /></td>
		</tr>
		<tr>
			<td>
				<p th:if="${#fields.hasErrors('course')}" th:errors="*{course}">course
					has errors</p>
			</td>
			<td>
				<p th:if="${#fields.hasErrors('firstname')}"
					th:errors="*{firstname}">firstname has errors</p>
			</td>
			<td>
				<p th:if="${#fields.hasErrors('lastname')}"
					th:errors="*{lastname}">lastname has errors</p>
			</td>
			<td>
				<p th:if="${#fields.hasErrors('address')}"
					th:errors="*{address}">address has errors</p>
			</td>
		</tr>    
	</form>
</table>

CONTROLLER

@PostMapping("/DeleteCustomer")
        public ModelAndView DeleteUser(@ModelAttribute("customers") Customers 
        Customers) {
        ModelAndView mav = new ModelAndView();

        System.err.println("Deleting:");
        System.err.println("getId " + Customers.getId());
        System.err.println("getCourse " + Customers.getCourse().toString());
        System.err.println("getCourse " + Customers.getFirstname().toString());
        System.err.println("getLastname " + Customers.getLastname().toString());
        System.err.println("getAddress " + Customers.getAddress().toString());
        customersService.removeCustomer(Customers.getId());
        customersService.listAllCustomers();
        mav.setViewName("redirect:/" + MAIN_VIEW);
        return mav;

    }

SERVICE

    public interface CustomersService {

    public abstract List<Customers> listAllCustomers();
    public abstract Customers addCustomer(Customers customers);
    public abstract int removeCustomer(int  id);
    public abstract Customers updateCustomer(Customers customers);

}

服务实施

    package com.project.springinventory.services;

    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;

    import com.project.springinventory.entity.Customers;
    import com.project.springinventory.repository.CustomersJpaRepository;

    @Service("customersServiceImpl")
    public class CustomersServiceImpl implements CustomersService {

    @Autowired
    @Qualifier("customersJpaRepository")
    private CustomersJpaRepository customersJpaRepository;  

    @Override
    public List<Customers> listAllCustomers() {
        // TODO Auto-generated method stub
        return  customersJpaRepository.findAll();
    }

    @Override
    public Customers addCustomer(Customers customers) {
        // TODO Auto-generated method stub
        return customersJpaRepository.save(customers);
    }

    @Override
    public int removeCustomer(int id) {
        // TODO Auto-generated method stub
        customersJpaRepository.delete(id);
        return 0;
    }

    @Override
    public Customers updateCustomer(Customers customers) {
        // TODO Auto-generated method stub
        return customersJpaRepository.save(customers);
    }

''

}

从字面上看,一切都是DELETE

的例外

1 个答案:

答案 0 :(得分:1)

控制器将收到空的Customers,因为您没有将任何数据发送回控制器。 我希望你使用Thymeleaf作为模板引擎。

试试这个

<form th:action="@{/DeleteCustomer}" th:object="${CustomersDelete}" method="post">
  <input type="submit" name="btnInsert" class="btn btn-primary" value="Delete" />
 <input type="hidden" th:field="*{id}" value="${customer.id}"/> 
</form>

在这里,您将id发送回控制器,同时提交表单。

也很少有变化。

@GetMapping("/customermanagement")
    public ModelAndView View() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("customers", customersService.listAllCustomers());
        mav.addObject("CustomersDelete", new Customers());
        mav.setViewName(MAIN_VIEW);

        return mav;
    }



@PostMapping("/DeleteCustomer")
    public ModelAndView DeleteUser(@ModelAttribute("CustomersDelete") Customers Customers) {
        ModelAndView mav = new ModelAndView();

        System.err.println("Deleting:");
        System.err.println("getId " + Customers.getId());
        System.err.println("getCourse " + Customers.getCourse().toString());
        System.err.println("getCourse " + Customers.getFirstname().toString());
        System.err.println("getLastname " + Customers.getLastname().toString());
        System.err.println("getAddress " + Customers.getAddress().toString());
        customersService.removeCustomer(Customers.getId());
        customersService.listAllCustomers();
        mav.setViewName("redirect:/" + MAIN_VIEW);
        return mav;
}