具有复选框的Thymeleaf Spring表单由于隐藏的输入而生成嘈杂的URL

时间:2013-09-12 12:00:04

标签: java html spring-mvc thymeleaf

根据{{​​3}}

,使用带复选框的表单时,Thymeleaf Spring会创建隐藏的输入

将表单作为GET方法提交时生成的url有很多噪音:

/search?q=stuff&_filters=on&filters=Foo&_filters=on&filters=Bar&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on&_filters=on

我想看到的网址是

/search?q=stuff&filters=Foo&filters=Bar

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

管理以使用javascript黑客拦截表单提交并删除所有隐藏的'_filters'输入:

function interceptFilterForm(form){
    var inputs = form.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; ++i) {
        var input = inputs[i];
        if (input.type == "hidden" && input.name == "_filters") {
            input.parentNode.removeChild(input);
        }
    }

    form.submit();
}

表格:

<form th:action="@{/search}" method="GET" th:object="${searchForm}" onsubmit="interceptFilterForm(this);">
  <input type="hidden" name="q" th:value="${searchForm.q}" />
  <input type="submit" value="Filter" ></input>

  <li th:each="facet : ${results.facets}" >
    <input type="checkbox" name="filters" th:value="${facet.fullPath}" th:field="*{filters}" />
  </li>  
</form>

答案 1 :(得分:1)

您可以使用后期请求。问题是您需要绑定信息,否则您无法知道复选框是否未选中或未显示。