Spring MVC ModelAndView addObject 使用EL表达式失效

一个答案:

http://www.mkyong.com/spring-mvc/modelandviews-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”.

 

但是,自己不明白的是,自己的web.xml中,是这样的:

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

为什么就不行呢?

结果改成:

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

这样就可以了。

没有答案,留作纪念。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值