转载 

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

分类:    888人阅读    IT小君  2015-08-28 21:46

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”.


This is my code to fix the problem:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
	     xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
	     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
	<display-name>Archetype Created Web Application</display-name>
	
......
	
</web-app>

Reference

  1. Write JSP code that uses the directives
点击广告,支持我们为你提供更好的服务

html5 canvas进度条圆环图表统计动画特效

HTML5现代家居装潢公司网站模板

HTML5 Canvas竖直流动线条背景动画特效

有机水果蔬菜HTML5网站模板

响应式太阳能能源公司网站模板

响应式咖啡饮品宣传网站模板

现代时尚家具公司网站模板

jQuery右端悬浮带返回顶部特效

html5 svg夜空中星星流星动画场景特效

中小型创意设计服务公司网站模板

canvas炫酷鼠标移动文字粒子特效

小众时尚单品在线电子商务网站模板

css+js实现的颜色渐变数字时钟动画特效

js+css3抽奖转盘旋转点餐代码

html5 canvas彩色碎片组合球形旋转动画特效

HTML5数字产品服务公司网站模板

html5图标下拉搜索框自动匹配代码

网页设计开发公司网站模板

响应式时尚单品在线商城网站模板

css鼠标跟随文字模糊特效

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务