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>