2010-05-25 76 views
56

在我的Grails應用程序控制器之一,我在會話變量保存參數值是這樣的:如何爲Grails應用程序配置會話超時?

session.myVariable = params.myValue 

在那之後,我可以從不同的控制器/ GSP的頁面,只要訪問保存價值我積極使用該應用程序。但是,如果我暫時不使用我的應用程序,即使我的瀏覽器窗口仍處於打開狀態,會話變量也會失去它的價值。

發生這種情況是因爲會話過期了嗎?我的印象是,一個會話直到瀏覽器窗口仍然打開,但顯然我錯了。

我應該怎麼做才能確保我在Grails應用程序中定義的所有會話變量在瀏覽器關閉之前不會過期?有沒有辦法手動設置會話超時?

非常感謝您的回答!

回答

81

另一種辦法是修改web.xml文件。在此之前,你必須調用

grails install-templates 

然後編輯的src /模板/戰爭/ web.xml並添加/後Servlet映射修改:

<session-config> 
    <session-timeout>60</session-timeout> 
</session-config> 

會話超時值使用分鐘爲單位。

+0

謝謝,斯蒂芬!這*完全*我在找什麼。我沒有意識到我必須明確地「安裝模板」才能訪問web.xml。我仍然是Grails中的n00b :) – curd0 2010-05-27 21:04:19

+2

我想要處理會話超時。我只是對此有疑問。在web.xml文件中配置它,一旦會話過期並執行一個控制器的動作,會發生什麼?來自@ curd0例子的session.myVariable會返回null嗎?謝謝! – Tomarto 2013-02-04 15:14:40

+1

不是100%確定,但我認爲它是空 – 2013-02-04 16:25:46

1

我可能是錯的,但我很確定Grails使用與您的應用程序容器關聯的會話。例如,如果您使用Tomcat,則可以指定會話的長度。

Tutorial for changing Tomcat session length.

+0

Grails的確實使用容器的會話,但只是覆蓋默認沒有按」因爲grails會生成一個新的servlet特定的web.xml來覆蓋默認值。 – codemonkey 2015-08-28 18:44:25

5

當前grails(2.x)有一個非常奇怪的設計方法來設置會話超時。的普遍想法都不是很大的:「//會話超時」部分

  1. 註釋掉的WebxmlGrails插件中添加「sessionConfig.sessionTimeout =」到Config.groovy中

  2. 的Grails安裝模板,從web.xml中刪除會話超時,在WebXmlConfig.groovy中添加超時

  3. 等待修復。 :/

一位同事想出了下面的代碼,它對我很好,直到真正的解決方案被構建到grails核心中。

只需將以下內容添加到config.groovy文件的底部,然後設置適當的超時時間。

grails.war.resources = { stagingDir, args -> 
    def webXML = new java.io.File("${stagingDir}/WEB-INF/web.xml") 
    webXML.text = webXML.text.replaceFirst("<session-timeout>30</session-timeout>", "<session-timeout>90</session-timeout>") 
} 

我我建議,正確的解決辦法是允許在Config.groovy中文件的單行:

session.timeout = 90; 

乾杯。

+0

你的意思是「將以下內容添加到你的** Build ** Config.groovy的底部」? – 2015-07-29 20:59:26

1

這裏是一個更好的工作解決方案。去你的Grails主目錄,找到 例如:E:\ grails-2.3。8 \ SRC \戰爭\ WEB-INF \ web3.0.template.xml編輯會話超時值所需值:

例子: enter code here

28

一晃幾年.. 。對於Grails 3.0,在應用程序配置文件中將會話超時設置爲ServerProperties

的grails-app/CONF/application.yml

server: 
    session: 
     timeout: 3600 #seconds 

默認值:秒(30分鐘)

使用 getMaxInactiveInterval()驗證從一個控制器爲HttpSession超時:

log.println "Timeout: ${session.getMaxInactiveInterval()} seconds" 

Out把 - >Timeout: 3600 seconds

更新:更改編輯的配置Grails的3.1

+2

其實現在是服務器:session:timeout:3600 – Kimi 2017-02-01 10:54:31

+0

謝謝@Kimi,答案已經更新。 – 2017-02-01 21:51:10

+0

如何禁用會話超時? @DemPilafian – akiong 2017-03-20 13:41:34

2

使用Grails 3.1.X會話超時已經過時了。在application.yml正確的屬性是:

server: 
    session.timeout: 7200 
0

Grails的3個應用程序,修改Application.groovy工作對我來說:

package foo 

import grails.boot.GrailsApp 
import grails.boot.config.GrailsAutoConfiguration 
import org.apache.catalina.Context 
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory 
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer 
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory 
import org.springframework.context.annotation.Bean 

class Application extends GrailsAutoConfiguration { 

    static void main(String[] args) { 
     GrailsApp.run(Application, args) 
    } 

    @Bean 
    EmbeddedServletContainerFactory containerFactory() { 
     TomcatEmbeddedServletContainerFactory containerFactory = new TomcatEmbeddedServletContainerFactory() 

     containerFactory.addContextCustomizers(new TomcatContextCustomizer() { 
      @Override 
      void customize(Context context) { 
       int oneWeekInMinute = 7 * 24 * 60 
       context.setSessionTimeout(oneWeekInMinute) 
      } 
     }); 

     return containerFactory 
    } 

} 
相關問題