grails中报Cannot create a session after the response has been committed异常的解决办法

grails中,我们在layoutsmain.gsp中使用类似如下的代码来判断当前用户处于登录状态时显示相关的登录信息:

 1 <g:if test="${session.users}">
 2 
 3 ...
 4 
 5 </g:if>
 6 
 7 <g:else>
 8 
 9 ...
10 
11 </g:else>

 

 

但在运行时,发生了如下的异常:

Caused by GroovyPagesException: Error processing GroovyPageView: Cannot create a session after the response has been committed

->> 2886 | runWorker in grails-appviewslayoutsmain.gsp

要解决这个异常,可以使用如下方式来解决:

 1 <g:if test="${request.getSession(false)?.users}">
 2 
 3 ...
 4 
 5 </g:if>
 6 
 7 <g:else>
 8 
 9 ...
10 
11 </g:else>

也就是通过request对象来获取session时,不使用自动创建session的模式。

原文地址:https://www.cnblogs.com/dreampursuer/p/3419213.html