【问题标题】:How to transfer parameters in th:href in spring boot Thymeleaf?如何在春季启动Thymeleaf中的th:href中传输参数?
【发布时间】:2019-02-28 20:16:36
【问题描述】:

这些是我的简单 Thymeleaf 表格 HTML 文件和 Spring MVC 控制器代码。下面首先是我的表格图片。

我尝试制作一些 html 代码来在单击“编辑”或“删除”链接时将帖子 ID 值传输到查看代码,但我不知道该怎么做。这些是我的 Spring MVC 控制器代码和 view.html 代码。

@Controller
public class PostController {

    @Autowired
    private PostService postService;

    @RequestMapping("/posts/view/{id}")
    public String view(@PathVariable("id") Long id, Model model) {
        Post post = postService.findById(id);
        model.addAttribute("post", post);

        return "posts/view";
    }

还有,

<table id="blogTable" border="1" width ="1000" height="400" align = "center">
        <thead>
            <tr>
                <th>Post ID</th>
                <th>Post Title</th>
                <th>Post Content</th>
                <th>Date</th>
                <th>Author</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <tr th:each="post : ${posts}">
            <td th:text="${post.id}">Post ID</td>    
            <td th:text="${post.title}">Post Title</td>
            <td th:text="${post.body}">Post Content</td>
            <td th:text="${post.date}">Date</td>
            <!--  <td th:text="${post.auther.userName()}">Author</td> -->
            <td>
                <a href="posts/view.html" th:href="@{posts/view/post.id}">Edit</a><br/>  ==> How to transfer the post.id parameter to th:href?
                <a href="posts/view.html" th:href="@{posts/view/post.id}">Delete</a>  ==> How to transfer the post.id parameter to th:href?
            </td>
        </tr>
        </tbody>
     </table>

我是 HTML 和 Spring 的初学者。如何通过th:href 标签将 post.id 值放入视图 mvc 控制器?

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    documentation 中所述使用 th:href

    • th:href 是一个属性修饰符属性:一旦处理,它将计算要使用的链接 URL,并将标签的 href 属性设置为该 URL。
    • 我们可以对 URL 参数使用表达式(如您在 orderId=${o.id} 中所见)。所需的 URL 编码操作也会自动执行。
    • 如果需要多个参数,这些参数将用逗号分隔,例如 @{/order/process(execId=${execId},execType='FAST')}
    • URL 路径中也允许使用变量模板,例如 @{/order/{orderId}/details(orderId=${orderId})}

    例如(注意th:href 和从变量post 接收值的参数postId):

    <td>
         <a href="posts/view.html" th:href="@{posts/view/{postId}(postId=${post.id})}">Edit</a><br/>  ==> How to transfer the post.id parameter to th:href?
    </td>
    

    【讨论】:

      【解决方案2】:

      可以在括号中添加参数:

      &lt;a th:href="@{/index(param1='value1',param2='value2')}"&gt;

      Thymeleaf 将上述结果评估为:

      &lt;a href="/index?param1=value1,m2=value2"&gt;

      例如,假设我们将默认值设置为两个参数; name="Dracula" 和年龄 = 25。

      &lt;a th:href=@{/person(name=${name},age=${age})}&gt;&lt;/a&gt;

      &lt;a href="/person?name='Dracula',age='25'"&gt;&lt;/a&gt;

      【讨论】:

        【解决方案3】:

        我使用这种格式并且工作正常。

        <a th:href="@{${urlBase} + '/#!/pedido' + ${other.value}" target="_blank">Link</a>
        

        【讨论】:

          【解决方案4】:

          您可以尝试以下方法:

          <a href="posts/view.html" th:href="@{/posts/view/__${post.id}__}">Edit</a><br/>
          <a href="posts/view.html" th:href="@{/posts/view/__${post.id}__}">Delete</a>
          

          【讨论】:

          • 糟糕,抱歉我的错误
          • 只需点击向下箭头。
          猜你喜欢
          • 2017-04-15
          • 1970-01-01
          • 2017-07-28
          • 1970-01-01
          • 2021-05-06
          • 2020-02-23
          • 1970-01-01
          • 2015-05-15
          • 2020-02-02
          相关资源
          最近更新 更多