Liferay分页新闻列表(Journal Articles二次开发)

本文介绍如何在Liferay的JournalArticles功能基础上扩展分页功能,包括配置portlet及实现自定义视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Liferay中自带了News的功能,可能是太高档了,到现在我都不是很清楚那个News功能的具体作用。

项目中需要用到新闻列表,只需要显示新闻标题,发布时间,有分页功能,可以通过标题上的链接查看新闻详细。

为了偷懒,就用Liferay的CMS中的Journal Articles即日志文章来实现。新闻可以通过CMS录入,这样我们在新增新闻的时候不仅可以指定新闻的类型,而且还可以通过Journal Articles现有的功能来配置显示的相关信息,同时还可以省掉新闻文章搜索的功能,方便不少哦。

好处很多,但是通常有一点达不到客户的要求,可能就需要重新开发,实际情况是Liferay中的Journal Articles并不带有分页功能。如何让他具备分页的功能呢?查看SOURCE决定在其现有的功能上扩展,保留其自身的功能作简单的配置即可。

一、目标:在Liferay的“增加内容”列表的新闻菜单下增加一个“日志新闻”的Portlet
打开liferay-display.xml文件,找到category.news的部分,新增一个id为journal_news的portlet,以下是修改后的代码
xml 代码
  1. <category name="category.news">
  2. <portlet id="4" />
  3. <portlet id="5" />
  4. <portlet id="39" />
  5. <portlet id="journal_news" />
  6. category>

上面的配置信息表示在种类为news的菜单下,有4个portlet可以供用户选择。现在增加了一个id为journal_news的portlet,实际上那只是配置上的设定,journal_news并没有实际存在。

二、打开liferay-portlet-ext.xml文件,新增portlet的配置信息,新增后如下:
xml 代码
  1. <portlet>
  2. <portlet-name>journal_newsportlet-name>
  3. <icon>/html/portlet/journal_articles/icon.pngicon>
  4. <struts-path>journal_articlesstruts-path>
  5. <configuration-action-class>com.liferay.portlet.journalarticles.action.ConfigurationActionImplconfiguration-action-class>
  6. <use-default-template>falseuse-default-template>
  7. <restore-current-view>falserestore-current-view>
  8. <instanceable>trueinstanceable>
  9. <private-request-attributes>falseprivate-request-attributes>
  10. <private-session-attributes>falseprivate-session-attributes>
  11. <render-weight>0render-weight>
  12. portlet>

可能有人看到了,上面的配置信息和journal_articles的配置几乎完全相同,只是名称换了一下。既然要使用journal_articles现有的功能,最简单的当然是直接依葫芦画瓢。

三、打开portlet-ext.xml文件,新增以下内容。
xml 代码
  1. <portlet>
  2. <portlet-name>journal_newsportlet-name>
  3. <display-name>Journal_Newsdisplay-name>
  4. <portlet-class>com.liferay.portlet.StrutsPortletportlet-class>
  5. <init-param>
  6. <name>view-actionname>
  7. <value>/journal_articles/view_newsvalue>
  8. init-param>
  9. <expiration-cache>0expiration-cache>
  10. <supports>
  11. <mime-type>text/htmlmime-type>
  12. supports>
  13. <resource-bundle>com.liferay.portlet.StrutsResourceBundleresource-bundle>
  14. <security-role-ref>
  15. <role-name>power-userrole-name>
  16. security-role-ref>
  17. <security-role-ref>
  18. <role-name>userrole-name>
  19. security-role-ref>
  20. <portlet-info>
  21. <title>Journal Newstitle>
  22. portlet-info>
  23. portlet>

同样也是小的变动,只是view-action的value和title换掉了。其他的也还是和journal_articles一样。
实际上我们要做的只是在表现的JSP看到的效果不一样而已,重点要做的只是在JSP上。

四、struts-config-ext.xml和tiles-defs-ext.xml中增加view-action的相关设定。

struts-config-ext.xml中增加以下部分:
xml 代码
  1. <action path="/journal_articles/view_news" type="com.liferay.portlet.journalarticles.action.ViewAction">
  2. <forward name="portlet.journal_articles.view" path="portlet.journal_articles.view.news" />
  3. action>

tiles-defs-ext.xml增加以下部分:
xml 代码
  1. <definition name="portlet.journal_articles.view.news" extends="portlet">
  2. <put name="portlet_content" value="/portlet/journal_articles/view_news.jsp" />
  3. definition>
仅仅只是表现的JSP不同,其他功能继续沿用。

五、在${CATALINA_HOME}/WABAPPS/ROOT/HTML/portlet/journal_articles目录下新增view_news.jsp,你也可以在开发环境的相关目录中新增该文件,然后通过ANT工具部署到这个这个目录中去。
xml 代码
  1. <%
  2. /**
  3. * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. %>
  24. <%@ page import="com.liferay.portlet.journal.NoSuchArticleException"%>
  25. <%@ include file="/html/portlet/journal_articles/init.jsp" %>
  26. <%
  27. String cur = ParamUtil.getString(request, "cur");
  28. String articleId = ParamUtil.getString(request, "articleId");
  29. boolean tag = Validator.isNull(articleId);
  30. if(!cur.equals(""))
  31. {
  32. //新增cur变量,根据该变量的取值来确定是否为分页操作
  33. tag=true;
  34. }
  35. double version = ParamUtil.getDouble(request, "version");
  36. %>
  37. <c:choose>
  38. <c:when test="<%= tag %>">
  39. <%
  40. if (Validator.isNull(type)) {
  41. type = null;
  42. }
  43. String status = "approved";
  44. PortletURL portletURL = renderResponse.createRenderURL();
  45. portletURL.setWindowState(WindowState.NORMAL);
  46. //相关path也做了同样的变动
  47. portletURL.setParameter("struts_action", "/journal_articles/view_news");
  48. ArticleSearch searchContainer = new ArticleSearch(renderRequest, portletURL);
  49. searchContainer.setDelta(pageDelta);
  50. List headerNames = searchContainer.getHeaderNames();
  51. headerNames.clear();
  52. //新闻列表不需要title,因此注释掉。
  53. //headerNames.add("name");
  54. //headerNames.add("display-date");
  55. //headerNames.add("author");
  56. searchContainer.setOrderableHeaders(null);
  57. ArticleSearchTerms searchTerms = (ArticleSearchTerms)searchContainer.getSearchTerms();
  58. searchTerms.setGroupId(groupId);
  59. searchTerms.setType(type);
  60. searchTerms.setStatus("approved");
  61. %>
  62. <form action="<%=portletURL %>" method="post" name=" <portlet:namespace>fm"</portlet:namespace> >
  63. <%@ include file="/html/portlet/journal/article_search_results.jspf" %>
  64. <%
  65. List resultRows = searchContainer.getResultRows();
  66. for (int i = 0; i < results.size(); i++) {
  67. JournalArticle article = (JournalArticle)results.get(i);
  68. ResultRow row = new ResultRow(article, article.getArticleId() + EditArticleAction.VERSION_SEPARATOR + article.getVersion(), i);
  69. String rowHREF = null;
  70. if (pageURL.equals("popUp")) {
  71. StringMaker sm = new StringMaker();
  72. sm.append(themeDisplay.getPathMain());
  73. sm.append("/journal_articles/view_article_content?groupId=");
  74. sm.append(article.getGroupId());
  75. sm.append("&articleId=");
  76. sm.append(article.getArticleId());
  77. sm.append("&version=");
  78. sm.append(article.getVersion());
  79. rowHREF = sm.toString();
  80. }
  81. else {
  82. portletURL.setParameter("groupId", String.valueOf(article.getGroupId()));
  83. portletURL.setParameter("articleId", article.getArticleId());
  84. portletURL.setParameter("version", String.valueOf(article.getVersion()));
  85. rowHREF = portletURL.toString();
  86. }
  87. String target = null;
  88. if (pageURL.equals("popUp")) {
  89. target = "_blank";;
  90. }
  91. TextSearchEntry rowTextEntry = new TextSearchEntry(SearchEntry.DEFAULT_ALIGN, SearchEntry.DEFAULT_VALIGN, article.getArticleId(), rowHREF, target, null);
  92. /*// Article id
  93. row.addText(rowTextEntry);
  94. // Version
  95. rowTextEntry = (TextSearchEntry)rowTextEntry.clone();
  96. rowTextEntry.setName(String.valueOf(article.getVersion()));
  97. row.addText(rowTextEntry);*/
  98. // Title
  99. rowTextEntry = (TextSearchEntry)rowTextEntry.clone();
  100. rowTextEntry.setName(article.getTitle());
  101. row.addText(rowTextEntry);
  102. // Display date
  103. rowTextEntry = (TextSearchEntry)rowTextEntry.clone();
  104. rowTextEntry.setName(dateFormatDateTime.format(article.getDisplayDate()));
  105. row.addText(rowTextEntry);
  106. // Author
  107. //rowTextEntry = (TextSearchEntry)rowTextEntry.clone();
  108. //rowTextEntry.setName(PortalUtil.getUserName(article.getUserId(), article.getUserName()));
  109. //row.addText(rowTextEntry);
  110. // Add result row
  111. resultRows.add(row);
  112. }
  113. %>
  114. <liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
  115. <liferay-ui:search-paginator searchContainer="<%= searchContainer %>" />
  116. form>
  117. c:when>
  118. <c:otherwise>
  119. <%
  120. String languageId = LanguageUtil.getLanguageId(request);
  121. String content = JournalArticleLocalServiceUtil.getArticleContent(groupId, articleId, version, languageId, themeDisplay);
  122. RuntimeLogic portletLogic = new PortletLogic(application, request, response, renderRequest, renderResponse);
  123. RuntimeLogic actionURLLogic = new ActionURLLogic(renderResponse);
  124. RuntimeLogic renderURLLogic = new RenderURLLogic(renderResponse);
  125. content = RuntimePortletUtil.processXML(request, content, portletLogic);
  126. content = RuntimePortletUtil.processXML(request, content, actionURLLogic);
  127. content = RuntimePortletUtil.processXML(request, content, renderURLLogic);
  128. %>
  129. <%= content %>
  130. c:otherwise>
  131. c:choose>

做相关部署,重启服务器。。。附件中有view_news.jsp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值