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

相关文章

Nginx安全防护的多种方法

《Nginx安全防护的多种方法》在生产环境中,需要隐藏Nginx的版本号,以避免泄漏Nginx的版本,使攻击者不能针对特定版本进行攻击,下面就来介绍一下Nginx安全防护的方法,感兴趣的可以了解一下... 目录核心安全配置1.编译安装 Nginx2.隐藏版本号3.限制危险请求方法4.请求限制(CC攻击防御)

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命