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>