简体   繁体   中英

Session set attribute not working

I have the following controller:

@Controller
public class HomeController {
@Resource(name="returnGraph")
Graph returnGraph;
@RequestMapping("/")
public String goToHomePage(HttpSession session){
    session.setAttribute("sm", returnGraph.getVertexes());
    return "home";

}
}

The following web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <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>/</url-pattern>
</servlet-mapping>


</web-app>

In the jsp I have typed:

${sm}

The output when I run on server is just:

${sm} 

on the webpage.

This is the home.jsp page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!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>
${sm}
</body>
</html>

What is missing?

As session is not available inside JSP declaration <%! %> <%! %> . So, you can use in following way.

<input type="text" value="<%= session.getAttribute("sm") %>" />

For expression language(EL), you can do

<input type="text" value="${sm}" />

The following needs to be added to the home.jsp:

<%@ page isELIgnored="false" %>

SkyWalker provided a good hint in his answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM