如何在Spring的控制器中将元素id添加到ThymileAF模板的url中
我有一个Spring应用程序和服务器端渲染,使用Thymeleaf作为模板语言。 一个按钮向Spring中的控制器发送一个get或post请求,该请求将一些消息放入视图,并将其呈现到HTML文件中并发送回客户端。该消息应该是可选的。这就是为什么模板也必须能够在没有消息的情况下调用 下一步我希望客户端浏览器向下滚动到页面的部分,该部分显示此消息,这通常非常简单。您只需将元素的id附加到url,如下面的示例所示如何在Spring的控制器中将元素id添加到ThymileAF模板的url中,spring,thymeleaf,Spring,Thymeleaf,我有一个Spring应用程序和服务器端渲染,使用Thymeleaf作为模板语言。 一个按钮向Spring中的控制器发送一个get或post请求,该请求将一些消息放入视图,并将其呈现到HTML文件中并发送回客户端。该消息应该是可选的。这就是为什么模板也必须能够在没有消息的情况下调用 下一步我希望客户端浏览器向下滚动到页面的部分,该部分显示此消息,这通常非常简单。您只需将元素的id附加到url,如下面的示例所示 https://stackoverflow.com/#footer 在本例中,浏览器向
https://stackoverflow.com/#footer
在本例中,浏览器向下滚动到页面的页脚
下面是我试过的。不幸的是,它不是那样工作的。Spring/Thymeleaf尝试查找找不到的索引#messagebox模板。因此,将抛出/显示白标错误页面错误
Page.html
<section>
<h2>Form to send request</h2>
<form action="showmessage" method="get">
<input type="submit" value="Click for message">
</form>
</section>
<body>
<h1>Index Page</h1>
<div id="messagebox" th:fragment="message" th:with="optionalmessage=${optionalmessage}">
<p th:if="${optionalmessage!=null}">[[${optionalmessage}]]</p>
</div>
</body>
<div th:if="${msg}">
<div class="message" >
<p>[[${msg}]]</p>
</div>
</div>
src/main/resources/templates/index.html
<section>
<h2>Form to send request</h2>
<form action="showmessage" method="get">
<input type="submit" value="Click for message">
</form>
</section>
<body>
<h1>Index Page</h1>
<div id="messagebox" th:fragment="message" th:with="optionalmessage=${optionalmessage}">
<p th:if="${optionalmessage!=null}">[[${optionalmessage}]]</p>
</div>
</body>
<div th:if="${msg}">
<div class="message" >
<p>[[${msg}]]</p>
</div>
</div>
索引页
[[${optionalmessage}]]
您可以使用?id=value
在URL中添加id
在控制器@RequestMapping(“/path/{id}”)
要访问该变量,可以使用Flashmessages和重定向来解决该问题。html基本上保持不变。如果设置了消息属性,则可以渲染它 src/main/resources/templates/index.html
<section>
<h2>Form to send request</h2>
<form action="showmessage" method="get">
<input type="submit" value="Click for message">
</form>
</section>
<body>
<h1>Index Page</h1>
<div id="messagebox" th:fragment="message" th:with="optionalmessage=${optionalmessage}">
<p th:if="${optionalmessage!=null}">[[${optionalmessage}]]</p>
</div>
</body>
<div th:if="${msg}">
<div class="message" >
<p>[[${msg}]]</p>
</div>
</div>