使用Vaadin获取Liferay用户信息 [英] get Liferay user information with Vaadin

查看:133
本文介绍了使用Vaadin获取Liferay用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查我的portlet,用户选择哪种语言作为他的主要语言,为此,我必须首先获得UserID(名称)。我一直在寻找它两天(Liferay论坛,vaadin论坛,stackoverflow等),但到目前为止没有找到任何工作。

I need to check in my portlet wich language does an user have selected as his "main" language, to do that, I have to get the UserID (name) first . i have been looking for it for two days (Liferay forums , vaadin forums , stackoverflow etc.) but nothing found that would work so far.

我找到了一个很好的例子,但它似乎无法工作(它总是返回null)。

I have found an nice example but it doesnt seem to work (It always returns "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.
    }

}

编辑:

这是我到目前为止所尝试的:

this is what i have tryed so far :

locale = user.getLocale();
button.setCaption(LanguageUtil.get(locale, "first_name"));

在我的Language.properties中,我将first_name的翻译设置为1st Name:

and in my Language.properties i have the translation for "first_name" set to 1st Name:

first_name=1st Name

Language.properties文件位于我已添加的内容文件夹中,资源包也添加到我的portlet.xml中:

the Language.properties file is located in my content folder i have added and resource-bundle to my portlet.xml too :

    <resource-bundle>content/Language</resource-bundle>

按钮的标题设置为first_name而不是第一个名称,如果我将密钥更改为名字我从language.properties文件得到一个默认翻译没有我的翻译,我错过了什么吗?

The caption of the button is set to "first_name" not 1st name , if i change the key to first-name i get an default translation no my tranlsation from the language.properties file , am i missing something ?

推荐答案

你有没有?尝试使用

final ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKey.THEME_DISPLAY);
themeDisplay.getUser().getLanguageId();

所需进口货物是

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

This is complete example that works for me. Try it, if it does not work for you please show your portlet.xml, web.xml and 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:

要从您的属性文件中获取标题,您可以尝试(假设上面的课程)

For getting caption from your property files you can try with (assuming above class)

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);

这篇关于使用Vaadin获取Liferay用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