如何使用Struts2标签和OGNL比较两个字符串?

时间:2012-12-24 15:34:02

标签: java jsp struts2 ognl struts-tags

我想比较两个值:一个来自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>

但是我无法比较我的价值观,请你告诉我我在哪里犯错误?

3 个答案:

答案 0 :(得分:8)

%{}应该(如有必要)围绕所有陈述,而不是在中间。

对于字符串,您应该使用.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)">

答案 1 :(得分:5)

(不是答案,而是两个建议,我需要格式化; 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)会更好,但是你明白了。这样做可以在主题信息的来源等方面提供很大的灵活性。

答案 2 :(得分: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>