淘宝商城月销5000件

OpenCms运行环境

OpenCms运行环境

TemplateTwo 扩展之二:为新闻列表(listbox.jsp)增加分页功能

作者【袁海明】 时间【2009-12-21】 浏览【3,054】

在创建“新闻公告”栏目时,我们已经实现了新闻列表的功能,但TemplateTwo本身并没有为列表提供分页功能,本文主要讲述如何为新闻列表增加分页功能。

1、找到新闻列表文件:/system/modules/org.opencms.frontend.templatetwo/elements /boxes/listbox.jsp;

2、编辑listbox.jsp,从原文件55行开始修改后内容如下,55行之上的内容不变,加粗部分是修改和添加的内容:

  <%-- Entries of the list box --%>
  <cms:contentload collector="${listbox.value['Collector']}" param="${cms.parameter}" preload="true" >
  
   <cms:contentinfo var="info" />   
   <c:if test="${info.resultSize > 0}">
    <%-- 以下61至62行为替换后内容,原内容为:<cms:contentload editable="true"> --%>
                <cms:contentload pageSize="1" pageIndex="%(param.pageIndex)" pageNavLength="10" editable="true">
     <cms:contentinfo var="contentInfo"/>

     <cms:contentaccess var="resource" />
     <c:set var="entry" value="${cms.mappedEntry[resource.rawContent]}" />
     
     <div>
      <h5><a href="${entry.link}">${entry.title}</a></h5>
  
      <%-- Author at top position --%>
      <c:if test="${!empty entry.author && listbox.value['PositionAuthor'] == 'Top'}">
       <p><small>${entry.author}</small></p>
      </c:if>
  
      <%-- Date at top position --%>
      <c:if test="${!empty entry.date && listbox.value['PositionDate'] == 'Top'}">
       <p><small><fmt:formatDate value="${entry.date}" type="date"/></small></p>
      </c:if>
      
      <%-- Image at top|left|right position --%>
      <c:if test="${!empty entry.image && (listbox.value['PositionImage'] == 'Top' || listbox.value['PositionImage'] == 'Left' || listbox.value['PositionImage'] == 'Right')}">
       <c:choose>
        <c:when test="${listbox.value['PositionImage'] == 'Left'}"><c:set var="float" value="left" /></c:when>
        <c:when test="${listbox.value['PositionImage'] == 'Right'}"><c:set var="float" value="right" /></c:when>
       </c:choose>
  
       <div <c:if test="${!empty float}">class="${float}"</c:if>>
        <cms:img src="${entry.image}" alt="${entry.title}" scaleType="1" width="100" scaleQuality="90"/>
       </div>
      </c:if>
  
      <%-- The text of the entry --%>
      <c:if test="${!empty entry.text}">
       <div>
        <c:out value="${entry.text}" escapeXml="false" />
   
        <%-- The more link of the entry --%>
        <c:if test="${!listbox.value['More'].isEmptyOrWhitespaceOnly}">
         <a href="${entry.link}"><c:out value="${listbox.value['More']}" escapeXml="false" /></a>
        </c:if>
       </div>
      </c:if>
      
      <%-- Image at bottom position --%>
      <c:if test="${!empty entry.image && listbox.value['PositionImage'] == 'Bottom'}">
       <cms:img src="${entry.image}" alt="${entry.title}" scaleType="1" width="100" scaleQuality="90"/>
      </c:if>
      
      <%-- Author at bottom position --%>
      <c:if test="${!empty entry.author && listbox.value['PositionAuthor'] == 'Bottom'}">
       <p><small>${entry.author}</small></p>
      </c:if>
      
      <%-- Date at bottom position--%>
      <c:if test="${!empty entry.date && listbox.value['PositionDate'] == 'Bottom'}">
       <p><small><fmt:formatDate value="${entry.date}" type="date"/></small></p>
      </c:if>  
     </div>
    </cms:contentload>
   </c:if>
   
   <%-- Additional Link --%>
   <c:if test="${listbox.hasValue['AdditionalLink']}">
    <a href="<cms:link><c:out value="${listbox.value['AdditionalLink'].value['Link']}" /></cms:link>"><c:out value="${listbox.value['AdditionalLink'].value['Text']}" /></a>
   </c:if>
            <!-- 分页开始 -->
            <c:if test="${contentInfo.pageIndex>1}">
                  <a href="<cms:link>${cms:getCmsObject(pageContext).requestContext.uri}</cms:link><c:out value="?pageIndex=${contentInfo.pageIndex-1}"/>">上一页</a>
            </c:if>
            <c:forEach var="i" begin="${contentInfo.pageNavStartIndex}" end="${contentInfo.pageNavEndIndex}">
              <c:choose>
                  <c:when test="${contentInfo.pageIndex==i}">
                              &nbsp;<font color="red"><c:out value="${i}"/></font>
                  </c:when>
                  <c:otherwise>
                              &nbsp;<a href="<cms:link>${cms:getCmsObject(pageContext).requestContext.uri}</cms:link><c:out value="?pageIndex=${i}"/>"><c:out value="${i}"/></a>
                  </c:otherwise>   
              </c:choose>
            </c:forEach>
            <c:if test="${contentInfo.pageIndex < contentInfo.pageCount}">
                  &nbsp;<a href="<cms:link>${cms:getCmsObject(pageContext).requestContext.uri}</cms:link><c:out value="?pageIndex=${contentInfo.pageIndex+1}"/>">下一页</a>
            </c:if>

            <!-- 分页结束 -->
  </cms:contentload>  
 </div>    
