问题描述
我正在尝试使用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>
CommonConstant:
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 ...
}
任何人都可以提供帮助我?
Can anybody help me?
推荐答案
这似乎是一种解决方法,但你可以使用 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动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!