使用 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”的翻译设置为第一个名称:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过
导入所需的内容是
编辑:
尝试使用这个
您也可以尝试
编辑2:
这是适合我的完整示例。尝试一下,如果它不适合您,请显示您的 portlet.xml、web.xml 和 liferay-portlet.xml
EDIT3:
要从您的属性文件中获取标题,您可以尝试使用(假设上面的类)
Did you try with
Imports needed are
EDIT:
Try with this
You can also try
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
EDIT3:
For getting caption from your property files you can try with (assuming above class)
另一种可能性是:
这不是来自用户浏览器,而是反映了liferay中的用户设置。
Another possibility is:
This does not come from the users browser but reflects the user setting in liferay.
如果你在 liferay 的 portletContext 中运行 vaadin 应用程序,你需要监听 portletlisteners,这里有一个 的链接示例。
当实现监听器时,您需要实现监听器:
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:
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