spring-mvc解决EL表达式不能使用问题

本文详细解释了在使用Spring MVC框架时,如何解决Controller层通过ModelAndView保存数据后,在JSP页面中使用EL表达式获取不到数据的问题。主要通过调整web.xml配置来开启JSP EL表达式支持,确保数据正确传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      刚开始学习spring mvc时经常会遇到 的一个问题就是在Controller层使用ModelAndView的addObject方法保存数据后,在jsp页面中使用EL表达式进行获取得不到数据,而是直接显示表达式的值,如${message},产生这个问题的原因主要是JSP1.2默认的EL表达式是关闭的,而JSP2.0默认的EL表达式是打开的。

      解决方法为: 

      1)、采用JSP1.2 

             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>
EL是关闭的,必须手动打开。<%@page isELIgnored="false" %>

      2)、采用JSP2.0

            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>
      可以直接使用。

      以下配置为成功的示例:

      web.xml文件       

<?xml version="1.0" encoding="UTF-8"?>
<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>
       jsp页面       

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
	${message}
</body>
</html>

       

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值