How to compare two strings using Struts2 tags and OGNL?

感情迁移 提交于 2019-12-04 22:28:26

问题


I am trying to compare two values : one from session and anther from 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>

But I am unable to compare my values, please can you tell me where I am doing mistakes ?


回答1:


%{} should be put (if necessary) around all the statement, not in the middle.

For Strings you should use .equals, .equalsIgnoreCase, .contains, .indexOf etc... Not ==.

Change to this:

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

this works too:

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



回答2:


(Not an answer, but two suggestions, and I needed formatting; Andrea's answer is correct.)

For the sanity of yourself and those that follow, turn that chunk of JSP into a single line:

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

Consider using theme-named CSS instead of inline CSS and avoid it completely, roughly:

td.theme1 {
  background-color: red;
}

td.theme2 {
  background-color: green;
}

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

(Assuming themes named "theme1", "theme2", "theme3", but that's not relevant.)

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

It'd be nicer to move the style info "up" a level, e.g., table.theme1 td, but you get the idea. Doing so allows a lot of flexibility in where the theme info comes from and so on.




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


来源:https://stackoverflow.com/questions/14023164/how-to-compare-two-strings-using-struts2-tags-and-ognl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!