【问题标题】:Spring mvc DELETE WITH classic POST (@PostMapping) not workingSpring mvc DELETE WITH Classic POST(@PostMapping)不起作用
【发布时间】:2018-02-13 09:23:26
【问题描述】:

您好,我正在使用 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 获取所有字段 有没有一种方法可以在不改变的情况下使用这种方法? (使用表格)

查看

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

控制器

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

''

}

实际上一切正常,删除除外

【问题讨论】:

标签: java spring spring-mvc thymeleaf


【解决方案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;
}

【讨论】:

  • org.springframework.beans.NotReadablePropertyException:bean 类 [java.util.ArrayList] 的无效属性 'id':Bean 属性 'id' 不可读或具有无效的 getter 方法:是否返回getter 的类型是否与 setter 的参数类型匹配?
  • 天啊..它绑定了arraylist..请将th:object更改为th:object="${customer}我省略了s
  • org.springframework.beans.NotReadablePropertyException:bean 类 [java.util.ArrayList] 的无效属性 'id':Bean 属性 'id' 不可读或具有无效的 getter 方法:是否返回getter 的类型是否匹配 setter 的参数类型?一样
  • 顺便说一句,我正在使用 th:object={customers} 因为我的实体是这样的,每个周期中的客户变量仅在 html 中用于“绘制”数据,这里是实体,如果你需要更多类似的服务和一些东西,让我知道我会带给你的 :) codepaste.net/4auewu
猜你喜欢
  • 1970-01-01
  • 2014-01-25
  • 2017-05-13
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 2018-10-05
相关资源
最近更新 更多