使用Vaadin获取Liferay用户信息
我需要检查我的portlet,用户选择哪种语言作为他的“主要”语言,为此,我必须首先获得UserID(名称)。 我一直在寻找它两天(Liferay论坛,vaadin论坛,stackoverflow等),但到目前为止没有找到任何工作。
我找到了一个很好的例子,但它似乎不起作用(它总是返回“null”)。
package com.example.translation_portlet; import javax.portlet.PortletRequest; import javax.portlet.PortletResponse; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.model.User; import com.liferay.portal.util.PortalUtil; import com.vaadin.Application; import com.vaadin.terminal.gwt.server.PortletRequestListener; import com.vaadin.ui.Label; import com.vaadin.ui.Window; public class Translation_portletApplication extends Application implements PortletRequestListener { /** * */ private static final long serialVersionUID = 1L; @Override public void init() { Window mainWindow = new Window("LoginApplication"); Label label = new Label("Hello anonymous Vaadin user"); if (getUser() != null) { // user has logged in label = new Label("Hello " + ((User) getUser()).getFullName()); } mainWindow.addComponent(label); setMainWindow(mainWindow); } @Override public void onRequestStart(PortletRequest request, PortletResponse response) { if (getUser() == null) { try { User user = PortalUtil.getUser(request); setUser(user); } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } } } @Override public void onRequestEnd(PortletRequest request, PortletResponse response) { // Nothing to do here currently, exists only to implement the // PortletRequestListener interface. } }
编辑:
这是我到目前为止所尝试的:
locale = user.getLocale(); button.setCaption(LanguageUtil.get(locale, "first_name"));
在我的Language.properties中,我将“first_name”的翻译设置为1st Name:
first_name=1st Name
Language.properties文件位于我已添加的内容文件夹中,资源包也添加到我的portlet.xml中:
content/Language
按钮的标题设置为“first_name”而不是第一个名称,如果我将密钥更改为名字,我将从language.properties文件中获取默认翻译而不是我的翻译,我错过了什么吗?
你试过吗?
final ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKey.THEME_DISPLAY); themeDisplay.getUser().getLanguageId();
需要import的是
import javax.portlet.PortletRequest; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.theme.ThemeDisplay;
编辑:
试试这个
@Override public void onRequestStart(PortletRequest request, PortletResponse response) { final ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKey.THEME_DISPLAY); final User user = themeDisplay.getUser(); if (user != null) { // will be printed to log/console System.out.println("User's language id = " + user.getLanguageId()); } else { System.out.println("Guest user."); } setUser(user); }
你也可以试试
@Override public void init() { Window mainWindow = new Window("LoginApplication"); Label label = new Label("Hello anonymous Vaadin user"); if (getUser() != null) { // user has logged in label = new Label("Hello " + ((User) getUser()).getFullName() + ", language id is '" + user.getLanguageId() + "'"); } mainWindow.addComponent(label); setMainWindow(mainWindow); }
EDIT2:
这是适合我的完整示例。 试试吧,如果它不适合你,请展示你的portlet.xml,web.xml和liferay-portlet.xml
package com.test; import java.util.Iterator; import javax.portlet.PortletRequest; import javax.portlet.PortletResponse; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.model.User; import com.liferay.portal.theme.ThemeDisplay; import com.vaadin.Application; import com.vaadin.terminal.gwt.server.PortletRequestListener; import com.vaadin.ui.Component; import com.vaadin.ui.Label; import com.vaadin.ui.Window; public class Translation_portletApplication extends Application implements PortletRequestListener { private User m_user; @Override public void init() { Window window = new Window("Vaadin Portlet Application"); setMainWindow(window); String caption = "Hello Vaadin user!"; if (m_user != null) { caption = caption + " with language id '" + m_user.getLanguageId() + "'"; } window.addComponent(new Label(caption)); } @Override public void onRequestEnd(PortletRequest p_request, PortletResponse p_response) { System.out.println("onRequestEnd"); } @Override public void onRequestStart(PortletRequest p_request, PortletResponse p_response) { final ThemeDisplay themeDisplay = (ThemeDisplay) p_request.getAttribute(WebKeys.THEME_DISPLAY); m_user = themeDisplay.getUser(); } }
EDIT3:
要从您的属性文件中获取标题,您可以尝试(假设上面的课程)
Button button = new Button() { @Override public void attach() { ResourceBundle bundle = ResourceBundle.getBundle(Translation_portletApplication.class.getName(), user.getLocale()); setCaption(bundle.getString("first_name")); } }; window.addComponent(button);
另一种可能性是:
VaadinSession.getCurrent().getLocale()
这不是来自用户浏览器,而是反映了liferay中的用户设置。
如果你在liferay中的portletContext中运行一个vaadin应用程序,你需要在这里监听一个portletlisteners,这是一个示例链接。
实现监听器时,你需要实现监听器:
- handleActionRequest
- handleEventRequest
- handleRenderRequest
- handleResourceRequest
当启动应用程序时,运行MainApplication init()方法,并在u(init)调用侦听器后自动运行侦听器方法。 handleRenderRequest方法你可以调用request.getRemoteUser()。 它返回数字编号,即远程用户ID,或者在任何编码中没有复制时返回null。
portletApplicationContext2