如何在Thymeleaf页面获取<input>参数并携带数据重定向至指定链接
问题描述
我基于SpringBoot开发了微服务,能读取Excel表单数据并展示为表格,该功能运行正常。我已自行验证Elasticsearch请求可正常工作,现在需要在Thymeleaf页面实现以下功能:在搜索框输入搜索内容后,点击“Submit”按钮,获取输入的搜索参数并携带该参数重定向至其他链接。以下是我的Thymeleaf页面代码:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Freight Masters LLC Agents</title> <style> /*** COLORS ***/ /*** DEMO ***/ html, body { height: 100%; margin: 0; } body { background: #F4B942; font: 13px monospace; color: #232e57; } p { margin-top: 30px; } .cntr { display: table; width: 100%; height: 100%; } .cntr .cntr-innr { display: table-cell; text-align: center; vertical-align: middle; } /*** STYLES ***/ .search { display: inline-block; position: relative; height: 35px; width: 35px; box-sizing: border-box; margin: 0px 8px 7px 0px; padding: 7px 9px 0px 9px; border: 3px solid #232e57; border-radius: 25px; transition: all 300ms ease; cursor: text; } .search:after { content: ""; position: absolute; width: 3px; height: 20px; right: -5px; top: 21px; background: #232e57; border-radius: 3px; transform: rotate(-45deg); transition: all 300ms ease; } .search.active, .search:hover { width: 300px; margin-right: 0px; } .search.active:after, .search:hover:after { height: 0px; } .search input { width: 100%; border: none; box-sizing: border-box; font-family: Helvetica; font-size: 15px; color: inherit; background: transparent; outline-width: 0px; } .button { width: 180px; height: 45px; font-family: 'Roboto', sans-serif; font-size: 11px; text-transform: uppercase; letter-spacing: 2.5px; font-weight: 500; color: #000; background-color: #fff; border: none; border-radius: 45px; box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.1); transition: all 0.3s ease 0s; cursor: pointer; outline: none; } .button:hover { background-color: #232e57; box-shadow: 0px 15px 20px #0f152b66; color: #fff; transform: translateY(-7px); } </style> </head> <body> <div class="cntr"> <div class="cntr-innr"> <label style="margin-bottom: 32px" class="search" for="inpt_search"> <input th:name="searchParams" id="inpt_search" type="text"/> </label><br> <button class="button">Submit</button> </div> </div> <script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script> <script> $("#inpt_search").on('focus', function () { $(this).parent('label').addClass('active'); }); $("#inpt_search").on('blur', function () { if($(this).val().length == 0) $(this).parent('label').removeClass('active'); }); </script> </body> </html>
解决方案
提供两种实现方式,可根据需求选择:
方式1:使用Thymeleaf表单提交(无额外JS依赖)
利用原生表单机制实现参数携带跳转,是最简洁的方案:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Freight Masters LLC Agents</title> <style> /*** 保留原有样式 ***/ /*** COLORS ***/ /*** DEMO ***/ html, body { height: 100%; margin: 0; } body { background: #F4B942; font: 13px monospace; color: #232e57; } p { margin-top: 30px; } .cntr { display: table; width: 100%; height: 100%; } .cntr .cntr-innr { display: table-cell; text-align: center; vertical-align: middle; } /*** STYLES ***/ .search { display: inline-block; position: relative; height: 35px; width: 35px; box-sizing: border-box; margin: 0px 8px 7px 0px; padding: 7px 9px 0px 9px; border: 3px solid #232e57; border-radius: 25px; transition: all 300ms ease; cursor: text; } .search:after { content: ""; position: absolute; width: 3px; height: 20px; right: -5px; top: 21px; background: #232e57; border-radius: 3px; transform: rotate(-45deg); transition: all 300ms ease; } .search.active, .search:hover { width: 300px; margin-right: 0px; } .search.active:after, .search:hover:after { height: 0px; } .search input { width: 100%; border: none; box-sizing: border-box; font-family: Helvetica; font-size: 15px; color: inherit; background: transparent; outline-width: 0px; } .button { width: 180px; height: 45px; font-family: 'Roboto', sans-serif; font-size: 11px; text-transform: uppercase; letter-spacing: 2.5px; font-weight: 500; color: #000; background-color: #fff; border: none; border-radius: 45px; box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.1); transition: all 0.3s ease 0s; cursor: pointer; outline: none; } .button:hover { background-color: #232e57; box-shadow: 0px 15px 20px #0f152b66; color: #fff; transform: translateY(-7px); } </style> </head> <body> <div class="cntr"> <div class="cntr-innr"> <!-- 替换目标路径为你的实际跳转地址 --> <form th:action="@{/your-target-path}" method="get"> <label style="margin-bottom: 32px" class="search" for="inpt_search"> <input th:name="searchParams" id="inpt_search" type="text" required/> </label><br> <!-- 设置按钮为提交类型 --> <button type="submit" class="button">Submit</button> </form> </div> </div> <script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script> <script> $("#inpt_search").on('focus', function () { $(this).parent('label').addClass('active'); }); $("#inpt_search").on('blur', function () { if($(this).val().length == 0) $(this).parent('label').removeClass('active'); }); </script> </body> </html>
关键改动说明
- 用
<form>包裹搜索组件,th:action="@{/your-target-path}"替换为实际需要跳转的后端接口或页面路径(如/es/search) - 将按钮类型设为
submit,点击后自动提交表单 - 输入框添加
required属性,强制用户输入内容后才能提交(可选,可按需移除) - 使用
get请求,参数会自动拼接到URL,格式为/your-target-path?searchParams=用户输入内容
方式2:使用jQuery监听点击事件跳转
若不想用表单,可通过JS手动获取参数并跳转:
仅修改页面中的JS部分:
$("#inpt_search").on('focus', function () { $(this).parent('label').addClass('active'); }); $("#inpt_search").on('blur', function () { if($(this).val().length == 0) $(this).parent('label').removeClass('active'); }); // 新增按钮点击处理逻辑 $(".button").on('click', function() { const searchValue = $("#inpt_search").val().trim(); if(searchValue) { // 替换为你的目标路径,encodeURIComponent处理特殊字符避免编码异常 window.location.href = `/your-target-path?searchParams=${encodeURIComponent(searchValue)}`; } else { // 可选:提示用户输入内容 alert("请输入搜索内容"); } });
关键说明
encodeURIComponent用于编码搜索参数,避免空格、中文等特殊字符导致URL异常- 无需修改页面结构,保留原有按钮和输入框即可
- 若跳转外部链接,直接替换
/your-target-path为完整URL即可
内容的提问来源于stack exchange,提问作者Kamal Muradov
相关产品推荐
相关产品推荐

