在 Liferay 中的页面重新加载期间添加 portlet 实例
我试图通过单击链接在 liferay 中添加一个新的 IFrame portlet。我已经通过一些问题实现了这一目标。
首先,我使用 liferay sdk 创建了一个 portlet 并自定义了默认的 view.jsp
:
<portlet:renderURL var="pageDisp">
<portlet:param name="jspPage" value="/view.jsp" />
</portlet:renderURL>
<a href="<%=pageDisp%>&clink=g">Google</a> <br />
<a href="<%=pageDisp%>&clink=y">Yahoo</a> <br />
<a href="<%=pageDisp%>&clink=d">Daily Tech</a> <br />
<a href="<%=pageDisp%>&clink=w">Wired</a> <br />
将 portlet.xml
更改为
<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>
我
<portlet-class>test.LinksPortlet</portlet-class>
在 LinksPortlet
中实现了以下代码>:
public void render(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException {
log("LinksPortlet:render");
try {
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY);
LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();
List<Portlet> portletList = layoutTypePortlet.getPortlets();
for(Portlet p : portletList) {
log(p.getPortletId());
log(p);
log(p.getDisplayName());
log(p.getInitParams());
}
String c = request.getParameter("clink");
log(c);
if(c != null && (c.equals("g") || c.equals("y") || c.equals("d") || c.equals("w"))) {
String portletId = layoutTypePortlet.addPortletId(Long.parseLong(request.getRemoteUser()), PortletKeys.IFRAME, "column-2", -1);
log("portletId: "+portletId);
long companyId = themeDisplay.getCompanyId();
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
Layout layout = themeDisplay.getLayout();
PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId, ownerId, ownerType, layout.getPlid(), portletId);
Locale locale = new Locale("en", "US");
prefs.setValue("portlet-setup-use-custom-title", "true");
if(c.equals("g")) {
prefs.setValue("src","http://www.google.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Google");
}
else if(c.equals("y")) {
prefs.setValue("src","http://www.yahoo.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Yahoo");
}
else if(c.equals("d")) {
prefs.setValue("src", "http://www.dailytech.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Daily Tech");
}
else if(c.equals("w")) {
prefs.setValue("src", "http://www.wired.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Wired");
}
PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType, layout.getPlid(), portletId, prefs);
portletList = layoutTypePortlet.getPortlets();
for(Portlet p : portletList) {
if(p.getPortletId().equals(portletId)) {
break;
}
}
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
}
}
catch(Exception e) {
log("LinksPortlet:doView:Exception");
e.printStackTrace();
}
super.render(request, response);
}
上述实现的问题是,页面重新加载后,新的 portlet 会出现在屏幕上: http://www.go-ahead.in/books/1.JPG http://www.go-ahead.in/books/2.JPG http://www.go-ahead.in/books/3.JPG
问题的原因是新的 portlet 是在布局渲染开始后添加的。是否可以自定义或添加一个钩子来执行我在页面/布局加载期间执行的操作。我发现了 liferay 的 LayoutAction.java 源代码,其中添加了 portlet。有没有办法在不修改liferay源代码的情况下添加post hook来添加portlet?
我正在使用 Liferay 6.0.5。
提前致谢。
I am trying to add a new IFrame portlet in liferay on click of a link. I have achieved it with some issues.
First I created a portlet using liferay sdk and customized the default view.jsp
:
<portlet:renderURL var="pageDisp">
<portlet:param name="jspPage" value="/view.jsp" />
</portlet:renderURL>
<a href="<%=pageDisp%>&clink=g">Google</a> <br />
<a href="<%=pageDisp%>&clink=y">Yahoo</a> <br />
<a href="<%=pageDisp%>&clink=d">Daily Tech</a> <br />
<a href="<%=pageDisp%>&clink=w">Wired</a> <br />
Changed portlet.xml
from
<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>
to
<portlet-class>test.LinksPortlet</portlet-class>
I have implemented the following code in LinksPortlet
:
public void render(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException {
log("LinksPortlet:render");
try {
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY);
LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();
List<Portlet> portletList = layoutTypePortlet.getPortlets();
for(Portlet p : portletList) {
log(p.getPortletId());
log(p);
log(p.getDisplayName());
log(p.getInitParams());
}
String c = request.getParameter("clink");
log(c);
if(c != null && (c.equals("g") || c.equals("y") || c.equals("d") || c.equals("w"))) {
String portletId = layoutTypePortlet.addPortletId(Long.parseLong(request.getRemoteUser()), PortletKeys.IFRAME, "column-2", -1);
log("portletId: "+portletId);
long companyId = themeDisplay.getCompanyId();
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
Layout layout = themeDisplay.getLayout();
PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId, ownerId, ownerType, layout.getPlid(), portletId);
Locale locale = new Locale("en", "US");
prefs.setValue("portlet-setup-use-custom-title", "true");
if(c.equals("g")) {
prefs.setValue("src","http://www.google.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Google");
}
else if(c.equals("y")) {
prefs.setValue("src","http://www.yahoo.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Yahoo");
}
else if(c.equals("d")) {
prefs.setValue("src", "http://www.dailytech.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Daily Tech");
}
else if(c.equals("w")) {
prefs.setValue("src", "http://www.wired.com");
prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Wired");
}
PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType, layout.getPlid(), portletId, prefs);
portletList = layoutTypePortlet.getPortlets();
for(Portlet p : portletList) {
if(p.getPortletId().equals(portletId)) {
break;
}
}
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
}
}
catch(Exception e) {
log("LinksPortlet:doView:Exception");
e.printStackTrace();
}
super.render(request, response);
}
The issue with the above implementation is that the new portlet appears on the screen after the page reload:
http://www.go-ahead.in/books/1.JPG
http://www.go-ahead.in/books/2.JPG
http://www.go-ahead.in/books/3.JPG
The reason for the issue is that the new portlet is added after the layout render starts. Is it possible to customize or add a hook for performing the action I am performing during page / layout load. I came across the source LayoutAction.java of liferay where portlets are added. Is there any way to add a post hook to add the portlet without modifying the liferay's source code?
I am using Liferay 6.0.5.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了问题的解决方案。
我已将代码从 render 方法移至 processAction 并使用
标记创建链接。Found the solution to the problem.
I have moved the code from render method to processAction and creating the link using
<portlet:actionURL>
tag.