Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts 2 dynamic variables

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 will return a dynamic value that retrieved from database. For example, if the value is NINE then number should be @xxx.CommonConstant@NINE

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)}" /> 

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)}')}" />

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>

or

<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?

like image 422
Zulhalimi Anuar Muhammad Avatar asked May 23 '13 11:05

Zulhalimi Anuar Muhammad


1 Answers

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>
like image 95
Aleksandr M Avatar answered Sep 21 '22 07:09

Aleksandr M