【问题标题】:th:field Not grabbing data from th:valueth:field 不从 th:value 获取数据
【发布时间】:2018-07-24 00:23:05
【问题描述】:

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

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

但是会发生什么?嗯.. 属性th:field="*{id}" 没有发挥作用,您可以在此处看到:

您可以看到的数字 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>

控制器

@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}" />

检查对象,检查一切。我不知道该怎么办。

我所看到的,如果我删除 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>

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 但是如果我使用相同的对象客户呢?我试过了,但没有用,这就是为什么我创建了另一个对象,你可以给我什么建议?
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多