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

2024-02-01 18:48
文章标签 java 方法 开发 75 有用 xpages

本文主要是介绍75. XPages中Java开发的一些有用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在用Java进行XPages开发时,有一些常见的基础性的任务。这些经常要做的事部分与在Lotus Notes客户端开发时遇到的相同,例如获得当前Session和数据库对象,但是达成的方法与用LotusScript截然不同;其它则是XPages开发环境特定的需求,比如获得当前com.ibm.xsp.designer.context.XSPContext和RequestMap对象(即RequestScope变量)。将这些频繁需要的任务以静态方法的形式写在一个工具类里是很好用的:

package starrow.xsp;import java.lang.reflect.Method;
import java.util.*;import javax.faces.context.FacesContext;import starrow.AppException;import lotus.domino.*;
import lotus.domino.local.NotesBase;
import com.acme.tools.JSFUtil;
import com.ibm.designer.runtime.directory.DirectoryUser;
import com.ibm.xsp.component.UIViewRootEx2;
import com.ibm.xsp.designer.context.XSPContext;
import com.ibm.xsp.model.DataSource;
import com.ibm.xsp.model.domino.DominoDocumentData;
import com.ibm.xsp.model.domino.DominoUtils;
import com.ibm.xsp.model.domino.wrapped.DominoDocument;@SuppressWarnings("unchecked")
public class XSPUtil {public static Session getSession(){//return (Session) JSFUtil.getVariableValue("session");return DominoUtils.getCurrentSession();}public static Session getSessionAsSigner(){return (Session) JSFUtil.getVariableValue("sessionAsSigner");}public static Database getDatabase() throws NotesException{return getSession().getCurrentDatabase();//return (Database) JSFUtil.getVariableValue("database");}public static Database getDatabase(String dbPath) throws NotesException{String server=getDatabase().getServer();return getDatabase(server, dbPath);}public static Database getDatabase(String server, String dbPath) throws NotesException{return getSession().getDatabase(server, dbPath);}public static XSPContext getContext(){//return (XSPContext) JSFUtil.getVariableValue("context");FacesContext fc=FacesContext.getCurrentInstance();return XSPContext.getXSPContext(fc);}public static Document getCurrentDocument() throws Exception{UIViewRootEx2 view=(UIViewRootEx2) FacesContext.getCurrentInstance().getViewRoot();for (DataSource ds : view.getData()){if (ds instanceof DominoDocumentData){DominoDocumentData ddd=(DominoDocumentData) ds;DominoDocument dd=(DominoDocument) ddd.getDataObject();return dd.getDocument();}}throw new AppException("No document data source is found.");//return null;		}public static DominoDocument getCurrentDominoDocument() throws Exception{UIViewRootEx2 view=(UIViewRootEx2) FacesContext.getCurrentInstance().getViewRoot();for (DataSource ds : view.getData()){if (ds instanceof DominoDocumentData){DominoDocumentData ddd=(DominoDocumentData) ds;return (DominoDocument) ddd.getDataObject();}}throw new AppException("No document data source is found.");//return null;		}public static Object[] getRoles(){XSPContext context=XSPUtil.getContext();List userRoles=context.getUser().getRoles();return userRoles.toArray();}public static boolean hasRoles(String[] roles){XSPContext context=XSPUtil.getContext();List userRoles=context.getUser().getRoles();for (Object ur : userRoles){for (String r : roles){if (ur.toString().equals(r)){return true;}}}return false;}public static boolean hasRole(String role){String[] roles={role};return hasRoles(roles);}//returns a Vector<String> containing the name, groups and roles of the current user.public static Vector<String> getUserNamesList() throws NotesException{Vector<String> result=new Vector<String>();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<String>包含当前用户的所有用户名、具有的角色和所属的群组。

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




这篇关于75. XPages中Java开发的一些有用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/668141

相关文章

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for