6

我正在尝试比较两个值:一个来自会话,另一个来自迭代器

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

但我无法比较我的价值观,请你告诉我我在哪里做错了吗?

4

3 回答 3

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)">
于 2012-12-24T15:47:27.600 回答
5

(不是答案,而是两个建议,我需要格式化;安德里亚的答案是正确的。)

为了你自己和后面的人的理智,把这块 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,但是您明白了。这样做可以在主题信息的来源等方面提供很大的灵活性。

于 2012-12-24T20:04:30.447 回答
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>
于 2013-07-19T08:58:37.987 回答