【问题标题】:How to compare two strings using Struts2 tags and OGNL?如何使用 Struts2 标签和 OGNL 比较两个字符串?
【发布时间】:2012-12-10 23:35:44
【问题描述】:

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

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

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

【问题讨论】:

    标签: java jsp struts2 ognl struts-tags


    【解决方案1】:

    %{} 应该(如有必要)放在整个语句的周围,而不是放在中间。

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

    【讨论】:

    • 嗨,这不适用于所有情况 确实有效,如何比较???
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
          <!--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>
      

      【讨论】:

        猜你喜欢
        • 2015-02-24
        • 1970-01-01
        • 2013-02-15
        • 1970-01-01
        • 2022-06-29
        • 2018-11-29
        • 2019-01-14
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多