问题描述
我正在尝试使用 set tag 在 Struts2 中创建一个动态变量
I'm trying to create a dynamic variable in Struts2 using set tag
<s:set var="myNum" value="numConst" />
<s:set var="number" value="%{getText('@xxx.CommonConstant@'+#myNum)}" />
numConst
将返回从数据库中检索到的动态值.例如,如果值为 NINE,则数字应为 @xxx.CommonConstant@NINE
numConst
will return a dynamic value that retrieved from database. For example, if the value is NINE then number should be @xxx.CommonConstant@NINE
我已经在我的 java 类中设置了值,以便 @xxx.CommonConstant@NINE
将返回 9
.
I have set the value in my java class so that @xxx.CommonConstant@NINE
will return 9
.
到目前为止,如果我使用,文本标签中的值可以毫无问题地显示
So far, the value can be displayed with no problem in text tag if I use
<s:text name="%{getText(#number)}" />
它会返回 9
但是当我尝试使用属性标签时它显示不正确
It will return 9
but it displayed incorrectly when I tried using property tag
<s:property value="%{getText(#number)}" />
<s:property value="%{#number}" />
<s:property value="#number" />
<s:property value="%{getText('%{getText(#number)}')}" />
以上所有示例都将给我的值为 @xxx.CommonConstant@NINE
.我尝试从属性标记中获取值的原因是因为我想复制有关如何显示值的正确方式,以便我可以在 if 标记中使用它们,如下例所示:
Which all of the above examples will give me the value as @xxx.CommonConstant@NINE
. The reason I try to get the value from property tag is because I want to copy the correct way on how to display the value so I can use them in if tag like below examples:
<s:if test="#number == 9">
do something
</s:if>
或
<s:if test="%{getText(#number)} == 9">
do something
</s:if>
通用常量:
package xxx;
public abstract class CommonConstant {
public static final int NINE = 9;
public static final int NINEONE = 91;
public static final double ADMIN_PGM = 1.4;
// ... omitted ...
}
有人可以帮我吗?
推荐答案
这似乎是一种解决方法,但您可以使用 attr
来评估字符串.
It seems like a workaround but you can use attr
to evaluate string.
<s:set var="myNum" value="numConst" />
<s:set var="number" value="'@xxx.CommonConstant@'+#myNum" />
<s:property value="#attr[#number]"/>
<s:if test="#attr[#number] == 9">
do something
</s:if>
这篇关于Struts 2 动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!