spring mvc java config怎么配置session-timeout

如题所述

第1个回答  2022-10-27
spring mvc java config怎么配置session-timeout 具体设定很简单,方法有三种:
(1)在主页面或者公共页面中加入:session.setMaxInactiveInterval(600);引数600单位是秒,即在没有10分钟活动后,session将失效。
这里要注意这个session设定的时间是根据伺服器来计算的,而不是客户端。所以如果是在除错程式,应该是修改伺服器端时间来测试,而不是客户端。
(2)也是比较通用的设定session失效时间的方法,就是在专案的web.xml中设定
<!-- 设定session失效,单位分 -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
设定为0,-1 表示永不超时
(3)直接在应用伺服器中设定,如果是tomcat,可以在tomcat目录下conf/web.xml中找到元素,tomcat预设设定是30分钟,只要修改这个值就可以了。
<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly -->
<!-- created sessions by modifying the value below. -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
需要注意的是如果上述三个地方如果都设定了,有个优先顺序的问题,从高到低:(1)>(2)>(3)

session 的timeout不在spring的配置档案里配置,它的配置实在web.xml档案里面

例如像这样
<session-config> <session-timeout>20</session-timeout></session-config>

如何使用纯java config来配置spring mvc
这个不是一定的,随你自己的意思,你可以放在WEB-INF里,也可以放在classpath下。只需在配置web.xml时指定位置即可。
<listener>
<listener-class>
.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:beans.xml
</param-value>
</context-param>
上面就是web.xml中对spring容器的初始化配置,<context-param>中<param-value>中的classpath:beans.xml 即是spring配置档案beans.xml的位置(classpath下,在myeclipse的工程中是src目录下)

这个不是一定的,随你自己的意思,你可以放在WEB-INF里,也可以放在classpath下。只需在配置web.xml时指定位置即可。
.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:beans.xml
上面就是web.xml中对spring容器的初始化配置,中中的classpath:beans.xml 即是spring配置档案beans.xml的位置(classpath下,在myeclipse的工程中是src目录下)
spring mvc应用基于Java config配置是怎么启动的
<!-- 包扫描 -->
<context:ponent-scan base-package=".eduask.chp.controller"></context:ponent-scan>
<context:annotation-config>
<!--检视解析器 -->
<bean
class=".springframework.web.servlet.view.InternalResourceViewResolver">
<property name=prefix value="/view/"></property>
<property name=suffix value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
相似回答