ModelAndView’s model value is not displayed in JSP via EL

Problem

In Spring MVC development, developer try to set a value into a model, and display the value in JSP via EL, e.g ${msg}, but it just outputs the result as it is – ${msg}, not the “value” stored in the model. The EL is just not working in JSP, why?

Spring’s Controller


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class ABCController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		ModelAndView model = new ModelAndView("HelloWorldPage");
		model.addObject("msg", "hello world");
		
		return model;
	}
	
}

JSP page


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
             ${msg}
</body>
</html>

Solution

This is the common asked question in the most Spring MVC hello world example. Actually it’s caused by the old JSP 1.2 descriptor.

1. JSP 1.2

If you are using the old JSP 1.2 descriptor, defined by DTD ,for example
web.xml


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
//...
</web-app>

The EL is disabled or ignored by default, you have to enable it manually, so that it will outputs the value store in the “msg” model.


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<%@ page isELIgnored="false" %>
</head>
<body>
           ${msg}
</body>
</html>

2. JSP 2.0

If you are using the standard JSP 2.0 descriptor, defined by w3c schema ,for example
web.xml


<web-app id="WebApp_ID" version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
//...
</web-app>

The EL is enabled by default, and you should see the value stored in the “msg” model, which is “hello world”.

Reference

  1. Write JSP code that uses the directives

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Tanmay Shah
Guest
Tanmay Shah

Thanks for sharing helpful information.. Saved my time…

Quoc
Guest
Quoc

Thank you very much! It saved my day

ulusalhafsa
Guest
ulusalhafsa

thank you so much!

henry
Guest
henry

Thank you, I spent weeks looking for a fix. You saved my day

cristian
Guest
cristian

Thanks!!!!

zeeshan
Guest
zeeshan

thanks alot mkyong, i almost gave up….u saved me…

Kaushik Lele
Guest
Kaushik Lele

Wow. same problem I faced. Every Spring MVC tutorial should include this.

Chaojun Zhong
Guest
Chaojun Zhong

Save me a day!!!

Prashant
Guest
Prashant

Thanks good job!

Rakesh Thakur
Guest
Rakesh Thakur

Thanks a lot for the great help. I wasted 1 day to search why ${} is not working

mabrouk monsif
Guest
mabrouk monsif

Thanks a lot man,this helped a lot.

Robert Rusu
Guest
Robert Rusu

Thanks!!!

Manik
Guest
Manik

Awesome! I was literally scratching my head for hours on why ${} is not working. Thanks a lot.

Ashish Aggarwal
Guest
Ashish Aggarwal

can we skip this JSTL part and use the controller passed variables directly like:

instead of ${message}

dsfgsdeftg435435643
Guest
dsfgsdeftg435435643

890870

fabian
Guest
fabian

this was just my problem, thank you

NicholasLe
Guest
NicholasLe

Great. Thank you so much

Vadim
Guest
Vadim

THANK YOU VERY MUCH!

zahra
Guest
zahra

@mkyong:
u are excellent;)

Pat
Guest
Pat

You are a life saver. I just spent 2 frustrating hours trying to figure out why? Being a beginner does not help. You just made me pain free. Thanks.

tony
Guest
tony

Awesome! Solve the problem which confused me for a very long time. Thank you guy!

?
Guest
?

Thank you! I spent 3 hours searching for the problem in Spring config, but it was just web.xml :-D

Heppy
Guest
Heppy

Hi do you know if its possible to add an object via spring mvc such as…

model.addObject(“msg”, “hello world”);

and then access the value directly in javascript without writing out to the JSP?

$(document).ready(function() {
alert(${msg}) ;
}

something like that?!

llhhss
Guest
llhhss

I seldom write a comment but this time I really want to thank you for this solution

Landei
Guest
Landei

Thanks for the hint, I was close to getting crazy! We had a project running on Tomcat without any attributes in web-app at all (don’t ask how *that* worked – I have no clue), and I couldn’t get it running on Jetty. Of course after adding the attributes it worked like a charm…

Victor Hugo
Guest
Victor Hugo

Many thanks man, help me a lot!

vineet tyagi
Guest
vineet tyagi

I am using servlet 3.0 version but same problem here.

Antonio
Guest
Antonio

Thanks man!
I got something similar. In the web.xml I changed the version=”2.4″ to Version=”2.5″ when I upgraded Tomcat and all my jsps stopped getting the Model values. The problem was the V in upper case in the version attribute. I was totally lost because I thought it was because of the new tomcat version….your post hinted me that the problem was in the web.xml header. Thanks again

ivan
Guest
ivan

mkyong,thank you very much!

Ankit Rastogi
Guest
Ankit Rastogi

Thanks so much for this article, I was googling to resolve this issue but no one has mentioned the resolution anywhere. This is very simple thing but is very useful.