</div>

其中pageSize="1" 设置的是每页显示的条数,“新闻公告”栏目只有三条新闻,设置每页显示一条,应该分三页显示,如下图所示,显示的是选择第二页时的效果:

添加分面后的新闻列表

图1(TemplateTwo 分页新闻列表)

20 条评论 »

  1. 匿名 - 2011年09月2日 @ 5:03 下午

    袁老师,请问怎么解决,当用ttarticle页面显示多个listbox的时候,分页不好用了:当listbox里面内容,也就是文件不统一数量时候,例如:news_01有5个文件,news_02有3个文件,同时在ttarticle显示时,按分页说,每页显示2条,那么前者可以分三页,后者可以分2页,当点显示是第二页时,点击下一页会报空指针异常,因为后者没有第三页。
    所以怎么可以做到当我选择点击第一个listbox里面的下一页时候,第二个listbox而不变。

  2. 可以翻页了。 - 2011年09月2日 @ 3:49 下午

    现在可以翻页了,本来我也点击下一页没有反应的,后来检查代码,发现多写了个%。。

  3. ch - 2011年07月18日 @ 1:50 上午

    已经能够翻页了,但点击下一页没有反应,看到的还是第一页的内容,请问是什么原因?

  4. 匿名 - 2011年05月19日 @ 5:02 下午

    为什么我按照上面改了报错呢
    javax.servlet.jsp.JspException: org.apache.jasper.JasperException: /WEB-INF/jsp/offline/system/modules/org.opencms.frontend.templatetwo/elements/boxes/listbox.jsp(119,3) The end tag “</c:if” is unbalanced
    Reason: /WEB-INF/jsp/offline/system/modules/org.opencms.frontend.templatetwo/elements/boxes/listbox.jsp(119,3) The end tag “</c:if” is unbalanced
    我统计了一下 c:if 是30个,都是配对的啊

  5. 匿名 - 2011年05月5日 @ 2:35 下午

    具体怎么不一样的参数啊?

    %(link1)news_01_%(number).html|ttnews|100

  6. 袁海明 - 2011年04月14日 @ 8:36 下午

    用不同的翻页参数

  7. 匿名 - 2011年04月13日 @ 8:03 下午

    文章里有两个listBox时翻页错误
    javax.servlet.jsp.JspException: java.lang.IllegalArgumentException: fromIndex(2) > toIndex(1)原因: fromIndex(2) > toIndex(1)
    请问袁老师怎么解决啊

  8. 匿名 - 2011年04月13日 @ 7:33 下午

    当在一个文章中加入两个listbox时翻页错误
    提示 indexfrom 比 indexend 大 
    是不是 pageIndex 变量的问题啊? 怎么解决啊

  9. 匿名 - 2010年10月20日 @ 4:20 下午

    附图(图1)链接错误,没有显示

  10. 首页分页错误-问题已经解决 - 2010年08月9日 @ 1:37 下午

    谢谢

  11. 首页分页错误 - 2010年08月9日 @ 10:53 上午

    如果我把列表模块放在首页,不希望首页进行分页,而且限制首页列表显示条数应该怎么做?

  12. 袁海明 - 2010年07月28日 @ 1:51 下午

    在缓存属性中添加你的分页参数,listbox.jsp的cache属性

  13. 另一个人 - 2010年07月26日 @ 10:06 上午

    我也遇到同样的问题:分页在offline正常,在online下没用,请我是什么原因?

  14. 一个人 - 2010年05月27日 @ 11:47 上午

    问下为什么,分页总是在offline下有用到online下就没用了?

  15. 匿名 - 2010年05月7日 @ 4:27 下午

    修改引号

  16. 袁海明 - 2010年04月20日 @ 10:58 上午

    配置有错误,检查一下吧。

  17. 匿名 - 2010年04月13日 @ 11:42 上午

    我改了之后转到内容页面的时候提示500的错误。恢复模版后也还是变成了500的错误。。。如何解决啊。。。急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急!

  18. 匿名 - 2010年04月13日 @ 10:01 上午

    改了之后就找不到我的页面了。。。请问是怎么回事。。。然后我把原有的代码回复回去还是一个效果

  19. 袁海明 - 2010年04月2日 @ 10:39 上午

    如果拷贝的内容无误,那再检查一下加的位置是否正确。

  20. 匿名 - 2010年04月1日 @ 9:19 下午

    为什么我试了以后会提示在这句话这里提示出错呢?提示是标签未闭合,检查了好久了都没有发现原因