Custom Search

Wednesday, July 3, 2013

spring / JSTL $ variable does not display the value Or Spring $ variable not carrying into view / JSP

The expression language (EL, $ syntax) like ${message} doesn't work in my JSP / JSTL tags while working with Spring!
 I noticed this issue while working with Spring demo.

Lets discuss first what was my settings and what I tried to make it work, then will take about the solution.
I was setting the variable in the spring Controller like below:

 public ModelAndView helloWorld() {
  String message = "HELLO SPRING MVC";
  return new ModelAndView("abcd", "msg", message);
        /* Even I tried below way of setting the variable msg */
 public String welcomeWorld(Model m) {
  String s = "HELLO SPRING MVC";
  m.addAttribute("msg", s);
  return "welcome";

And I was trying to use the "msg" variable  in the JSP (abcd.jsp) as below:

<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="s" uri="" %>

Message is: <%= request.getAttribute("msg") %> <br/>
Message is: <c:out value="${msg}" /> <br/>
Message is: ${"msg"} <br/>
Message is: ${msg} <br/>

Tried every possible combination of displaying the variable in the jsp.
 But I was not getting any value corresponding to the variable when used along with the EL $, I used to get below response through JSP page when run under tomcat 5.5.x:

So, after googling out, I found that, its the Servlet and JSP versions not compatible with the server.
Basically in my web.xml I had declared the version of Servlet I am using is 2.5 as below:

<?xml version="1.0" encoding="UTF-8"?>



But, I was deploying above application on a Tomcat server of version 5.5.x (which supports Servlets version 2.4). And that was the main root cause.

So, When I deployed the application to Tomcat 6.x (supporting Servlets 2.5 and a higher version of JSP and EL), It worked fine.

Now I am getting the expected result of the above JSP (abcd.jsp) page, the output is shown below:

Hope this helps you too!!

References I followed: