Grails 无法在提交响应后创建会话?
Grails Cannot create a session after the response has been committed?
我添加了一个 grails 过滤器来将带有 www 的 url 重定向到非 www url。进行此更改后,触发了许多此类性质的错误。
更改是添加一个过滤器,如下所示
class DomainFilters {
def filters = {
wwwCheck(uri:'/**') {
before = {
if (request.getServerName().toLowerCase().startsWith("www.")) {
int port = request.getServerPort();
if (request.getScheme().equalsIgnoreCase("http") && port == 80) {
port = -1;
}
URL redirectURL = new URL(request.getScheme(), request.getServerName().replaceFirst("www.",""), port, request.forwardURI);
response.setStatus(301)
response.setHeader("Location", redirectURL.toString())
response.flushBuffer()
}
}
}
}
}
出错的地方是
session['products-ids'] = sizes.join(",")
错误如下
ERROR 2021-07-15 13:48:48,478 [ajp-bio-8109-exec-720] errors.GrailsExceptionResolver: IllegalStateException occurred when processing request: [GET] /payment/productsPurchaseSummary/976634
Cannot create a session after the response has been committed. Stacktrace follows:
java.lang.IllegalStateException: Cannot create a session after the response has been committed
at registration.PaymentController.productsPurchaseSummary(PaymentController.groovy:621)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
我认为原因与添加的过滤器有关,但我不确定是什么导致无法创建会话错误。我很欣赏任何见解。谢谢!
更新:
在错误点的日志中是由于上述过滤器而被 301 重定向的请求。
185.191.171.18 - - [15/Jul/2021:13:48:48 -0600] "GET /payment/productsPurchaseSummary/976634 HTTP/1.1" 301 950
185.191.171.5 - - [15/Jul/2021:13:48:49 -0600] "GET /payment/productsPurchaseSummary/976634 HTTP/1.1" 200 6341
添加returnfalse后好像修复了。我想如果没有return那么它会继续执行controller action
class DomainFilters {
def filters = {
wwwCheck(uri:'/**') {
before = {
if (request.getServerName().toLowerCase().startsWith("www.")) {
int port = request.getServerPort();
if (request.getScheme().equalsIgnoreCase("http") && port == 80) {
port = -1;
}
URL redirectURL = new URL(request.getScheme(), request.getServerName().replaceFirst("www.", ""), port, request.forwardURI);
response.setStatus(301)
response.setHeader("Location", redirectURL.toString())
response.flushBuffer()
return false
}
}
}
}
}
我添加了一个 grails 过滤器来将带有 www 的 url 重定向到非 www url。进行此更改后,触发了许多此类性质的错误。
更改是添加一个过滤器,如下所示
class DomainFilters {
def filters = {
wwwCheck(uri:'/**') {
before = {
if (request.getServerName().toLowerCase().startsWith("www.")) {
int port = request.getServerPort();
if (request.getScheme().equalsIgnoreCase("http") && port == 80) {
port = -1;
}
URL redirectURL = new URL(request.getScheme(), request.getServerName().replaceFirst("www.",""), port, request.forwardURI);
response.setStatus(301)
response.setHeader("Location", redirectURL.toString())
response.flushBuffer()
}
}
}
}
}
出错的地方是
session['products-ids'] = sizes.join(",")
错误如下
ERROR 2021-07-15 13:48:48,478 [ajp-bio-8109-exec-720] errors.GrailsExceptionResolver: IllegalStateException occurred when processing request: [GET] /payment/productsPurchaseSummary/976634
Cannot create a session after the response has been committed. Stacktrace follows:
java.lang.IllegalStateException: Cannot create a session after the response has been committed
at registration.PaymentController.productsPurchaseSummary(PaymentController.groovy:621)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
我认为原因与添加的过滤器有关,但我不确定是什么导致无法创建会话错误。我很欣赏任何见解。谢谢!
更新:
在错误点的日志中是由于上述过滤器而被 301 重定向的请求。
185.191.171.18 - - [15/Jul/2021:13:48:48 -0600] "GET /payment/productsPurchaseSummary/976634 HTTP/1.1" 301 950
185.191.171.5 - - [15/Jul/2021:13:48:49 -0600] "GET /payment/productsPurchaseSummary/976634 HTTP/1.1" 200 6341
添加returnfalse后好像修复了。我想如果没有return那么它会继续执行controller action
class DomainFilters {
def filters = {
wwwCheck(uri:'/**') {
before = {
if (request.getServerName().toLowerCase().startsWith("www.")) {
int port = request.getServerPort();
if (request.getScheme().equalsIgnoreCase("http") && port == 80) {
port = -1;
}
URL redirectURL = new URL(request.getScheme(), request.getServerName().replaceFirst("www.", ""), port, request.forwardURI);
response.setStatus(301)
response.setHeader("Location", redirectURL.toString())
response.flushBuffer()
return false
}
}
}
}
}