在 Grails 中,控制器中会自动编译、或者动态注入一些预定以的属性和方法,常用的如 params、request、response 等,这些属性和方法极大的提升了开发效率。
在 BroFramework 中,我们同样定义了一批开箱即用的属性和API。
目录- 属性
- 方法
- paginateParams( Integer defaultMax = null )
- nocache
- isAjax
- renderAjaxMessage( Map/String message, String jsonp = null )
- renderExport( Map modelAndView )
- renderAjaxError( Object bean, String jsonp = null )
- renderMessage( String message )
- renderError( String message )
- renderWarning( String message )
- renderDenied( String message = null )
- renderText( String message )
- updateDynamicTables( Object bean, Map params, Map callbacks = null )
- updateDynamicTable( Object bean, Class hasManyClass, String hasManyName, String belongsToName, Map params, String paramsPrefix, String requiredField, Map callbacks = null )
- saveReferences( Object bean )
- updateAttachments( Object bean )
- deleteFailedAttachments( Object bean )
- withInstance( Closure c )
属性
employee
当前请求的员工对象(bropen.framework.core.osm.Employee),进而可以获得对应的用户等。
如果当前请求用户不是员工(如管理员用户 admin),则返回一个包含 name 和 email 属性的、未保存的员工实例,并且 email 属性固定为系统参数中的 bropen.framework.mail.from.email 值。
例如:
class FoobarController {
def test() {
render employee.name
}
}
osmEmployeeService
员工服务(bropen.framework.core.OsmEmployeeService)对象,包含一些员工查询与操作的API。例如:
class FoobarController {
def test() {
render osmEmployeeService.getCurrentEmployee() == employee
}
}
osmOrganizationService
机构服务(bropen.framework.core.OsmOrganizationService)对象,包含一些机构查询与操作的API。例如:
def test() {
render osmOrganizationService.getDeptManagers( params.orgCode )
}
settingService
系统参数管理服务(bropen.framework.core.SettingService)对象,包含存取参数的API。例如:
def test() {
render settingService.get("bropen.framework.plugin.im.rtx.enabled", false)
}
attachmentService
附件管理服务(bropen.framework.core.AttachmentService)对象,包含存取附件的API,下面的 updateAttachment 等附件方法都是调用本属性中的API实现的。例如:
def test() {
attachmentService.writeResponse(request, response, new FileInputStream("filepath"), "foo.zip", null, null, null)
}
processEngineService
流程引擎服务(bropen.bpm.ProcessEngineService)对象,包含几乎所有流程引擎相关的API。如:
// 创建一个新的文档,并启动一个新的流程实例
def bean = new Foobar(xx:yy ...)
if ( bean.save(flush:true) ) {
processEngineService.start( employee, bean )
}
方法
paginateParams( Integer defaultMax = null )
处理 params 中的翻页参数,主要用来处理 max;此外,同时兼容 jqGrid 的 page 参数(将其转换为 offset 参数)。
一般在脚手架模板生成的 list 操作中自动调用本方法。
- 如果 params 中包含参数 all,并且为真,则自动将 max 设为 10000、offset 设为 0
- 如果 params 中的参数 max 值大于系统参数 bropen.framework.pagination.max,则将其设置为系统参数值,即不允许一次请求太多数据
- 如果 params 中不包含 max,而调用API时的 defaultMax 参数不为空,则将 max 设为 defaultMax
- 否则,从员工的个性设置(fwk.pagination.max)或者系统默认值(bropen.framework.pagination.default)中计算 max 值
nocache
设置 response 头(Cache-Control、Expires等),不允许浏览器缓存。
在 renderExport 方法中也会自动调用本方法。
isAjax
判断是否是ajax请求,如果参数中有jsonp,则也认为是ajax请求(跨域的json调用)。例如:
if ( isAjax() ) {
renderAjaxMessage( [message: xxx] )
} else {
render view: xxx, model: [yyy: zzzz]
}
renderAjaxMessage( Map/String message, String jsonp = null )
渲染AJAX消息,参数 message 为包含消息的Map,将转成JSON返回给浏览器,或者已转成JSON的字符串直接返回。如 [message:"XXXX", ...]。例如:
renderAjaxMessage( [message: 'foobar', reload: true] )
renderExport( Map modelAndView )
渲染套打视图,参数 modelAndView 为包含view、model属性的Map,和方法 render 的参数一致。
在脚手架模板生成的 export 操作中自动调用本方法渲染套打视图,套打功能的详细开发过程可以参见开发手册。
本方法主要根据 params._export_format 或 response.format 参数设置渲染的 contentType(如 doc -> application/msword),并调用相关的服务,渲染出对应格式的文件给浏览器下载或打开。
例如:
def export() {
Foobar foobar = Foobar.get(params.id)
if ( !foobar ) renderError(message(code:'default.not.found.message', args:[message(code:c1,default:c2), params.id]))
else renderExport( [view: "${VIEW_PATH}export", model:[foobar: foobar]] )
}
renderAjaxError( Object bean, String jsonp = null )
根据 Bean 的 errors 属性,渲染 AJAX 错误消息,渲染结果为 json 格式的 Map 对象,且每条错误之间用回车符 \n 分隔,如:
{ error: "error1\nerror2"}
renderMessage( String message )
将一段简单的文本消息放在一个包含样式的消息页面中渲染,用于提醒用户,以提供更好用户体验。
可以通过链接查看渲染效果:/Foobar/error/message?message=Hello+World
renderMessage( "Hello World!" )
可以通过定制 grails-app/views/bropen/framework/core/Error/ 下的视图文件来个性化提醒界面。
renderError( String message )
将文本消息渲染成一个包含样式的错误页面。
查看渲染效果:/Foobar/error/error?message=Hello+World
renderWarning( String message )
将文本消息渲染成一个包含样式的警告页面。
查看渲染效果:/Foobar/error/warning?message=Hello+World
renderDenied( String message = null )
将文本消息渲染成一个包含样式的拒绝访问页面。
查看渲染效果:/Foobar/error/denied?message=Hello+World
renderText( String message )
直接渲染文本消息,注意 ContextType 将会被设置为 text/plain。
updateDynamicTables( Object bean, Map params, Map callbacks = null )
保存(更新)客户端提交的所有动态表的数据。
在脚手器模板生成的 save、update 操作中会调用该方法,动态表的使用和参数说明详见开发手册与 bropen.toolkit.utils.grails.BeanUtils.updateDynamicTables 的 API 手册。
updateDynamicTable( Object bean, Class hasManyClass, String hasManyName, String belongsToName, Map params, String paramsPrefix, String requiredField, Map callbacks = null )
保存(更新)客户端提交的某张动态表的数据。参数说明详见 BeanUtils 的同名方法。
saveReferences( Object bean )
保存(更新)提交的关联对象(xxx.yyyy)的数据,一般在脚手架模板生成的 save 和 update 方法中被调用。
详细解释见 ControllersApiService 中的同名方法。
updateAttachments( Object bean )
保存请求对象(request)中的附件,并返回附件(bropen.framework.core.Attachment)对象列表。
一般在脚手架模板生成的 save 和 update 方法中,当 bean 被保存成功后调用。
deleteFailedAttachments( Object bean )
当bean保存失败时,删除已保存的附件。一般在 withInstance 中自动调用。
withInstance( Closure c )
简化控制器里的操作代码,将最常用的判断放到本方法中,如判断 params.id 代表的 bean 是否存在、保存失败时自动回滚并删除本次上传的附件等。
在脚手架模板生成的多个操作中都会调用本方法。
详细说明参见 ControllersApiService 中的同名方法。