Claudio Query
Claudio Query

Reputation: 323

Change behavior of a page using Struts2

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)

Upvotes: 1

Views: 736

Answers (3)

Rais Alam
Rais Alam

Reputation: 7016

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>

Upvotes: 1

Andrea Ligios
Andrea Ligios

Reputation: 50281

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>

Upvotes: 2

Aleksandr M
Aleksandr M

Reputation: 24406

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>

Upvotes: 1

Related Questions