Abhijeet Shukla
Abhijeet Shukla

Reputation: 153

Spring Model Attribute overriding the Session Attribute with same name

I am very new to Spring world and trying a few things related to Spring MVC and session handling.

my question is that if we have Model Attribute and session attribute of same name then does the Model Attribute overrides the value of session attribute ?

In code snippet below (apologies for poor formatting, I am new here) I am adding an attribute names sessionAttribute into Model and Session. While accessing the same attribute in JSP I am getting value of Model Attribute ([name] as Model Attribute ).

@RequestMapping(value="/hello", method=RequestMethod.GET)     
public String hello(@RequestParam(value="username", required=false,defaultValue="World") String name, Model model,HttpServletRequest req) {    
model.addAttribute("sessionAttribute", name+" as Model Attribute");   
System.out.println("In controller");    
HttpSession hs=req.getSession();   
hs.setAttribute("sessionAttribute","overridden Session attribute");     //prints"overridden Session attribute"    
System.out.println(hs.getAttribute("sessionAttribute"));
return "someViewName";
}

Below is the View (someViewName) and it is printing the value of sessionAttribute as Model attribute

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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>Spring4 MVC -HelloWorld</title>
 </head>
  <body>

  <% HttpSession hs=request.getSession();
     String sesstionAttr=(String)session.getAttribute("sessionAttribute");
     out.println(sesstionAttr); //printin [name] as Model Attribute 
  %>

  </body>
 </html>

Upvotes: 3

Views: 3473

Answers (1)

vtor
vtor

Reputation: 9329

my question is that if we have Model Attribute and session attribute of same name then does the Model Attribute overrides the value of session attribute ?

In general, model and session are different things, therefore model attributes and session attributes are different. Model attributes are referring to the model properties (which you have in your view), and session attributes are stored in http session, so that if you add one attribute in session in one controller and then change a view, you can still access added attribute in another one.

So if you are inside of your controller method, adding the session attribute into session will not override the value in model attribute.

However, this may happen in one case - if you are using @SessionAttributes provided by Spring exactly for this reason.

When using @SessionAttributes after your method execution will be finished, Spring will load all attributes from your model, and add them into the session (so that if you have the same name it will be overridden). And next time you try to access the attribute from the session - you will see overridden value from model.

For me it looks like this is happening in your case. However I do not how you have configured your controller, so check if you have specified SessionAttributes or not.

Upvotes: 3

Related Questions