1

我正在使用 Struts2 编写网页。

我的需要是仅当 Action 类(即sectionHidden)的布尔属性设置为时才在表中显示一行false。为此,我编写了以下代码:

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

我认为这不是最好的方法,还有其他更好的方法吗?(我想避免写两次相关的html代码tr

4

3 回答 3

2

根据您的需求,

如果要裁剪它,请使用一个<s:if>来绘制(或不绘制)行,如 AleksandrM 答案中的那样;

相反,如果您想隐藏它,但又希望它在页面中(例如,稍后使用 javascript 显示它,或在源代码中查看它),您可以使用 an<s:if>来应用(或不应用)隐藏状态:

<tr id="sectionTitle" <s:if test="sectionHidden">style="display: none;"</s:if>>
   <td>This is the section title</td>
</tr>
于 2013-02-04T09:03:19.027 回答
1

嗯......只使用一个怎么样<s:if>

<s:if test="!sectionHidden">
  <tr id="sectionTitle">
    <td>This is the section title</td>
  </tr>
</s:if>
于 2013-02-04T08:49:10.903 回答
1

如果标签以下面的示例为例,您可以使用提供的 struts2 测试任何值

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

在您的情况下,下面的代码将做正确的工作

    <%@ 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>
于 2013-02-04T09:02:43.317 回答