java - <C :if/Choose/When> condition to be made as one 的多个实例

标签 java html jsp liferay

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:现在我正在获取用户 session (如果已登录)。注意>>:使用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??

最佳答案

一些替代方案:

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

关于java - <C :if/Choose/When> condition to be made as one 的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4794195/

相关文章:

java - 通过 spring 表单操作传递的 URI 错误

jquery - Struts 2 和 jQuery 网格,检索网格行

java - 更新按钮的异步任务

html - 绝对定位的内容溢出粘性页脚

javascript - 切换类别按钮不起作用?

javascript - OnClick 元素仅移除一次

java - 我可以使用 EL 从 JSP 访问枚举类的值吗?

java - 如何在Android和iOS浏览器上运行JSP文件?

java - Android 日志无法解析

java - 在单个方法中重用连接对象