Struts 2 s:if 与 getText
Struts 2 s:if with getText
我想看看消息包中是否有消息密钥。所以我在下面进行了尝试,但其中 none 成功了:
<s:if test=" getText('site.message') == 'site.message'">
<s:if test=" getText('site.message').equals('site.message')">
<s:if test=" %{getText('site.message').equals('site.message')}">
但是这个变通方法工作正常:
<s:text name="site.message" var="test"/>
<s:if test=" #test=='site.message'">
Please consider: <s:text name="site.message"/>
</s:if>
那么是否可以省略#test变量并在s:if
中完成所有工作
如果您将 <s:text>
的正文而不是 <s:if>
标记的正文用于默认消息,则这是可能的。来自文档页面
If the named message is not found in a property file, then the body of the tag will be used as default message. If no body is used, then the stack will be searched, and if a value is returned, it will written to the output. If no value is found on the stack, the key of the message will be written out. Then you could use another key to set the default message.
等价代码
<s:text name="site.message"><s:text name="error.message"/></s:text>
还要考虑 getText(String aTextName, String defaultValue)
的默认值。
Gets a message based on a key, or, if the message is not found, a supplied default value is returned.
那你就可以使用
<s:if test="getText('site.message','') != ''">
Please consider: <s:text name="site.message"/>
</s:if>
<s:else>
<s:text name="error.message"/>
</s:else>
检查密钥
Checks if a message key exists.
<s:if test="hasKey('site.message')">
Please consider: <s:text name="site.message"/>
</s:if>
<s:else>
<s:text name="error.message"/>
</s:else>
我想看看消息包中是否有消息密钥。所以我在下面进行了尝试,但其中 none 成功了:
<s:if test=" getText('site.message') == 'site.message'">
<s:if test=" getText('site.message').equals('site.message')">
<s:if test=" %{getText('site.message').equals('site.message')}">
但是这个变通方法工作正常:
<s:text name="site.message" var="test"/>
<s:if test=" #test=='site.message'">
Please consider: <s:text name="site.message"/>
</s:if>
那么是否可以省略#test变量并在s:if
如果您将 <s:text>
的正文而不是 <s:if>
标记的正文用于默认消息,则这是可能的。来自文档页面
If the named message is not found in a property file, then the body of the tag will be used as default message. If no body is used, then the stack will be searched, and if a value is returned, it will written to the output. If no value is found on the stack, the key of the message will be written out. Then you could use another key to set the default message.
等价代码
<s:text name="site.message"><s:text name="error.message"/></s:text>
还要考虑 getText(String aTextName, String defaultValue)
的默认值。
Gets a message based on a key, or, if the message is not found, a supplied default value is returned.
那你就可以使用
<s:if test="getText('site.message','') != ''">
Please consider: <s:text name="site.message"/>
</s:if>
<s:else>
<s:text name="error.message"/>
</s:else>
检查密钥
Checks if a message key exists.
<s:if test="hasKey('site.message')">
Please consider: <s:text name="site.message"/>
</s:if>
<s:else>
<s:text name="error.message"/>
</s:else>