75. XPages中Java开发的一些有用方法(二)

2014-11-24 07:25:43 · 作者: · 浏览: 1
(r)){ return true; } } } return false; } public static boolean hasRole(String role){ String[] roles={role}; return hasRoles(roles); } //returns a Vector containing the name, groups and roles of the current user. public static Vector getUserNamesList() throws NotesException{ Vector result=new Vector (); DirectoryUser user=getContext().getUser(); //DirectoryUser.getFullName() returns the common name. //I can reproduce the issue in both 8.5.1-3. //For local preview, getFullName() returns Anonymous, getDistinguishedName() returns anonymous. result.add(getSession().getEffectiveUserName()); result.addAll(user.getGroups()); result.addAll(user.getRoles()); return result; } public static Map getRequestMap(){ return FacesContext.getCurrentInstance().getExternalContext().getRequestMap(); } public static void feedback(String msg){ getRequestMap().put("message", msg); } }

对这个工具类稍加说明:

feedback()将一个字符串消息传递到RequestMap里,以message键保存,供页面上的消息控件显示。此一约定的键和方法可被managed bean用于向前端反馈消息。

getContext()方法返回com.ibm.xsp.designer.context.XSPContext对象,XSPContext作为对FacesContext的扩展提供了一个XPage运行时的上下文信息。

getCurrentDocument()和getCurrentDominoDocument()方法分别返回当前Document和DominoDocument对象。

getDatabase()的三个重载方法分别依据参数返回当前数据库对象。

getRequestMap()方法返回代表RequestScope变量的java.util.Map对象。

getRoles()返回当前用户的所有角色。

getSession()和SessionAsSigner()分别返回当前Session和以程序签名者身份的Session对象。

getUserNamesList()模仿@UserNamesList函数,返回的Vector 包含当前用户的所有用户名、具有的角色和所属的群组。

hasRole()和hasRoles()分别判断当前用户是否具有给定的一个或者多个角色中任何一个。

程序中用到的AppException就是笔者以前提到的一个简单的Exception的扩展,用以创建自定义的异常。

package starrow;

public class AppException extends Exception {

	private static final long serialVersionUID = -143353750902427769L;

	public AppException(String message){
		super(message);
	}
}

com.acme.tools.JSFUtil来自以前的文章中也提到的KarstenLehmann的blog:http://www.mindoo.de/web/blog.nsf/dx/18.07.2009191738KLENAL.htm opendocument