2016-04-04 45 views
1

我想重构我的代码,我正在寻找动态设置<s:textfield>的关键属性的可能性。Struts2 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()'/>"/> 

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

回答

3

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

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

或者用<s:text>代替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()