【问题标题】:thymeleaf how to get an input backing bean generated name attributethymeleaf 如何获取输入支持 bean 生成的名称属性
【发布时间】:2017-03-29 12:28:34
【问题描述】:

百里香是否可以检索输入的“生成”名称属性?

我需要创建一个 id 等于输入名称属性的 div。 输入名称通过 th:field 生成。

标记:

<input type="radio" id="winterTiresInstalled" th:field="*{winterTiresInstalled.value}" />

生成的标记:

<input id="fld_winterTires" type="radio" name="vehicle.winterTiresInstalled.value" />

我想要什么:

<div th:id="???" />

获得:

<div id="vehicle.winterTiresInstalled.value" />

那么,如何使用 thymleaf 检索生成的名称属性值“vehicle.winterTiresInstalled.value”?

我可以在 js 中做,但我更喜欢在我的模板中做。

谢谢。 大卫

【问题讨论】:

  • name属性好像是由类SpringInputRadioFieldAttrProcessor.doProcess方法生成的(String name = bindStatus.getExpression();)。我们如何检索 thymeleaf 中的 spring-mvc bindStatus ?
  • 使用 thymeleaf 2.1.4.RELEASE 和 spring-mvc 4.3.3.RELEASE

标签: spring-mvc thymeleaf


【解决方案1】:

现在用javascript解决了。页面加载后,我更改了 div 的 id。 我宁愿直接在我的模板中执行此操作,但无法找到使用 thymeleaf 执行此操作的方法。

【讨论】:

    【解决方案2】:

    th-attr 最适合这个用例。

    <a href="#" th:attr="data-id=${object.getId()}, data-name=${object.getName()}"/>
    

    上面的结果是这样的(未测试)

    <a href="#" data-id="420" data-name="user-link"/>
    

    【讨论】:

    • 我无权访问 getName 方法。我无法控制如何在我的输入中生成名称属性。它似乎是由 spring-mvc BindStatus 生成的。它使用支持 bean 路径来生成名称。我想用 thymeleaf 访问 bindStatus.getExpression()。
    猜你喜欢
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2012-03-15
    • 2013-08-03
    • 2013-04-02
    • 1970-01-01
    相关资源
    最近更新 更多