【问题标题】:Multiple Instances of <C:if / Choose /When> condition to be made as one<C:if / Choose /When> 条件的多个实例作为一个
【发布时间】:2011-01-25 22:59:12
【问题描述】:

1:我有一个带有多个 html“TR”的 JSP

  <tr>'s

我的页面

  <% ...%>
  <html>
  ...
  <body>
  <table>
  <tr class="1"> This is first line</tr>
  <br/>
  <tr class="2"> This is Second line</tr>
  <br/>
  <tr class="3"> This is Third line</tr>
  <br/>
  <tr class="4"> This is Fourth line</tr>
  <br/>
  <tr class="5"> This is Fifth line</tr>
  <br/>
  ...
  </html>

2:现在我正在获取用户会话(如果已登录)。注意>>:这里使用Liferay主题显示

  <%String userEmail=themeDisplay.getUser().getEmailAddress();%>
  <c:set var="userEmail" value="<%=userEmail%>"></c:set>

3:我检查了它的访客或注册/登录用户。

然后显示更改为登录用户的第一个和最后一个“TR”。

  <% ...%>
  <html>
  ...
  <body>
  <table>
  <c:if test="${not empty userEmail}">      
  <tr class="1"> This is first line for registered user</tr>
  <c:if test="${empty userEmail}">      
  <tr class="1"> This is first line</tr>
  <br/>
  <tr class="2"> This is Second line</tr>
  <br/>
  <tr class="3"> This is Third line</tr>
  <br/>
  <tr class="4"> This is Fourth line</tr>
  <br/>
  <c:if test="${not empty userEmail}">      
  <tr class="5"> This is Fifth line for registered user</tr>
  <c:if test="${empty userEmail}">      
  <tr class="5"> This is Fifth line</tr>
  <br/>
  ...
  </html>

这很好用!!

我的问题>>>>>如果我想将此检查作为某种常量,在 JSP/HTML 页面上不会重复多次。该怎么办???

  <c:if> //to be declared only once. And called which ever <TR> needs a check??

【问题讨论】:

    标签: java html jsp liferay


    【解决方案1】:

    一些替代方案:

    1. 使用c:set 在页面上设置一个已注册的变量,用于c:choose
    <c:set var="registered" value="${not empty userEmail}"/> <c:choose> <c:when test="${registered}"> <tr class="1"> This is first line for registered user</tr> </c:when> <c:otherwise> <tr class="1"> This is first line</tr> </c:otherwise> </c:choose>
    1. 将第一行和最后一行写为空白表格行,然后使用c:choose 运行 javascript 以插入所需的数据。
    
        <table>
        <tr id="firstRow"></tr>
        ...
        <tr id="lastRow"></tr>
        </table>
    
        <c:choose>
        <c:when test="${not empty userEmail}">
        <script>
        document.getElementById('firstRow').innerHTML = 'This is first line for registered user';
        document.getElementById('lastRow').innerHTML = 'This is Fifth line for registered user';
        </script>
        </c:when>
        <c:otherwise>
        <script>
        document.getElementById('firstRow').innerHTML = 'This is first line';
        document.getElementById('lastRow').innerHTML = 'This is Fifth line';
        </script>
        </c:otherwise>
        </c:choose>
    

    【讨论】:

      猜你喜欢
      • 2019-07-15
      • 1970-01-01
      • 2012-01-11
      • 2021-11-04
      • 2018-09-16
      • 2018-01-02
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多