如何比较使用Struts2的标签和OGNL两个字符串?(How to compare two str

2019-08-04 15:29发布

我想比较两个值:一个来自会话和花药从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>

但我无法比拟的我的价值观,请你能告诉我在哪里,我在做错误?

Answer 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)">


Answer 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 ,但你的想法。 这样做允许在主题信息来自等具有很大的灵活性。



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


文章来源: How to compare two strings using Struts2 tags and OGNL?