首页 > 其他分享 >EL表达式取出Map集合中key为Integer类型的值,bug解决方案

EL表达式取出Map集合中key为Integer类型的值,bug解决方案

时间:2022-12-28 12:35:43浏览次数:52  
标签:EL key map Student put new Map


EL表达式取出Map集合中key为Integer类型的值,bug解决方案


    今天,我在用EL表达式取Map集合中key为Integer类型的值时,发现无法取出。


    问题 Demo如下:


 <body>
<%
//创建Student对象
Student stu1 = new Student(1, "木丁西", '男', 24);
Student stu2 = new Student(2, "小龙女", '女', 14);
Student stu3 = new Student(3, "张馨予", '女', 28);
Student stu4 = new Student(4, "刘先生", '男', 35);
//创建Map集合对象
Map<Integer, Student> map = new HashMap();
//添加数据到 Map结合中
map.put(1, stu1);
map.put(2, stu2);
map.put(3, stu3);
map.put(4, stu4);
//保存集合对象到page域对象中。
pageContext.setAttribute("map", map);
%>

<%-- 使用EL表达式获取Map集合对象 --%>
EL表达式获取集合对象:${map }<br/>
EL表达式获取集合中的第1个对象:${map[1]}<br/>
</body>


效果:




EL表达式取出Map集合中key为Integer类型的值,bug解决方案_css



原因分析:

     从网上链接1: ​​http://www.codeweblog.com/el%E8%A1%A8%E8%BE%BE%E5%BC%8Fmap-key%E4%B8%BAinteger%E7%B1%BB%E5%9E%8B-%E5%8F%96%E5%80%BCbug%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/​​​  链接2:​​http://stackoverflow.com/questions/924451/el-access-a-map-value-by-integer-key​​ 获知。EL表达式在解析Integer类型数字的时候,会自动把数字转换成Long类型,后台使用Integer类型作为key值,进行判断的时候Integer与Long对象不相等。导致无法取出key值。



解决方案:使用Long类型作为Map中的键值类型。


Demo如下:



 <%@ page language="java" import="java.util.*, com.cn.entity.Student" pageEncoding="UTF-8"
session="true"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'el.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%
//创建Student对象
Student stu1 = new Student(1, "木丁西", '男', 24);
Student stu2 = new Student(2, "小龙女", '女', 14);
Student stu3 = new Student(3, "张馨予", '女', 28);
Student stu4 = new Student(4, "刘先生", '男', 35);
//创建Map集合对象
Map<Long, Student> map = new HashMap();
//添加数据到 Map结合中
map.put(1L, stu1);
map.put(2L, stu2);
map.put(3L, stu3);
map.put(4L, stu4);
//保存集合对象到page域对象中。
pageContext.setAttribute("map", map);
%>

<%-- 使用EL表达式获取Map集合对象 --%>
EL表达式获取集合对象:${map }<br/>
EL表达式获取集合中的第1个对象:${map[1]}<br/>
</body>
</html>

效果如下:



EL表达式取出Map集合中key为Integer类型的值,bug解决方案_bug_02


标签:EL,key,map,Student,put,new,Map
From: https://blog.51cto.com/u_15769923/5974279

相关文章

  • Manage Spring Boot Logs with Elasticsearch, Logstash and Kibana
    下载地址:https://www.elastic.co/downloads Whentimecomestodeployanewproject,oneoftenoverlookedaspectislogmanagement.ELKstack(Elasticsearch,Logs......
  • IntelliJ IDEA 编译方式介绍及查看内存占用
    编译方式介绍相比较于Eclipse的实时自动编译,IntelliJIDEA的编译更加手动化,虽然IntelliJIDEA也支持通过设置开启实时编译,但是不建议,因为太占资源了。IntelliJIDEA编......
  • Mapper的动态代理
    可以自动生产接口的实现类,所以就不需要再写daoImpl这个实现类了,直接使用sqlSession.getMapper自动生成实现类  @Before此注解的目的是为了将@Befoe作为首先执行的......
  • java stream map和 flatmap区别
    区别:mapmapper返回R,flatMapmapper返回Stream<R>官网解释1,<R> Stream<R>flatMap(Function<?super T,?extends Stream<?extendsR>> mapper) Returnsastream......
  • Java HashMap原理
    HashMap是Java中用于实现映射关系的一种数据结构。它允许将一个对象(称为键)映射到另一个对象(称为值)。当需要访问值时,可以使用键来查找值。HashMap的实现原理是使用散列函数......
  • react和vue中为什么用key、并且用key的时候为什么不能用index
    为什么用key在虚拟dom进行diff算法的时候,使用key可以对key进行比较然后来判断两个节点是否是同一节点,极大的增加了速度。为什么避免使用index先来总结好了的:如果只是......
  • 1.excel基础
    1.数据分析概述1.1数据分析定义数据分析指用适当的统计,分析方法对收集来的大量数据进行分析,将它们加以汇总和理解并消化,以求最大化的开发数据的功能,发挥数据的作用......
  • Java HashMap原理
    HashMap是Java中用于实现映射关系的一种数据结构。它允许将一个对象(称为键)映射到另一个对象(称为值)。当需要访问值时,可以使用键来查找值。HashMap的实现原理是使用散列函数......
  • 面试官问:为啥不建议使用 Select *?请你大声地回答他!!
    作者:小目标青年来源:https://blog.csdn.net/qq_35387940/article/details/125921218前言不建议使用select*这几个字眼,做开发的都不陌生吧。阿里的开发手册上面也......
  • 《HelloGitHub》第 81 期
    兴趣是最好的老师,HelloGitHub让你对编程感兴趣!简介HelloGitHub分享GitHub上有趣、入门级的开源项目。https://github.com/521xueweihan/HelloGitHub这里有实......