的多个实例成为一体的条件

发布于 2024-10-14 08:17:25 字数 1850 浏览 3 评论 0 原文

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

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

1: I have a JSP with multiple html "TR's"

  <tr>'s

My page

  <% ...%>
  <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: Now I am getting user session (if logged in). Note>>:Using Liferay themeDisplay here

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

3: I made a check to see if its guest or registered/logged in user.

Then the display changes for first and last "TR's" to logged-in user.

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

This is working fine!!

My question>>>>>If I want to make this check as constant of some sort, which would not be repeated many times on the JSP/HTML page. What is to be done ???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2024-10-21 08:17:25

一些替代方案:

  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>

<代码>

A few alternatives:

  1. Use c:set to set a registered variable on the page, which you use for a 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. Write the first and last lines as blank table rows, then use a c:choose to run javascript to insert the data you want.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文