1

I'm writing a web page using Struts2.

My need is to show a row in a table only when an boolean attribute of an Action class (ie sectionHidden) is set to false. To do this, I've written the following code:

<s:if test="sectionHidden"><tr id="sectionTitle" style="display: none;"></s:if>
<s:else><tr id="sectionTitle"></s:else>
    <td>This is the section title</td>
</tr>

I think this is not the best way, are there other better ways to do it? (I would like to avoid to write twice the html code related to the tr)

Roman C
  • 49,761
  • 33
  • 66
  • 176
Claudio Query
  • 323
  • 1
  • 4
  • 13

3 Answers3

2

Basing on your needs,

if you want to crop it, use one <s:if> to draw (or not) the row as in AleksandrM answer;

if instead you want to hide it, but you want it to be in the page (for example for showing it later with javascript, or viewing it in the source), you can use an <s:if> to apply (or not) the hidden state:

<tr id="sectionTitle" <s:if test="sectionHidden">style="display: none;"</s:if>>
   <td>This is the section title</td>
</tr>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
1

Hmm... How about using just one <s:if>:

<s:if test="!sectionHidden">
  <tr id="sectionTitle">
    <td>This is the section title</td>
  </tr>
</s:if>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
1

you can test any value with struts2 provided if tag take an example below

<s:if test="anyBooleanValue">
 I am returning TRUE.<br/>
 </s:if>
 <s:else>
 I am returning FALSE.<br/>
 </s:else>

 <!-- For String Value -->
 <s:if test="%{myStringValue!=null}">
 String is not null<br/>
 </s:if>
 <s:elseif test="%{myStringValue==null}">
 String is null<br/>
 </s:elseif>
 <s:else>
 String is null<br/>
 </s:else>

 <!-- For Object Value -->
 <s:if test="%{checkArrayList.size()==0}">
 Object Size is Zero<br/>
 </s:if>
 <s:else>
 Object Size is not a Zero<br/>
 </s:else>

in your case below code will do right job

    <%@ taglib prefix="s" uri="/struts-tags" %>
 <html>
<head>
</head>

<body>
<h1>Struts 2 If, Else, ElseIf tag example</h1>

<s:set name="sectionHidden" value="false"/>

    <table>

        <tr id="sectionTitle" style="display:<s:if test="sectionHidden">none</s:if><s:else>block</s:else>">
              <td>This is the section title</td>
        </tr>
    </table>
</body>
</html>
Rais Alam
  • 6,970
  • 12
  • 53
  • 84
  • 1
    My need is a little bit different. Using jquery I hide/show the row, using Struts I want to set the initial status of the row (for example hidden) and than change it using jquery. I was looking for a way (if it exists) to put style="display: none;" writing only one time – Claudio Query Feb 04 '13 at 10:30
  • Changed answer to meet your requirement – Rais Alam Feb 04 '13 at 10:58
  • Incase of true row will be in hiiden mode and in case of false row will be in block mode. – Rais Alam Feb 04 '13 at 11:03