【问题标题】:get Liferay user information with Vaadin使用 Vaadin 获取 Liferay 用户信息
【发布时间】:2011-11-16 08:45:23
【问题描述】:

我需要在我的 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>

按钮的标题设置为“名字”而不是名字,如果我将键更改为名字,我会从 language.properties 文件中获得默认翻译,而不是我的翻译,我是否遗漏了什么?

【问题讨论】:

  • LanguageUtil 将从 liferay 属性文件中获取属性。我会编辑答案。

标签: java liferay vaadin


【解决方案1】:

你试过了吗

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

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

【讨论】:

  • 叫我菜鸟,但我对请求一无所知,我知道我不能把它放在 init 方法中,但是在哪里呢?在公共 void onRequestStart() 方法中?以及如何打印出我得到的语言 ID(如果有效)。
  • 我已经尝试过了,但是 onRequestStart 方法根本没有被调用,只有 init 方法。有任何想法吗 ?也许我应该使用 HTTPRequest ?
  • 用 HTTP 请求尝试它可以工作,我得到了当前用户的语言 ID,但我看不到我的 portlet 的内容,我收到以下错误:内部错误请通知管理员。记下所有未保存的数据,然后单击此处继续。
  • 它有效,正如你所说,我的问题出在 portlet.xml 中。我为 Eclipse 创建了一个带有 vaadin 插件的 LiferayProject。我有一个示例 vaadin portlet,但是当我查看 portlet.xml 时,我看到了 com.liferay.util.bridges.mvc.MVCPortlet .... 不是 com.vaadin.terminal.gwt.server.ApplicationPortlet2,因为它应该是;/ 感谢您的帮助 +1 + 接受
  • 嘿,你知道如何用 vaadin 读出 Language.properties 文件吗?我也有一些问题。
【解决方案2】:

另一种可能是:

VaadinSession.getCurrent().getLocale()

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

【讨论】:

    【解决方案3】:

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

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

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

    当启动应用程序时,运行 MainApplication init() 方法,并监听 portlet listenert,在 init() 方法之后自动运行 listeners 方法。你可以调用 request.getRemoteUser() 的 handleRenderRequest 方法。它返回数字号码,即远程用户 ID,或者在任何人都没有签名时返回 null。

    portletApplicationContext2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2020-09-07
      • 1970-01-01
      • 2018-03-07
      • 2016-01-29
      • 1970-01-01
      相关资源
      最近更新 更多