2011-11-16 130 views
2

我需要檢查我的portlet是否有用戶選擇作爲其「主要」語言的語言,爲此,我必須先獲取UserID(名稱)。我一直在尋找它兩天(Liferay論壇,vaadin論壇,stackoverflow等),但沒有發現,將工作到目前爲止。使用Vaadin獲取Liferay用戶信息

我找到了一個很好的例子,但它似乎並沒有工作(它總是返回「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. 
    } 

} 

編輯:

這是我迄今tryed:

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得到一個默認的轉換沒有我tranlsation文件,我錯過了什麼?

+0

LanguageUtil將從liferay屬性文件中獲取屬性。我會編輯答案。 –

回答

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

打電話給我noob,但我不知道請求,我知道我不能把它放在init方法,但在那裏呢?在public void onRequestStart()方法?以及如何打印出我得到的語言ID(如果有效)。 – Kiesa

+0

我已經嘗試過,但onRequestStart方法並沒有被調用,只有init方法。有任何想法嗎 ?也許我應該使用HTTPRequest? – Kiesa

+0

試用它HTTP請求它的工作原理,我得到當前用戶的語言ID,但我不能看到我的portlet的內容我得到以下錯誤:內部錯誤 請通知管理員。 記下任何未保存的數據,然後單擊此處繼續。 – Kiesa

0

如果妳portletContext在Liferay中運行vaadin應用,ü需要監聽portletlisteners下面就來example的鏈接。

當實現u需要implemened監聽器監聽器:

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

當啓動應用程序,所以運行MainApplication的init( )方法,並且在ini之後聆聽portlet listenert t()方法自動運行偵聽器方法。 handleRenderRequest方法可以調用request.getRemoteUser()。它返回數字號碼,這是一個遠程用戶ID,或者當不在任何人身上燒錄時爲空。

portletApplicationContext2

1

另一種可能性是:

VaadinSession.getCurrent().getLocale() 

這不來從用戶的瀏覽器,但反映在Liferay的用戶設置。