如何在Liferay 6.1中以编程方式设置"链接到页面"布局?

Mat*_*sig 2 spring-mvc liferay liferay-6

我目前正在使用Liferay Portal Enterprise Edition 6.1 EE GA1(Paton/Build 6110/2012年2月15日),它部署在我的本地计算机(Win 7/x64)上,用于开发和测试目的.它运行在标准的7.0.25 Tomcat中,我们使用Spring MVC来开发我们的portlet.

我正在以编程方式向Liferay添加新布局(页面),我将类型的类型设置为"link_to_layout"(LayoutConstants.TYPE_LINK_TO_LAYOUT).布局已成功创建,但到目前为止,我还没有想出如何为链接设置值,例如"/ login"之类的内容.下面是我用来添加布局的代码:

// Spring specific code for getting the Request
HttpServletRequest request = null;    
ServletRequestAttributes sa = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if( null != sa ){
    request = sa.getRequest();
}    
if (request == null) { break; }

// all values are usually retrieved via special methods from our code
// for better readability I have added the real values here
long userId = 10102;
int groupId = 13056;
boolean privateLayout = false;
long plid = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
String title = "my title";
ServiceContext serviceContext = ServiceContextFactory.getInstance( request );
String layoutType = LayoutConstants.TYPE_LINK_TO_LAYOUT;
boolean hidden = false;
String friendlyURL = "/new-page";
// finally, add the layout
Layout layout = LayoutLocalServiceUtil.addLayout( userId, groupId, privateLayout, plid, title, title, StringPool.BLANK, layoutType, hidden, friendlyURL, serviceContext );
Run Code Online (Sandbox Code Playgroud)

我现在可以通过检查"控制面板"中的"网站页面"手动验证页面是否已创建并且页面类型已设置为"link_to_layout".在那里,我看到新页面带有一个空的"链接到页面"值.我也可以通过调用以下代码(在控制台中显示消息)以编程方式验证此操作:

if( layout.isTypeLinkToLayout() ){
 log.debug( "type is link to page" );
}
Run Code Online (Sandbox Code Playgroud)

如果有人知道如何设置页面链接的价值,请与我分享您的知识.非常感谢您的帮助和答案:-)

最好的问候,Mathias

Mat*_*sig 5

在通过Liferay花费更多时间处理问题和调试之后,我终于遇到了一个解决方案:"链接到页面"页面的值可以通过布局的TypeSettingsProperties中的属性进行设置.具有必要布局ID的键"linkToLayoutId"负责处理它.

以下代码对我有用,我希望它对其他开发人员有益:

// Spring specific code for getting the Request
HttpServletRequest request = null;    
ServletRequestAttributes sa = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if( null != sa ){
    request = sa.getRequest();
}    
if (request == null) { break; }

// all values are usually retrieved via special methods from our code
// for better readability I have added the real values here
long userId = 10102;
int groupId = 13056;
boolean privateLayout = false;
long plid = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
String title = "my title";
ServiceContext serviceContext = ServiceContextFactory.getInstance( request );
String layoutType = LayoutConstants.TYPE_LINK_TO_LAYOUT;
boolean hidden = false;
String friendlyURL = "/new-page";
// add the layout
Layout layout = LayoutLocalServiceUtil.addLayout( userId, groupId, privateLayout, plid, title, title, StringPool.BLANK, layoutType, hidden, friendlyURL, serviceContext );
String LinkToPageUrl = "/login";
Layout linkToPageLayout = LayoutLocalServiceUtil.getFriendlyURLLayout( groupId, false, linkToPageUrl);
long linkToPageId = linkToPageLayout.getLayoutId();
// set the value of the "link to page"
UnicodeProperties props = layout.getTypeSettingsProperties();
props.put( "linkToLayoutId", Long.toString( linkToPageId ) );
layout.setTypeSettingsProperties( props );
LayoutLocalServiceUtil.updateLayout( layout ); // crucial, 'cause otherwise the changes will not appear on the layout
Run Code Online (Sandbox Code Playgroud)