I found a peculiar problem today. Normally when I make controller methods, I use a ModelMap object for carrying data from the method to the jsp. Using these in spring mvc allows me to access the data using the ${ } construct and jstl tags when needed. This keeps the jsp code compact and manageable compared to adding java scriplets into the jsp. Heres a simple example :
This is a method I wrote for performing a login process.
@RequestMapping(value = "/submit.htm", method = RequestMethod.POST)
public String doLogin(HttpServletRequest hsreq, HttpServletResponse hsres,
ModelMap model) throws Exception {
		String user = hsreq.getParameter("user");
String passwd = hsreq.getParameter("passwd");
		if (user != null && passwd != null
&& loginservice.isLoginValid(user, passwd)) {
			logger.log(Priority.INFO, "Controller – Credentials are valid.");
model.addAttribute("user", user);
if (user.equals(AppConstants.GUEST_USER)) {
logger.log(Priority.INFO, "Controller – Guest User Mode.");
return AppConstants.HOME_PAGE;
} else {
return AppConstants.MAIN_PAGE;
}
}
logger.log(Priority.INFO, "LoginController – Credentials are invalid.");
model.addAttribute(AppConstants.MAIN_ERROR_KEY,
AppConstants.LOGIN_ERROR_MESSAGE);
return AppConstants.LOGIN_PAGE;
}
This is just a code I wrote that allows a user to login into my application. As you can see, I am adding the name of the user, as well as error messages to the model object of type ModelMap. The used is then accessed in the jsp as ${user}.
The problem happened when I wanted to go from the home page to another page. So, heres what I did, I used an HttpSession object as :
session = request.getSession();
session.addAttribute("model",model);
and at the controller method for displaying the other page, I assigned the current model object with the one I had stored in the session as :
model = (ModelMap)session.getAttribute("model");
However, when the page was displayed, none of the data was retrievable using the ${ }. I checked value of the model.toString() object and found that the data is there and somehow the jsp is unable to access it. This is the problem.
It took the better part of the day googling and blind trials and my observation is that for model attributes to be set in the view, we must explicitly add using the add method of the ModelMap class, into the model object. Otherwise, the jstl view is not recognizing the attributes and hence not accessible from the jsp.
So, finally the solution is to store the data you want to pass as a bean in the session from the previous controller method and then retrieve the bean and add it to the model object as attributes at the destination controller method. I don’t have even the slightest clue as to why this behavior exists.

