Spring:会话创建时添加jsessionid属性

时间:2012-03-09 07:46:06

标签: spring session jsessionid

当我在我的控制器中调用方法request.getSession()并进行重定向时,容器会在浏览器的url中添加jsessionid。如果我在url中删除jsessionid,则下一页中的request.getSession(false)将返回null。 Cookie已启用并正常运行。

我的控制器代码:

public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

    HttpSession session = request.getSession();
    ModelAndView modelAndView = new ModelAndView("redirect:items.htm");

    return modelAndView;
}

我使用Tomcat 7。 我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>App Name</display-name>
    <welcome-file-list>
        <welcome-file>login.htm</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <resource-ref>
        <res-ref-name>jdbc/oracle</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

</web-app>

我的dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /login.htm=showLoginController
                /guest.htm=guestEnterController
                /items.htm=showItemsController
            </value>
        </property>
    </bean>

    <bean name="guestEnterController"
        class="app.controller.web.controllers.GuestEnterController" />

    <bean name="showItemsController"
        class="app.controller.web.controllers.ShowAllItemsController">
        <property name="factory" ref="daoFactory" />
    </bean>

    <bean name="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/oracle" />
        <property name="resourceRef" value="true" />
    </bean>

    <bean name="sqlConnector" class="app.datalayer.DataSourceConnector">
        <constructor-arg ref="jndiDataSource" />
    </bean>

    <bean name="daoFactory" class="app.datalayer.oracle.OracleDAOFactory">
        <constructor-arg ref="sqlConnector" />
    </bean>

    <bean name="showLoginController"
        class="app.controller.web.controllers.LoginController">
        <property name="formView" value="login" />
        <property name="successView" value="redirect:items.htm" />
        <property name="commandClass" value="app.controller.beans.LoginBean" />
        <property name="commandName" value="loginBean" />
        <property name="factory" ref="daoFactory" />
        <property name="validator">
            <bean class="app.controller.web.validators.LoginValidator"/>
        </property>
    </bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

可能有什么问题?

0 个答案:

没有答案