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

相关文章

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti