GoogleSearchBox

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:

 @RequestMapping("/hello")
 public ModelAndView helloWorld() {
  String message = "HELLO SPRING MVC";
  return new ModelAndView("abcd", "msg", message);
 }
 
        /* Even I tried below way of setting the variable msg */
 @RequestMapping("/welcome")
 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="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>

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


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"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

   <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>
</web-app>


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:
http://stackoverflow.com/tags/jstl/info