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>java
这里发现一个struts2 s标签问题,<s:set value="jkadf1213" name="test"/> ${test}发现了获取不到值,value="'jkadf1213'" 若是是字符串的话,在双引号里面还要加单引号才能获取到值。
session
关于struts2里面s:set 和s:if的问题,
app
<s:set name="seasonList" value="{'Spring',Summer,'Autumn','Winter'}" /> ide
<span style="font-size: small;"><s:set name="seasonList" value="{'Spring',Summer,'Autumn','Winter'}" /></span> 性能
这么是正确的,可是咱们set一个map,这样就不行了:
测试
<s:set name="monthMap" value='#{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May", spa
"6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}' /> .net
<span style="font-size: small;"><s:set name="monthMap" value='#{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May", code
"6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}' /> blog
</span>
奇怪的是,前面必需要加一个#,感受很奇怪,我也不知道为何,但愿研究过源码的给一个解答。
<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 style="font-size: small;"><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>
发现这样不行,后来通过我反复的测试,当选择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>
<span style="font-size: small;"><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>
</span>