服务器端通过:request.setAttribute(name, value);设置
jsp页面 ${name} 取不到值
通过查找资料网友给出相关的建议,在此记录下,以备后用:
解决方法:
在使用el表达式的jsp中配置:
- 1
该设置代表在本jsp中使用el表达式,可以解析其中的值。若isELIgnored设置为true,代表在本页不使用el表达式,当做字符串解析出来显示。此时,el表达式正常工作,显示正常。那么为什么会这样?首先查看,web.xml中配置的<web-app>
标签,jsp servlet版本是多少?发现之前配置如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
发现使用版本为2.3,google一下发现oracle官方如下描述:
- 1
- 2
- 3
大意就是:
如果isELIgnored是true,当EL表达式出现在文本或者标签属性时被忽略。如果是false,则EL表达式通过容器来决定如何解析,只有属性有返回表达式被设置为true或者表达式是一个延迟表达式时不解析。
isELIgnored的值取决于web应用部署描述符的版本。使用Servlet2.4的描述符的JSP页面默认是解析EL表达式,即表达式有效。这默认提供了大部分应用想要的情况。而使用Servlet2.3或者更早的描述符的JSP页面默认是忽略EL表达式的,即不解析EL表达式,这样就提供了向后兼容性。
Oracle文档:https://docs.oracle.com/cd/E19316-01/819-3669/bnaic/index.html
也就是说,描述符2.4或者更新的版本,isELIgnored默认值为false,而2.3或者 更早的版本isELIgnored默认值为true。就导致了出现EL表达式无效的情况。
至此,问题已经解决。
网上还有一个解决方案,本人测试是可以解决的:
在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"
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">