使用 Vaadin 获取 Liferay 用户信息

发布于 2024-12-16 01:27:55 字数 2420 浏览 0 评论 0原文

我需要检查我的 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”的翻译设置为第一个名称:

first_name=1st Name

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

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

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

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.

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.
    }

}

EDIT :

this is what i have tryed so far :

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

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

first_name=1st Name

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>

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李白 2024-12-23 01:27:55

您是否尝试过

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

编辑2:

这是适合我的完整示例。尝试一下,如果它不适合您,请显示您的 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);

Did you try with

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

Imports needed are

import javax.portlet.PortletRequest;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;

EDIT:

Try with this

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

You can also try

@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:

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);
小情绪 2024-12-23 01:27:55

另一种可能性是:

VaadinSession.getCurrent().getLocale()

不是来自用户浏览器,而是反映了liferay中的用户设置。

Another possibility is:

VaadinSession.getCurrent().getLocale()

This does not come from the users browser but reflects the user setting in liferay.

风追烟花雨 2024-12-23 01:27:55

如果你在 liferay 的 portletContext 中运行 vaadin 应用程序,你需要监听 portletlisteners,这里有一个 的链接示例

当实现监听器时,您需要实现监听器:

  1. handleActionRequesthandleEventRequesthandleRenderRequesthandleResourceRequest
  2. ,因此运行MainApplication init()方法,并且您监听portlet监听
  3. 当启动应用程序时
  4. 器,在

init()方法之后自动运行监听器方法。您可以调用 request.getRemoteUser() 的handleRenderRequest 方法。它返回数字,即远程用户 ID,如果没有任何人签名,则返回 null。

portletApplicationContext2

If u run a vaadin application in portletContext in liferay, u need to listening a portletlisteners here's an link to example.

When implemented the listener u need implemened the listener:

  1. handleActionRequest
  2. handleEventRequest
  3. handleRenderRequest
  4. handleResourceRequest

When started the application, so run the MainApplication init() method, and u listening to portlet listenert, after the init() method automatic run the listeners methods. The handleRenderRequest method u can call the request.getRemoteUser(). It return the digital number, thats a remote user ID, or null when no singed in anybodey.

portletApplicationContext2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文