2012-12-24 28 views
6

Tôi cố gắng để so sánh hai giá trị: một từ phiên và bao phấn từ iteratorLàm thế nào để so sánh hai chuỗi bằng cách sử dụng các thẻ Struts2 và OGNL?

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

Nhưng tôi không thể so sánh giá trị của tôi, xin vui lòng bạn có thể cho tôi biết nơi tôi đang làm sai lầm?

Trả lời

8

%{} nên được đặt (nếu cần) xung quanh tất cả các tuyên bố, không phải ở giữa.

Đối với chuỗi, bạn nên sử dụng .equals, .equalsIgnoreCase, .contains, .indexOf vv ... Không phải ==.

Thay đổi thế này:

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

này hoạt động quá:

<s:if test="#session.usertheme.equalsIgnoreCase(themeName)"> 
+0

Così funziona bene! –

5

(Không phải là câu trả lời, nhưng hai đề nghị, và tôi cần định dạng; câu trả lời của Andrea là đúng.)

Vì sự tỉnh táo của bản thân bạn và những người theo sau, hãy biến đoạn JSP đó thành một dòng:

<s:iterator value="themes"> 
    <tr> 
     <s:set var="currTheme" value="%{userTheme == themeName ? 'red' : 'green'}"/> 
     <td bgcolor="${currTheme}">Cell content</td> 
    </tr> 
    </s:iterator> 

Xem xét sử dụng theme tên CSS thay vì inline CSS và tránh nó hoàn toàn, khoảng:

td.theme1 { 
    background-color: red; 
} 

td.theme2 { 
    background-color: green; 
} 

td.theme3 { 
    background-color: #daa520; 
} 

(Giả sử chủ đề có tên là "THEME1", "THEME2", "theme3", nhưng đó là không có liên quan.)

<table class="themed-table"> 
    <s:iterator value="themes"> 
    <tr> 
     <td class="${themeName}">Cell content</td> 
    </tr> 
    </s:iterator> 
</table> 

Nó muốn được đẹp hơn để di chuyển các thông tin kiểu "lên" một mức độ, ví dụ, table.theme1 td, nhưng bạn sẽ có được ý tưởng. Làm như vậy cho phép rất nhiều tính linh hoạt trong đó thông tin chủ đề đến từ và vân vân.

+0

+1 Tôi thích đề xuất của bạn. –

0
<!--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> 
Các vấn đề liên quan