set標籤注意的地方

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

Java代碼  複製代碼
  1. <s:set name="seasonList" value="{'Spring',Summer,'Autumn','Winter'}" />  ide

  1. <span style="font-size: small;"><s:set name="seasonList" value="{'Spring',Summer,'Autumn','Winter'}" /></span>  性能


這麼是正確的,可是咱們set一個map,這樣就不行了: 
測試

Java代碼  複製代碼
  1. <s:set name="monthMap" value='#{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May",   spa

  2. "6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}' />  .net

  1. <span style="font-size: small;"><s:set name="monthMap" value='#{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May",  code

  2. "6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}' />  blog

  3. </span>  


奇怪的是,前面必需要加一個#,感受很奇怪,我也不知道爲何,但願研究過源碼的給一個解答。 

Java代碼  複製代碼
  1. <span class="s">   

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

  3.                         <s:set name="currentMonth" value="%{ chargeMonth }" />   

  4.                         <select name="chargeMonth"class="styled">   

  5.                             <option value=" ">-- Select Month --</option>   

  6.                             <s:iterator value="#monthMap" id="month">   

  7.                                 <option value="<s:property value='key'/>"

  8.                                         <s:if test="%{key==#currentMonth}">   

  9.                                             selected="selected"

  10.                                         </s:if>   

  11.                                         >   

  12.                                     <s:property value='value'/>   

  13.                                 </option>   

  14.                             </s:iterator>   

  15.                         </select>  

  1. <span style="font-size: small;"><span class="s">  

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

  3.      <s:set name="currentMonth" value="%{ chargeMonth }" />  

  4.     <select name="chargeMonth"class="styled">  

  5.         <option value=" ">-- Select Month --</option>  

  6.         <s:iterator value="#monthMap" id="month">  

  7.         <option value="<s:property value='key'/>"

  8.         <s:if test="%{key==#currentMonth}">  

  9.             selected="selected"

  10.         </s:if>  

  11.             >  

  12.         <s:property value='value'/>  

  13.         </option>  

  14.         </s:iterator>  

  15.          </select></span>  


發現這樣不行,後來通過我反覆的測試,當選擇10,11,12時候能正確的將selected加上,可是選擇前面的數字怎麼都不行,後來經我觀察,前面我將字符串用的是’,估計在後臺反射的時候將其類型錯誤的轉化成char類型了,可是估計當判斷length大於1的時候,將其轉化成字符串了,因此可以正確。不知道我這麼猜的對不對,貼出來分享一下吧,之後但願這樣的問題不能在犯了。 
我修改成下面的就能夠了: 

Java代碼  複製代碼
  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"}' />   

  2.                         <s:set name="currentMonth" value="%{chargeMonth}" />   

  3.                         <select name="chargeMonth"class="styled">   

  4.                             <option value=" ">-- Select Expiration Month --</option>   

  5.                             <s:iterator value="#monthMap" id="month">   

  6.                                 <option value="<s:property value='key'/>"

  7.                                         <s:if test="%{key==#currentMonth}">   

  8.                                             selected="selected"

  9.                                         </s:if>   

  10.                                         >   

  11.                                     <s:property value='value'/>   

  12.                                 </option>   

  13.                             </s:iterator>   

  14.                         </select>  

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

  2. <s:set name="currentMonth" value="%{chargeMonth}" />  

  3.     <select name="chargeMonth"class="styled">  

  4.     <option value=" ">-- Select Expiration Month --</option>  

  5.     <s:iterator value="#monthMap" id="month">  

  6.     <option value="<s:property value='key'/>"

  7.         <s:if test="%{key==#currentMonth}">  

  8.         selected="selected"

  9.         </s:if>  

  10.         >  

  11.         <s:property value='value'/>  

  12.     </option>  

  13.     </s:iterator>  

  14.     </select>  

  15. </span>  

 

 

http://blog.csdn.net/cuker919/article/details/5181286

相關文章
相關標籤/搜索