th:field不从th:value中获取数据

时间:2018-02-13 15:21:21

标签: java spring spring-mvc spring-boot thymeleaf

好的,所以,基本上我必须使用带有此值的输入才能将其发送到表单帖子以删除我想要的行...

<input type="text" th:field="*{id}" value="${customer.id}" />

但是会发生什么?嗯..财产th:field="*{id}"没有正常工作,你可以在这里看到:

Screenshot

你可以看到的数字3是提交按钮,因为你看到它正确地抓取了值,但无论我做什么,每次我尝试删除一些东西,它不会做,而不是,不是逻辑,因为在一些愚蠢的输入上硬编码html上的id并且它有效..

这是html代码:

<form th:action="@{/DeleteCustomer}"
                                th:object="${customersdelete}" method="post">
                                <span th:value="${customer.id}"></span>

                                <span th:field="*{id}" value="${customer.id}"></span>

                                <input type="text" 
                                th:field="*{id}" value="${customer.id}" />

<!--                                <input type="text" th:value="${customer.id}" th:field="*{id}" /> -->

                                <input
                                    type="submit" name="btnInsert" class="btn btn-primary"
                                    value="Delete" th:value="${customer.id}" />
                            </form>

CONTROLLER

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

        System.err.println("ID =" + Customers.getId());
        customersService.removeCustomer(Customers.getId());
        customersService.listAllCustomers();
        mav.setViewName("redirect:/" + MAIN_VIEW);
        return mav;

    }

错误:

ID =0
Hibernate: select customers0_.id as id1_0_0_, customers0_.address as address2_0_0_, customers0_.course as course3_0_0_, customers0_.firstname as firstnam4_0_0_, customers0_.lastname as lastname5_0_0_ from customers customers0_ where customers0_.id=?
2018-02-13 11:17:25.924  INFO 10812 --- [nio-8080-exec-9] c.p.s.component.RequestTimeInterceptor   : --REQUEST URL:'http://localhost:8080/DeleteCustomer'-- TOTAL TIME: '8'ms
2018-02-13 11:17:25.927 ERROR 10812 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.EmptyResultDataAccessException: No class com.project.springinventory.entity.Customers entity with id 0 exists!] with root cause

org.springframework.dao.EmptyResultDataAccessException: No class com.project.springinventory.entity.Customers entity with id 0 exists!

我尝试过但不适合我的事情:

                            <input type="text" 
                                th:field="*{id}" value="${customer.id}" />



                            <input type="text" 
                                th:field="*{id}" th:value="${customer.id}" />

                    <input type="hidden" 
                                th:field="*{id}" th:value="${customer.id}" />

&lt; =当id为4或

时显示0

检查对象,检查所有内容。我不知道该怎么做。

我所看到的,如果我删除th:field =“* {id}”id将加载到输入但不会发送到帖子中,尝试使用不同的名称创建另一个对象以避免数据或某事的冲突仍然相同..

    <tr th:each="customer:${customers}">
                        <th><span th:value="${customer.id}"></span></th>
                        <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">
                                <button class="btn btn-warning">
                                    <span class="glyphicon glyphicon-pencil" aria-hidden="true">Show</span>
                                </button>
                            </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="${customersdelete}" method="post">
                                <span th:value="${customer.id}"></span>

                                <span th:field="*{id}" value="${customer.id}"></span>

                                <input type="text" 
                                th:field="*{id}" value="${customer.id}" />

<!--                                <input type="text" th:value="${customer.id}" th:field="*{id}" /> -->

                                <input
                                    type="submit" name="btnInsert" class="btn btn-primary"
                                    value="Delete" th:value="${customer.id}" />
                            </form>

1 个答案:

答案 0 :(得分:0)

th:field设置输入标记的名称,值和id。 (覆盖您手动设置的一些值。)

由于您的th:object被定义为${customersdelete},因此*{id}表达式${customersdelete.id} - 这与您尝试使用的不同  th:value="${customer.id}"。表达式th:value="${customer.id}"完全被忽略。

您应该在表单之前在控制器中设置customersdlete的id,而不是使用th:value="${customer.id}"