由于隐藏的输入,带有复选框的 Thymeleaf Spring 表单会生成嘈杂的 url

Posted

技术标签:

【中文标题】由于隐藏的输入,带有复选框的 Thymeleaf Spring 表单会生成嘈杂的 url【英文标题】:Thymelaf Spring forms with checkboxes generate noisy urls due to hidden inputs 【发布时间】:2013-09-16 19:22:09 【问题描述】:

Thymeleaf Spring 在使用带有复选框的表单时创建隐藏输入,根据 http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#checkbox-fields

以 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

我怎样才能做到这一点?

【问题讨论】:

【参考方案1】:

设法让它与拦截表单提交并删除所有隐藏的“_filters”输入的javascript hack一起工作:

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>

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于由于隐藏的输入,带有复选框的 Thymeleaf Spring 表单会生成嘈杂的 url的主要内容,如果未能解决你的问题,请参考以下文章

验证隐藏的多复选框/输入字段

在 Thymeleaf 中禁用 CSRF 隐藏输入

Thymeleaf - 如何有条件地将选中的属性添加到输入

带有复选框的表单输入字段,反之亦然 - 缺少第一个值

Vue - 带有多个复选框的输入

显示/隐藏带有匹配复选框的内容