【问题标题】:Struts2 textfield dynamic key attributeStruts2 textfield 动态键属性
【发布时间】:2016-04-04 20:10:23
【问题描述】:

我正在尝试重构我的代码,并且正在寻找一种可能性来动态设置 <s:textfield> 的关键属性。

所以我的代码如下所示:

<s:set name="type" value="%{process.commands[%{#counter}].type}"/>
<s:if test="%{#type.getLabel() == 'Start'}">
    <s:textfield name="process.commands[%{#counter}].statement" 
                  key="lbl.commandType.start"/>
</s:if>
<s:if test="%{#type.getLabel() == 'Stop'}">
    <s:textfield name="process.commands[%{#counter}].statement" 
                  key="lbl.commandType.stop"/>
</s:if>
<s:if test="%{#type.getLabel() == 'Check'}">
    <s:textfield name="process.commands[%{#counter}].statement" 
                  key="lbl.commandType.check"/>
</s:if>  

但我真正寻找的是这样的东西,所以它会在一行中:

key="lbl.commandType.'%{#type.getLabel()}'"

key="lbl.commandType.<s:property value='#type.getLabel()'/>"/>

但是这些都不起作用,我没有找到任何关于动态键属性的信息。有谁知道解决方案?

【问题讨论】:

    标签: jsp struts2 ognl struts-tags


    【解决方案1】:

    如果您想从 i18n 资源中获取字段标签,请使用 label 属性和 getText 方法从资源中实际检索值。

    <s:textfield name="process.commands[%{#counter}].statement" 
                 label="%{getText('lbl.commandType.' + #type.getLabel())}" />
    

    或者使用&lt;s:text&gt; 代替getText

    <s:text var="labelText" name="%{'lbl.commandType.' + #type.getLabel()}" />
    
    <s:textfield name="process.commands[%{#counter}].statement" label="%{#labelText}" />
    

    请注意,如果您的type 中有属性label 并带有适当的getter 和setter,那么您可以使用#type.label 而不是#type.getLabel()

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多