---
title: "OFBiz 常用代码段分享"
author: "孔已己"
date: "2014-12-08T14:04:48Z"
tags: []
categories: ["编程语言"]
slug: "ofbiz"
draft: false
lastmod: "2014-12-08T14:04:48Z"
id: 2164142
url: "https://5ven.iteye.com/blog/2164142"
---

1、快速封装需要调用services 的参数

`ModelService promoModel = dctx.getModelService("updateContent");`

`Map<String, Object> invokeCtx = promoModel.makeValid(tempMap, ModelService.IN_PARAM);`

 

2、快速验证services返回是否正确和错误信息

`ServiceUtil.isError(result)`

`ServiceUtil.getErrorMessage(result)`

 

3、快速将一个实体对象转换成Map

Map&lt;String,Object&gt; tempMap = genericvalue.getAllFields();

 

4、在screen中使用groovy调用java方法

`<actions>`

`<set field="productStore"` `value="${groovy: org.ofbiz.product.store.ProductStoreWorker.getProductStore(request)}"` `/>`

`</actions>`

 

5、OFBiz常用的工具类（转载，地址 [http://blog.163.com/kel\_scott66/blog/static/115053963200982168180/](http://blog.163.com/kel_scott66/blog/static/115053963200982168180/)）

`工具类主要在包org.ofbiz.core.util中。`

`1、 属性文件访问工具类：UtilProperties。`

`2、 Map、List对象操作工具类：UtilMisc。`

`3、 UtilFormatOut ：通用格式化输出工具类（主要用在 Jsp文件或View Helper中）。`

`4、 UtilURL：得到文件流的URL地址类。`

`5、 UtilCache：缓存管理类。`

`6、 UtilValidate：通用数据输入输出数据校验（合法性和有效性）类，可任意扩展。.`

`7、 UtilDateTime：java.util.Date和java.sql.Date格式的日期/时间处理类。`

`8、 StringUtil：增强的字符串处理类。`

`9、 UtilXML：增强的符合JAXP & DOM 规范的XMl解析器处理工具类。`

`10、 SiteDefs：常数定义类，定义所有Web 程序用到的和环境有关的常量。`

`11、 Debug：格式化输出程序调试信息类。`

`12、 HttpClient：模拟一个HttpServlet请求类。`

`13、 HttpRequestFileUpload：接受一个通过Http上传的文件工具类。`

`14、 SendMailSMTP：符合SMTP协议的邮件发送处理类（实现发送邮件服务器的功能）。`

6、在请求的时候调用service和java代码

`<request-map uri="updateActivityAppreciate">`

`<security auth="true"/>`

`<event type="service"` `invoke="updateActivityAppreciate"></event> 这里调用service`

`<response name="success"` `type="view"` `value="EditActivityAppreciate"/>`

`<response name="error"` `type="view"` `value="EditActivityAppreciate"/>`

`</request-map>`

`<request-map uri="downloadActivityWorks">`

`<security  auth="true"/>`

`<event type="java"` `path="org.ofbiz.activity.ActivityServices"` `invoke="downloadActivityWorks"` `/> 这里直接调用java代码，也就是servlet`

`<response name="success"` `type="view"` `value="downloadActivityWorks"/>`

`</request-map>`

 

7、错误信息的返回

`ServiceUtil.returnError("错误");`

前台可以通过${parameters.\_ERROR\_MESSAGE\_} 来获取到这个错误

8、FTL中可以这样调用java代码

`<#assign smallImageUrl = Static["org.ofbiz.product.product.ProductContentWrapper"].getProductContentAsText(itemPro, "SMALL_IMAGE_URL")?if_exists />`