首页 > 解决方案 > 即使在 jsp 页面中提供 taglib 之后,JSTL sessionScope 属性也不起作用

问题描述

我正在创建一个 Demo Spring MVC 应用程序,并且正在尝试使用 Interceptor 维护 Session。一切工作正常流入,但是当我尝试使用 JSTL sessionScope在 JSP 页面上获取会话对象值时,如下所示:${sessionScope.LoginSession.firstName},jsp 页面不接受它,它正在打印这个 sessionScope 行,虽然我已经正确添加了 taglib 和 evrything。

当我尝试获取这样的值时,我正在正确获取会话值:session.getAttribute("LoginSession"),但它不适用于 JSTL sessionScope。

我的 JSP 页面是:

    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <div align="center">
        <h1>Carwale Header</h1>
    </div>
    <div align="right">
        <%
            Object user = session.getAttribute("LoginSession");
        System.out.println("Value from jsp "+user);
        %>
        <c:out value="${sessionScope.LoginSession.firstName}"></c:out>
        <c:choose>
            <c:when test="${sessionScope.LoginSession!=null}">
                <c:out value="Welcome," />
                <a href="addProduct.do"> <c:out
                        value="${sessionScope.LoginSession.firstName}" />
                </a>
                <div align="right">
                    <a href="logout.do">Logout</a>
                </div>
            </c:when>
        </c:choose>
    </div>

</body>
</html>

我的 web.xml 文件如下所示:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_3.xsd">

    <display-name>Archetype Created Web Application</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext-dao.xml</param-value>
    </context-param>


    <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>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

POM中的JSTL依赖:

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

我期望它应该正确地给我会话值,但是在 JSP 页面 sessionScope 值显示为它被写入:${sessionScope.LoginSession.firstName}

标签: javaspringjspsessionjstl

解决方案


推荐阅读