gpt4 book ai didi

java - Spring Thymeleaf 缩短 url (form - get) 参数

转载 作者:行者123 更新时间:2023-12-02 09:50:33 28 4
gpt4 key购买 nike

当前状态:

https://localhost:8443/workaround/?query=dasda&atn=s&filterSoftwareType=ANY ......

期望的状态: https://localhost:8443/workaround/ ?q=dasda&atn=s&fst=ANY.....

使用 q 代替 query 并使用 fst 代替 filterSoftwareType 来缩短 url

我的 thymeleaf html 看起来像这样,简短的示例:

<form action="#" class="card card-sm rounded"
method="get"
th:action="@{${T(com.bisciak.workaround.util.Utils).MAPPING_INDEX}}" th:object="${search}">

<div class="col">
<input class="form-control form-control-lg form-control-borderless"
placeholder="Search here"
style="padding-left: 1rem"
th:field="${search.query}" type="search"/>
</div> etc...

Controller :

  @GetMapping(value = Utils.MAPPING_INDEX, params = "atn=s")
public ModelAndView indexActionSearch(@ModelAttribute(name = "s") Optional<Search> search .....

搜索对象具有查询等属性,但我肯定不想重命名这些属性!通过我只想使用短版本 URL 的代码来命名将是非常可怕的。

有人知道如何做到这一点吗?我已经尝试过输入芽上的名称属性,但没有帮助:/。

我还想将内容保留在表单中,以便自动构建 url。我还想将其保留为获取而不是帖子,以便用户可以通过 URL 栏中的复制粘贴等轻松共享此链接。通过帖子,他不会看到这一点。

最佳答案

th:field 属性可以轻松地从 Java 对象构建表单,并且字段可以使用相同的名称回发,以便可以将值自动分配回 Java 对象,相同类型,在服务器上。

如果您想要不同的名称,那么您就没有将其用于其预期目的,因此请停止使用它。

如果您查看文档,即章节 7.2 Inputs ,您会看到 th:fields 的作用:

Let’s see now how to add an input to our form:

<input type="text" th:field="*{datePlanted}" />

... In this case (input[type=text]), the above line of code is similar to:

<input type="text" id="datePlanted" name="datePlanted" th:value="*{datePlanted}" />

因此,更改您的代码来执行此操作:

<form ... th:object="${search}">

<input ... name="q" th:value="*{query}"/>

您是否还需要 id="q" 由您决定。

请注意,如果您使用 th:value="${search.query}",则不需要 th:object="${search}"

关于java - Spring Thymeleaf 缩短 url (form - get) 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56348631/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com