Servlet 相关工具
| Modifiers | Name | Description |
|---|---|---|
static String |
X_REQUESTED_WITH |
AJAX请求头名称 |
static String |
contextPath |
当前WebApp的上下文根 |
| Type | Name and description |
|---|---|
static String |
controllerURI(javax.servlet.http.HttpServletRequest request) |
static javax.servlet.http.HttpServletRequest |
createMockRequest(String requestURI)生成一个模拟的request对象 |
static javax.servlet.http.HttpServletResponse |
createMockResponse(java.io.PrintWriter writer)生成一个模拟的response对象,相比 MockHttpServletResponse,便于操作输出结果 |
static boolean |
executeWithSessionLock(String lockKey, Integer synSize, Closure callback, Closure callbackFail = null )在session范围内限定并发的执行,如果没有session,则抛出IllegalStateException。 |
static Map |
expandParams(Map params, boolean removeExpanded = true )将Map展开,便于数据绑定。 |
static Map |
flattenParams(Map params)将params扁平化,便于redirect或forward。 |
static String |
getActionName()获得当前控制器操作名 |
static eu.bitwalker.useragentutils.Browser |
getClientBrowser(javax.servlet.http.HttpServletRequest request = getRequest()获得客户端浏览器类型 |
static eu.bitwalker.useragentutils.Browser |
getClientBrowser(String userAgent)获得客户端浏览器类型 |
static eu.bitwalker.useragentutils.Version |
getClientBrowserVersion(javax.servlet.http.HttpServletRequest request = getRequest()获得客户端浏览器版本 |
static String |
getClientIpAddr(javax.servlet.http.HttpServletRequest request = getRequest()获得客户端IP地址 |
static String |
getClusterNode(boolean configOnly = false )获取当前集群节点(实验性质) |
static List<String> |
getClusterNodes(boolean rebroadcast = false )获得所有集群节点(实验性质,需配置 clusters.properties、并在系统防火墙中开放ehcache同步端口) |
static String |
getControllerName()获得当前控制器名称 |
static String |
getControllerURI(javax.servlet.http.HttpServletRequest request)根据http请求,获得控制器的URI,如:/Foo/bar/ |
static Locale |
getLocale(javax.servlet.http.HttpServletRequest request = getRequest()获得当前请求的locale |
static Map |
getParams()获得当前控制器操作请求中的params对象 |
static javax.servlet.http.HttpServletRequest |
getRequest()获得当前请求的request对象 |
static String |
getRequestBody(javax.servlet.http.HttpServletRequest request = getRequest()当请求不是以参数方式提交时(如WebService),获得请求内容的字符串 |
static String |
getRequestURI(javax.servlet.http.HttpServletRequest request = null, Boolean decodeQueryString = false )计算访问的URI |
static javax.servlet.http.HttpServletResponse |
getResponse()获得当前请求的response对象 |
static String |
getServerHost(javax.servlet.http.HttpServletRequest request = getRequest(, boolean isLocalAddr = false)获得服务器的主机地址 |
static String |
getServerURL(javax.servlet.http.HttpServletRequest request = getRequest(, boolean isLocalAddr = false)获得服务器的绝对地址,如:http://xxx/cc/ |
static javax.servlet.http.HttpSession |
getSession()获得当前请求用户的session |
static String |
getWebappPath()获得应用的web文件夹路径,如 /opt/Tomcat7/webapps/Foobar,或者开发环境下工程的 web-app 文件夹 |
static boolean |
isAjax(javax.servlet.http.HttpServletRequest request = getRequest(, String jsonp = "jsonp" )判断通过http头,判断request是否是ajax请求。 |
static boolean |
isIE(javax.servlet.http.HttpServletRequest request = getRequest()客户端浏览器是否是IE |
static void |
nocache(javax.servlet.http.HttpServletResponse response)禁止缓存 |
static void |
paginateParams(Map params, Integer defaultMax = null )处理params中的翻页参数(offset,max),并且可以自动转换 jqGrid 的翻页参数 |
static void |
renderAjaxMessage(Object controller, String msg, javax.servlet.http.HttpServletRequest request = getRequest(, String jsonp = "jsonp")控制器辅助:渲染AJAX消息 |
static void |
renderAjaxMessage(Object controller, Map msg, javax.servlet.http.HttpServletRequest request = getRequest(, String jsonp = "jsonp")控制器辅助:渲染AJAX消息 |
static String |
renderModelAndView2Text(Map modelAndView, javax.servlet.http.HttpServletRequest request)返回渲染ModelAndView(gsp)后的文本字符串,返回结果会自动套用layout模板 |
static String |
renderModelAndView2Text(Map modelAndView)返回渲染ModelAndView(gsp)后的文本字符串,返回结果不会套用layout模板(可能导致公共的js、样式等无法加载)。 |
static void |
setFilename(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String filename)转换文件名,并设置到 response 的 Content-Disposition 头中,已便客户端保存文件的时候显示正确的文件名 |
| Methods inherited from class | Name |
|---|---|
class Object |
Object#wait(long), Object#wait(long, int), Object#wait(), Object#equals(Object), Object#toString(), Object#hashCode(), Object#getClass(), Object#notify(), Object#notifyAll() |
AJAX请求头名称
当前WebApp的上下文根
生成一个模拟的request对象
生成一个模拟的response对象,相比 MockHttpServletResponse,便于操作输出结果
如:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
def resp = ServletUtils.createMockResponse(new PrintWriter(baos))
....
contents = baos.toString()
baos.close()
在session范围内限定并发的执行,如果没有session,则抛出IllegalStateException。
需要执行的逻辑以闭包的形式传入。
lockKey - 锁的keysynSize - 最大并发数callback - 需要执行的闭包callbackFail - 超过并发数时执行的闭包将Map展开,便于数据绑定。
如 [ "a.b":1 ] 将转为 ["a": ["b":1]]。
将params扁平化,便于redirect或forward。
如 [ "a.b":1, "a":["b":1] ] 将去掉Map类型的参数 a。
获得当前控制器操作名
获得客户端浏览器类型
获得客户端浏览器类型
获得客户端浏览器版本
获得客户端IP地址
获取当前集群节点(实验性质)
获得所有集群节点(实验性质,需配置 clusters.properties、并在系统防火墙中开放ehcache同步端口)
获得当前控制器名称
根据http请求,获得控制器的URI,如:/Foo/bar/
获得当前请求的locale
获得当前控制器操作请求中的params对象
获得当前请求的request对象
当请求不是以参数方式提交时(如WebService),获得请求内容的字符串
计算访问的URI
request - 请求对象,默认自动计算当前请求decode - 是否解码 query string 中的参数,默认为否获得当前请求的response对象
获得服务器的主机地址
request - HttpServletRequest对象isLocalAddr -
是否取服务器的本地地址(如apache的mod_proxy_http绑定的地址)和端口,
默认为否,即取浏览器访问时使用的地址和端口获得服务器的绝对地址,如:http://xxx/cc/
request - HttpServletRequest对象isLocalAddr -
是否取服务器的本地地址(如apache的mod_proxy_http绑定的地址)和端口,
默认为否,即取浏览器访问时使用的地址和端口。获得当前请求用户的session
获得应用的web文件夹路径,如 /opt/Tomcat7/webapps/Foobar,或者开发环境下工程的 web-app 文件夹
判断通过http头,判断request是否是ajax请求。
参考 SpringSecurity 插件的 grails.plugin.springsecurity.SpringSecurityUtils 中的同名方法。
request - 可选,Http请求对象jsonp -
可选,jsonp的参数名称,默认为jsonp,即找到名为jsonp的参数,则也认为是ajax请求;
如果为空,则不判断。
客户端浏览器是否是IE
禁止缓存
处理params中的翻页参数(offset,max),并且可以自动转换 jqGrid 的翻页参数
params - 需要处理的MapdefaultMax -
如果参数中不包含max时的默认值。
如果为空,会从 setting 或 profile 中取默认值(部署了BroFramework的话),或设为20。控制器辅助:渲染AJAX消息
controller - 控制器对象控制器辅助:渲染AJAX消息
返回渲染ModelAndView(gsp)后的文本字符串,返回结果会自动套用layout模板
modelAndView - 包含view、model、plugin属性的Map(同控制器中render的参数)request - 当前的http请求对象,用于套用layout模板返回渲染ModelAndView(gsp)后的文本字符串,返回结果不会套用layout模板(可能导致公共的js、样式等无法加载)。
modelAndView - 包含view、model、plugin属性的Map(同控制器中render的参数)转换文件名,并设置到 response 的 Content-Disposition 头中,已便客户端保存文件的时候显示正确的文件名