本文介绍了我可以在Spring Boot应用程序中从Thymeleaf表发出HTTP POST请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个简单的Spring Boot应用程序中有一个Thymeleaf模板。

 < p>有< span th:text =$ {#lists.size(人)}>< /跨度>人:其中/ p为H. 
< tr>
< th> ID< / th>
< th>名称< / th>
< th>地址< / th>
< th>电话< / th>
< th>电子邮件< / th>
< th>操作< / th>
< / tr>
< tr th:each =person:$ {persons}>
< td th:text =$ {person.personId}>< / td>
< td th:text =$ {person.name}>< / td>
< td th:text =$ {person.address}>< / td>
< td th:text =$ {person.telephone}>< / td>
< td th:text =$ {person.email}>< / td>
< td>< a href =#data-th-href =@ {/ edit(personId = $ {person.personId})}>编辑< / a> |
< a href =#data-th-href =@ {/ delete(personId = $ {person.personId})}>删除< / a>< / td>
< / tr>
< / table>

我想根据表格中的最后一个单元启用编辑和删除功能。但目前这两个请求都是针对HTTP GET的。这对编辑而言很好,因为从服务器获取某人的详细信息以进行编辑,但是由于服务器上的数据更改,删除应该会触发POST请求。



有没有人知道Thymeleaf是否允许每行表的POST请求?或者我必须每行写一个简单的HTML表单?



GET表单当前是:



<$ p $ < code>< td>< a href =#data-th-href =@ {/ edit(personId = $ {person.personId})}>编辑< / a>

< form method =getth:action =@ {/ edit(personId = $ {person.personId})}>
< button type =submitname =submitvalue =value>编辑< / button>
< / form>
< / td>

在哪里有链接和测试表单。



要调用的控制器方法是:

  //获取一个Person。 
@RequestMapping(value =/ edit,method = RequestMethod.GET)
public String getEditPerson(@RequestParam(personId)String personId,
Model model){
logger.info(PersonController.class.getName()+.getEditPerson()方法调用。);

Person person = personDAO.get(Integer.parseInt(personId));
model.addAttribute(person,person);

//设置视图。
返回/ edit;




调用GET按钮版本时的错误是:

 白标签错误页面

此应用程序没有明确的映射/错误,所以您可以看到作为后备。

Sun Jul 24 00:26:16 BST 2016
出现意外错误(type = Bad Request,status = 400)。
必填字符串参数'personId'不存在

我使用GET触发编辑因为除了personId以外,没有数据发送到服务器。没有数据库操作,所以它应该是一个GET。

解决方案

您正在使用链接,我不认为这是可能的,您需要使用一个表单,您可以指定要使用的POST方法。



在下面的示例中,使用<按钮> ; 而不是< a> 元素,但它可以工作,您需要做的唯一事情就是使用CSS设置样式按钮看起来像你的链接

 < form method =POSTth:action =@ {/ edit(personId = $ { PERSON.PERSONID})}> 
< button type =submitname =submitvalue =valueclass =link-button>这是一个发送POST请求的链接< / button>
< / form>

现在在您的代码中应该是这样的

 < tr th:each =person:$ {persons}> 
< td th:text =$ {person.personId}>< / td>
< td th:text =$ {person.name}>< / td>
< td th:text =$ {person.address}>< / td>
< td th:text =$ {person.telephone}>< / td>
< td th:text =$ {person.email}>< / td>
< td>
< form method =POSTth:action =@ {/ edit(personId = $ {person.personId})}>
< button type =submitname =submitvalue =valueclass =link-button> EDIT< / button>
< / form> |
< form method =POSTth:action =@ {/ delete(personId = $ {person.personId})}>
< button type =submitname =submitvalue =valueclass =link-button> DELETE< / button>
< / form>
< / td>
< / tr>

编辑



Java代码,在控制器中,您期望personId不是PathVariable,但是作为RequestParam,在这种情况下,
表单应该具有该值...



 < form method =POSTth:action = @ {/编辑}> 
< input type =hiddenname =personidid =personIdth:value =$ {person.personId}/>
< button type =submitname =submitvalue =valueclass =link-button>这是一个发送POST请求的链接< / button>
< / form>

另请注意,我将表单的操作更改为just / edit,因为它的控制器外观像


I have a Thymeleaf template in a simple Spring Boot application. The template contains a list in a table as follows:

<p>There are <span th:text="${#lists.size(persons)}"></span> people:</p>
    <table th:if="${not #lists.isEmpty(persons)}" border="1">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Address</th>
            <th>Telephone</th>
            <th>Email</th>
            <th>Actions</th>
        </tr>
        <tr th:each="person : ${persons}">
            <td th:text="${person.personId}"></td>
            <td th:text="${person.name}"></td>
            <td th:text="${person.address}"></td>
            <td th:text="${person.telephone}"></td>
            <td th:text="${person.email}"></td>
            <td><a href="#" data-th-href="@{/edit(personId=${person.personId})}">Edit</a> |
                <a href="#" data-th-href="@{/delete(personId=${person.personId})}">Delete</a></td>
        </tr>
    </table>

I want to enable edit and delete functionality as per the last cell in the table. But at the moment both requests are for HTTP GET. That is fine for edit where a person's details are fetched from the server for editing, but delete should trigger a POST request because of the data changes on the server.

Does anyone know if Thymeleaf allow a POST request per row of a table? Or do I have to write a simple HTML form per row?

The GET form is currently:

<td><a href="#" data-th-href="@{/edit(personId=${person.personId})}">Edit</a>
                    <!--a href="#" data-th-href="@{/delete(personId=${person.personId})}">Delete</a></td-->

                    <form method="get" th:action="@{/edit(personId=${person.personId})}">
                        <button type="submit" name="submit" value="value">Edit</button>
                    </form>
                </td>

Where I have a link and a form for testing.

The controller method to be called is:

// Gets a Person.
    @RequestMapping(value="/edit", method=RequestMethod.GET)
    public String getEditPerson(@RequestParam("personId") String personId,
                                 Model model) {
        logger.info(PersonController.class.getName() + ".getEditPerson() method called.");

        Person person = personDAO.get(Integer.parseInt(personId));
        model.addAttribute("person", person);

        // Set view.
        return "/edit";

    }

The error when the button version of GET is called is:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jul 24 00:26:16 BST 2016
There was an unexpected error (type=Bad Request, status=400).
Required String parameter 'personId' is not present`

I am using GET to trigger editing because no data is sent to the server here other than the personId. No database action is taken so it should be a GET.

解决方案

you are using Links and I don't think that is possible, you would need to use a form where you can specify the method POST to be used.

In the example below im using a <button> instead of a <a> element, but it will work, the only thing you need to do is to style your button with CSS to look like your links

<form method="POST" th:action="@{/edit(personId=${person.personId})}">
    <button type="submit" name="submit" value="value" class="link-button">This is a link that sends a POST request</button>
</form>

now in your code should look like this

<tr th:each="person : ${persons}">
    <td th:text="${person.personId}"></td>
    <td th:text="${person.name}"></td>
    <td th:text="${person.address}"></td>
    <td th:text="${person.telephone}"></td>
    <td th:text="${person.email}"></td>
    <td>
        <form method="POST" th:action="@{/edit(personId=${person.personId})}">
            <button type="submit" name="submit" value="value" class="link-button">EDIT</button>
        </form> |
        <form method="POST" th:action="@{/delete(personId=${person.personId})}">
            <button type="submit" name="submit" value="value" class="link-button">DELETE</button>
        </form>
    </td>
</tr>

EDIT

As you just shared you Java code, in the controller you are expecting the personId not as a PathVariable, but as a RequestParam,in that case your form should have that value...

edit your form and add the person id as follows.

<form method="POST" th:action="@{/edit}">
    <input type="hidden" name="personid" id="personId" th:value="${person.personId}" />
    <button type="submit" name="submit" value="value" class="link-button">This is a link that sends a POST request</button>
</form>

Notice also I changed the action of the form to be just /edit, as its what your controller looks like

这篇关于我可以在Spring Boot应用程序中从Thymeleaf表发出HTTP POST请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:26