<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
Springboot
如何为 th:action &#34; action url&#34;构建表达式通知?网址取决于&#34; name&#34; 变量。
我是这样尝试的,但它不起作用:
<form method="get"
th:action="@{'/' + {baseUrl}(baseUrl=${#strings.isEmpty(name) ? '' : 'user/find/'})}">
<input type="text" th:value="${name}"/>
<button type="submit">Find</button>
<button type="button"
th:classappend="${#strings.isEmpty(name)}?'hidden':''"
onclick="clearSearch()">X</button>
</form>
我试过这个,也不行:
<form th:with="baseUrl=(${#strings.isEmpty(name) ? '/' : '/user/find/'})"
th:action="@{${baseUrl}}" method="get">
答案 0 :(得分:0)
我只是写一个JS代码。
$(document).ready(function () {
var searchForm = $(this).find('#searchForm');
searchForm.on('click', '#searchButton', function (event) {
var form = $('#searchForm');
var name = $('#searchInputName').val();
var addUrl = (name === '') ? '/' : '/user/find/';
form.attr('action', addUrl);
});
});
之后我意识到我可以在我的控制器中实现它更简单:)
@GetMapping("/user/find/")
public String getNextPage(Model model, Pageable pageable,
@RequestParam(name = "name") String name) {
if (name.isEmpty()) return "redirect:/";
....
}