Liferay7 BPM门户开发之29: 核心kernel.util包下面的通用帮助类ParamUtil、GetterUtil使用...

本文主要是介绍Liferay7 BPM门户开发之29: 核心kernel.util包下面的通用帮助类ParamUtil、GetterUtil使用...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

与其闭门造车,不如直接开动原装、进口、免费的法拉利。 -- 作者说

不多说废话,直接上代码。

 

ParamUtil

ParamUtil、GetterUtil是Liferay最重要的帮助类

  • ParamUtil用途:获取PortletRequest的参数值,并作类型转换和判空处理(内部机制是通过GetterUtil)
  • GetterUtil用途:对输入做类型转换、判空处理、赋予默认值(即期望的获取值为空时侯的初始值)

ParamUtil的使用例子1:

 

public void deleteInterview(ActionRequest actionRequest, ActionResponse actionResponse)throws Exception {long interviewId = ParamUtil.getLong(actionRequest, "interviewId");InterviewLocalServiceUtil.deleteInterview(interviewId);sendRedirect(actionRequest, actionResponse);
}

 

ParamUtil的使用例子2:

public void updateQuestion(ActionRequest actionRequest, ActionResponse actionResponse)throws Exception {long questionId = ParamUtil.getLong(actionRequest, "questionId");long questionSetId = ParamUtil.getLong(actionRequest, "questionSetId");String title = ParamUtil.getString(actionRequest, "title");String description = ParamUtil.getString(actionRequest, "description");int type = ParamUtil.getInteger(actionRequest, "type");ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);try {if (questionId <= 0) {QuestionLocalServiceUtil.addQuestion(questionSetId, title, description, type, serviceContext);}else {QuestionLocalServiceUtil.updateQuestion(questionId, title, description, type, serviceContext);}}catch (Exception e) {...}sendRedirect(actionRequest, actionResponse);
}

 

ParamUtil可以通过get来直接获取PortletRequest参数,如:

public static short get(PortletRequest portletRequest, String param, short defaultValue) {return GetterUtil.get(portletRequest.getParameter(param), defaultValue);}public static String get(PortletRequest portletRequest, String param, String defaultValue) {String returnValue =GetterUtil.get(portletRequest.getParameter(param), defaultValue);if (returnValue != null) {return returnValue.trim();}return null;}

 

也可以通过类似getInteger、getLong 获取单一值和获取数组的getIntegerValues、getLongValues之类的方法(用于checkbox的提交)实际都是一样的。

区别是getIntegerValues、getLongValues的返回值是通过调用GetterUtil

public static int getInteger(HttpServletRequest request, String param) {return GetterUtil.getInteger(request.getParameter(param));}public static int getInteger(HttpServletRequest request, String param, int defaultValue) {return get(request, param, defaultValue);}public static int[] getIntegerValues(HttpServletRequest request, String param) {return getIntegerValues(request, param, new int[0]);}public static int[] getIntegerValues(HttpServletRequest request, String param, int[] defaultValue) {return GetterUtil.getIntegerValues(request.getParameterValues(param), defaultValue);}

 


同时,还可以通过print来打印参数输出,方便程序员使用

public static void print(PortletRequest portletRequest) {Enumeration<String> enu = portletRequest.getParameterNames();while (enu.hasMoreElements()) {String param = enu.nextElement();String[] values = portletRequest.getParameterValues(param);for (int i = 0; i < values.length; i++) {System.out.println(param + "[" + i + "] = " + values[i]);}}}     

 

另外较高版本增加了ServiceContext的参数

static float getFloat(ServiceContext serviceContext, String param, float defaultValue) static float[] getFloatValues(ServiceContext serviceContext, String param, float[] defaultValue) 

 

GetterUtil


GetterUtil的代码,就不具体解释了
可以访问:
https://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/util/GetterUtil.java.html

GetterUtil的使用例子:发送request属性到jsp

//java:

package com.liferay.docs.exampleserviceconsumerportlet;import java.io.IOException;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.service.UserLocalService;
import com.liferay.bookmarks.service.BookmarksFolderLocalService;@Component(immediate = true,property = {"com.liferay.portlet.display-category=category.sample","com.liferay.portlet.instanceable=true","javax.portlet.display-name=Example Service Consumer Portlet","javax.portlet.init-param.template-path=/","javax.portlet.init-param.view-template=/view.jsp","javax.portlet.security-role-ref=power-user,user"},service = Portlet.class
)
public class ExampleServiceConsumerPortlet extends MVCPortlet {@Overridepublic void doView(RenderRequest request, RenderResponse response)throws IOException, PortletException {int userCount = getUserLocalService().getUsersCount();request.setAttribute("USER_COUNT", userCount);int bookmarksFolderCount =getBookmarksFolderLocalService().getBookmarksFoldersCount();request.setAttribute("BOOKMARKS_FOLDER_COUNT", bookmarksFolderCount);super.doView(request, response);}public BookmarksFolderLocalService getBookmarksFolderLocalService() {return _bookmarksFolderLocalService;}public UserLocalService getUserLocalService() {return _userLocalService;}@Referencepublic void setBookmarksFolderLocalService(BookmarksFolderLocalService bookmarksFolderLocalService) {_bookmarksFolderLocalService = bookmarksFolderLocalService;}@Referencepublic void setUserLocalService(UserLocalService userLocalService) {_userLocalService = userLocalService;}private UserLocalService _userLocalService;private BookmarksFolderLocalService _bookmarksFolderLocalService;
}

 

//jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<portlet:defineObjects /><%
int userCount = GetterUtil.getInteger(renderRequest.getAttribute("USER_COUNT"));
int bookmarksFolderCount = GetterUtil.getInteger(renderRequest.getAttribute("BOOKMARKS_FOLDER_COUNT"));
%><p>The portal has <%= userCount %> users.</p>
<p>The portal has <%= bookmarksFolderCount %> bookmarks folders.</p>

 

GetterUtil和ParamUtil的区别是:ParamUtil是解析请求专用类,而GetterUtil是通用的类型转换输出类
使用这两个类ParamUtil、GetterUtil的好处显而易见,如果配合Validator类,像判空、类型转换、验证的大段代码可以大大的省略,非常简洁

这篇关于Liferay7 BPM门户开发之29: 核心kernel.util包下面的通用帮助类ParamUtil、GetterUtil使用...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可