[英]how to iterate a arraylist from session into jsp using struts2 ognl tags
[英]How to compare two strings using Struts2 tags and OGNL?
我試圖比較兩個值:一個來自session,另一個來自iterator
<s:iterator value="themes" status="currentRecord">
<s:if test="%{usertheme}) == %{themeName}">
<td align="center" bgcolor="red">
</s:if>
<s:else>
<td align="center" bgcolor="green">
</s:else>
</s:iterator>
但我無法比較我的價值觀,請你告訴我我在哪里犯錯誤?
應該在所有語句周圍放置%{}
(如果需要),而不是在中間。
對於字符串,你應該使用.equals
, .equalsIgnoreCase
, .contains
, .indexOf
等......不是==
。
改為:
<s:iterator value="themes" status="currentRecord">
<s:if test="%{#session.usertheme.equalsIgnoreCase(themeName)}">
<td align="center" bgcolor="red">
</s:if>
<s:else>
<td align="center" bgcolor="yellow">
</s:else>
....
這也有效:
<s:if test="#session.usertheme.equalsIgnoreCase(themeName)">
(不是答案,而是兩個建議,我需要格式化; Andrea的回答是正確的。)
為了您自己和隨后的理智,將JSP的大塊變成一行:
<s:iterator value="themes">
<tr>
<s:set var="currTheme" value="%{userTheme == themeName ? 'red' : 'green'}"/>
<td bgcolor="${currTheme}">Cell content</td>
</tr>
</s:iterator>
考慮使用主題命名的CSS而不是內聯CSS,並完全避免它,大致:
td.theme1 {
background-color: red;
}
td.theme2 {
background-color: green;
}
td.theme3 {
background-color: #daa520;
}
(假設主題名為“theme1”,“theme2”,“theme3”,但這不相關。)
<table class="themed-table">
<s:iterator value="themes">
<tr>
<td class="${themeName}">Cell content</td>
</tr>
</s:iterator>
</table>
將樣式信息“提升”到一個級別更好,例如table.theme1 td
,但是你明白了。 這樣做可以在主題信息的來源等方面提供很大的靈活性。
<!--name attribute inside select tag must be a variable in action class with getter/setter -->
<!-- test variable sets the value of selected item in action class -->
<select name="test">
<!-- name attribute could be anything you want but value attribute must be a model class variable-->
<s:set name="lead_string_LS_ID" value="MasterDataModel.string_LS_ID" />
<!-- value attribute must be a list to iterate, status (an instanceof IteratorStatus will be pushed into stack upon each iteration)or result -->
<!-- var Name used to reference the value pushed into the Value Stack (my list contain leadSource_String_Id)-->
<s:iterator value="leadSource_list" status="result" var="leadSource_String_Id">
<!--#lead_string_LS_ID is value taken from <set> tag above. Note # must be in-front of the name
leadSource_String_Id is element specified in var of <iterator> tag
-->
<s:if test='(#lead_string_LS_ID.equals(leadSource_String_Id))'>
<option value="<s:property value='leadSource_String_Id'/>" selected="selected">
<s:property value="leadSource_String_Name" />
</option>
</s:if>
<s:else>
<option
value="<s:property value='leadSource_String_Id'/>">
<s:property value="leadSource_String_Name" />
</option>
</s:else>
</s:iterator>
</select>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.