关于struts2里面s:set 和s:if的问题,
<s:set name="seasonList" value="{'Spring',Summer,'Autumn','Winter'}" />
这么是正确的,但是我们set一个map,这样就不行了:
<s:set name="monthMap" value='#{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May",
"6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}' />
奇怪的是,前面必须要加一个#,感觉很奇怪,我也不知道为什么,希望研究过源码的给一个解答。
<span class="s">
<s:set name="monthMap" value="#{'1':'Jan','2':'Feb','3':'Mar','4':'Apr','5':'May','6':'Jun','7':'Jul','8':'Aug','9':'Sep','10':'Oct','11':'Nov','12':'Dec'}" />
<s:set name="currentMonth" value="%{ chargeMonth }" />
<select name="chargeMonth" class="styled">
<option value=" ">-- Select Month --</option>
<s:iterator value="#monthMap" id="month">
<option value="<s:property value='key'/>"
<s:if test="%{key==#currentMonth}">
selected="selected"
</s:if>
>
<s:property value='value'/>
</option>
</s:iterator>
</select>
发现这样不行,后来经过我反复的测试,当选择10,11,12时候能正确的将selected加上,但是选择前面的数字怎么都不行,后来经我观察,前面我将字符串用的是’,估计在后台反射的时候将其类型错误的转化成char类型了,但是估计当判断length大于1的时候,将其转化成字符串了,所以能够正确。不知道我这么猜的对不对,贴出来分享一下吧,以后希望这样的问题不能在犯了。
我修改为下面的就可以了:
<s:set name="monthMap" value='#{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May","6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}' />
<s:set name="currentMonth" value="%{chargeMonth}" />
<select name="chargeMonth" class="styled">
<option value=" ">-- Select Expiration Month --</option>
<s:iterator value="#monthMap" id="month">
<option value="<s:property value='key'/>"
<s:if test="%{key==#currentMonth}">
selected="selected"
</s:if>
>
<s:property value='value'/>
</option>
</s:iterator>
</select>