您的位置:首页 > 编程语言 > Java开发

struts2 s标签 s:set 和 s:if

2010-01-12 14:01 274 查看
set标签是将某个值放到指定范围内, 比如说 student.teacher.parent.age 每次访问这个属性不仅性能低,而且代码可读性很差,为了解决这个问题,可以将这个值设置为一个新值,并且放入指定范围内
name 是必填属性,是重新生成的新变量的名字
scope 可选属性,指定新变量被放置的范围,可以接受application,session,request,page,action 这5个值 没有指定默认是Stack Context中
value 可选属性,指定变量的值 如果没有指定,使用ValueStack栈顶的值赋给新变量
id 可选属性,指定新元素的引用ID

下面是个例子:

<!-- 使用bean标签定义一个javaBean实例--!>
<s:bean name="lee.Person" id="p">
<s:param name="name" value="zhangsan"/>
<s:param name="age" value="29"/>
</s:bean>
将p放入默认范围内
<s:set value="#p" name="test"/>
<s:property value="#test.name"/> <br>
<s:property value="#test.age"/> <br>
将p放入application范围内。
<s:set value="#p" name="test" scope="application"/>
<s:property value="#attr.test.name"/> <br>
<s:property value="#attr.test.age"/> <br>
将p放入session范围内。
<s:set value="#p" name="test" scope="session"/>
${sessionScope.test.name} <br>
${sessionScope.test.age} <br>

这里发现一个struts2 s标签问题,<s:set value="jkadf1213" name="test"/> ${test}发现了获取不到值,value="'jkadf1213'" 如果是字符串的话,在双引号里面还要加单引号才能获取到值。

关于struts2里面s:set 和s:if的问题,

Java代码



<s:set name="seasonList" value="{'Spring',Summer,'Autumn','Winter'}" />

<s:set name="seasonList" value="{'Spring',Summer,'Autumn','Winter'}" />


这么是正确的,但是我们set一个map,这样就不行了:

Java代码



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


奇怪的是,前面必须要加一个#,感觉很奇怪,我也不知道为什么,希望研究过源码的给一个解答。

Java代码



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

<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的时候,将其转化成字符串了,所以能够正确。不知道我这么猜的对不对,贴出来分享一下吧,以后希望这样的问题不能在犯了。
我修改为下面的就可以了:

Java代码



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

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



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: