request.getSession(false) 是做什么用的?

What is request.getSession(false) for?

我目前正在阅读 Grails in Action 书中作者在 TagLib 下编写名为 loginToggle 的自定义标签的部分 class

我不太明白 request.getSession(false) 是为了

def loginToggle = {
    out << "<div style='margin: 15px 0 40px;'>"
    if (request.getSession(false) && session.user){
        out << "<span style='float:left; margin-left: 15px'>"
        out << "Welcome ${session.user}."
        out << "</span><span style='float:right;margin-right:15px'>"
        out << "<a href='${createLink(controller:'brewUser', action:'logout')}'>"
        out << "Logout </a></span>"
    } 
    else{
        out << "<span style='float:right;margin-right:10px'>"
        out << "<a href='${createLink(controller:'brewUser', action:'login')}'>"
        out << "Login </a></span>"
    }
    out << "</div><br/>"
}

好书顺便说一句

以上代码的意思是,如果有一个会话(但不创建一个)并且包含一个用户。

查看文档:

HttpSession getSession(boolean create)

Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.

If create is false and the request has no valid HttpSession, this method returns null.

To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.

Parameters:

create - true to create a new session for this request if necessary; false to return null if there's no current session

Returns:

the HttpSession associated with this request or null if create is false and the request has no valid session

See Also:

getSession()