我想获取liferay中所有用户的电子邮件地址...
我使用了以下代码..
DynamicQuery dynamicQuery = UserLocalServiceUtil.dynamicQuery();
dynamicQuery.add(RestrictionsFactoryUtil.isNotNull("emailAddress"));
int startOfList = 0;
int endOfList = 100;
List<User> articles = (List<User>) UserLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);
有人可以告诉我我在哪里做错了。
我得到了:
java.lang.InstantiationException
和com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering portlets for ControlPanelPortlet-portlet
。ControlPanelPortlet是我的portlet的名称。
答案 0 :(得分:1)
如果您只想获取所有用户的电子邮件地址列表,则可以使用以下代码:
DynamicQuery dynamicQuery = UserLocalServiceUtil.dynamicQuery();
dynamicQuery.add(RestrictionsFactoryUtil.isNotNull("emailAddress"));
// this is the statement which would cause to return only a list of emails
dynamicQuery.setProjection(ProjectionFactoryUtil.property("emailAddress"));
int startOfList = QueryUtil.ALL_POS;
int endOfList = QueryUtil.ALL_POS; // this will fetch all the Users
// note: it returns a list of String
List<String> emailAddresses = (List<String>) UserLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);
如果您想要提取User
,那么您的代码就可以了,只需要更改startOfList
和endOfList
字段:
DynamicQuery dynamicQuery = UserLocalServiceUtil.dynamicQuery();
dynamicQuery.add(RestrictionsFactoryUtil.isNotNull("emailAddress"));
int startOfList = QueryUtil.ALL_POS;
int endOfList = QueryUtil.ALL_POS;
List<User> users = (List<User>) UserLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);
或者如果您不想检查任何条件并想要获取User
s,那么只需使用:
List<User> users = UserLocalServiceUtil.getUsers(startOfList, endOfList);
关于您看到的例外情况,它们与您发布的代码段无关。这可能是由于Java类或JSP中的一些其他代码。如果你把完整的堆栈跟踪,那么我们可以提供帮助。
答案 1 :(得分:0)
试试这段代码..
List<User> users = UserLocalServiceUtil.getUsers(0, UserLocalServiceUtil.getUsersCount());
for(User user : users) {
System.out.println(user.getEmailAddress());
}
如果您有问题,请告诉我