从 Thymeleaf 模板重定向页面

syv*_*syv 2 thymeleaf

如何从 Thymeleaf 重定向页面(我有如下 JSP 代码)

out.println("REDIRECT=http://www.example.com/api/response?id="+id)
Run Code Online (Sandbox Code Playgroud)

Thymeleaf 中的等价物是什么?我想从模板中做到这一点。

Met*_*ids 6

Thymeleaf 不提供任何从其模板中重定向到另一个页面的机制——我也不建议这样做(因为这可能应该在控制器级别处理)。

话虽如此,可以使用 javascript 来做到这一点。像这样的东西:

<script th:inline="javascript">
    window.location.href = 'http://www.example.com/api/response?id=' + [[${id}]];
</script>
Run Code Online (Sandbox Code Playgroud)

或者可能是元刷新标签。

<meta http-equiv="refresh" th:attr="content=${'5; url=http://example.com/api/response?id=' + id}" />
Run Code Online (Sandbox Code Playgroud)