`
jinnianshilongnian
  • 浏览: 21458201 次
  • 性别: Icon_minigender_1
博客专栏
5c8dac6a-21dc-3466-8abb-057664ab39c7
跟我学spring3
浏览量:2409811
D659df3e-4ad7-3b12-8b9a-1e94abd75ac3
Spring杂谈
浏览量:3001362
43989fe4-8b6b-3109-aaec-379d27dd4090
跟开涛学SpringMVC...
浏览量:5634405
1df97887-a9e1-3328-b6da-091f51f886a1
Servlet3.1规范翻...
浏览量:258555
4f347843-a078-36c1-977f-797c7fc123fc
springmvc杂谈
浏览量:1594603
22722232-95c1-34f2-b8e1-d059493d3d98
hibernate杂谈
浏览量:249440
45b32b6f-7468-3077-be40-00a5853c9a48
跟我学Shiro
浏览量:5851210
Group-logo
跟我学Nginx+Lua开...
浏览量:699569
5041f67a-12b2-30ba-814d-b55f466529d5
亿级流量网站架构核心技术
浏览量:782089
社区版块
存档分类
最新评论

jsp EL表达式中令人郁闷的int/float/char

 
阅读更多

在EL表达式计算过程中,有朋友会遇到许多奇怪的问题,经常非常郁闷,在此我把这些总结一下,方便查询:

1、所有的整数数字字面量都是Long类型的;

2、所有小数字面量都是Double类型的;

3、""或''声明的是字符串,即''也是字符串,非char;

4、比较时都是equals比较。

 

接下来看几个可能出问题的例子,你会遇到一下的几个呢:

1、

如${1+2147483647} 结果是多少?

如果在java程序里边运行会得到-2147483648,而在jsp el中会得到2147483648。

 

2、

<%

    Map<Object, Object> map = new HashMap<Object, Object>();

    map.put(new Long(1), 123);

    request.setAttribute("map", map);

    request.setAttribute("a", new Long(1));

%>

${map[1]}  正确

${map[a]} 正确

 

3、

<%

    Map<Object, Object> map = new HashMap<Object, Object>();

    map.put(new Integer(1), 123);

    request.setAttribute("map", map);

    request.setAttribute("a", new Long(1));

    request.setAttribute("b", new Integer(1));

%>

${map[1]}  错误

${map[a]}  错误

${map[b]}  正确

 

4、

<%

    Map<Object, Object> map = new HashMap<Object, Object>();

    map.put(1.1, 123); //map.put(1.1d, 123);

    request.setAttribute("map", map);

    request.setAttribute("a", new Double(1.1));

%>

map.a=${map[1.1]}  正确

map.a=${map[a]}     正确

 

5、

<%

    Map<Object, Object> map = new HashMap<Object, Object>();

    map.put(1.1f, 123); //map.put(new Float(1.1), 123);

    request.setAttribute("map", map);

    request.setAttribute("a", new Double(1.1));

    request.setAttribute("b", new Float(1.1));

%>

map.a=${map[1.1]}  错误

map.a=${map[a]}     错误

map.a=${map[b]}     正确

 

6、

结合struts2的ognl表达式

<s:property value="#map = #{'a':123, 'b':234}, ''"/>  --->定义一个map,放入值栈的上下文区

<s:property value="#map['a']"/> ---------->正确,因为其支持char

${map['a']} ------------>错误, 因为'a'在jsp el表达式中是字符串,不能=char。

 

<s:property value='#map = #{"a":123, "b":234}, ""'/>  --->此时key是字符串

${map['a']}

 

此处需要注意ognl中'×××' 如果长度是1那么是Character 否则是String 可参考

http://jinnianshilongnian.iteye.com/blog/1870662

 

补充:

在EL表达式规范2.2中,定义了:

写道
■ The value of an IntegerLiteral ranges from Long.MIN_VALUE to
Long.MAX_VALUE
■ The value of a FloatingPointLiteral ranges from Double.MIN_VALUE to
Double.MAX_VALUE

 在tomcat7.0.6实现中,jasper.jar(实现了EL2.2规范):

AstFloatingPoint表示小数,AstInteger表示整数,其定义如下:

public final class AstInteger extends SimpleNode
{
  private volatile Number number;

  public AstInteger(int id)
  {
    super(id);
  }

  protected Number getInteger()
  {
    if (this.number == null) {
      try {
        this.number = new Long(this.image);
      } catch (ArithmeticException e1) {
        this.number = new BigInteger(this.image);
      }
    }
    return this.number;
  }

  public Class<?> getType(EvaluationContext ctx)
    throws ELException
  {
    return getInteger().getClass();
  }

  public Object getValue(EvaluationContext ctx)
    throws ELException
  {
    return getInteger();
  }
}

 

public final class AstFloatingPoint extends SimpleNode
{
  private volatile Number number;

  public AstFloatingPoint(int id)
  {
    super(id);
  }

  public Number getFloatingPoint()
  {
    if (this.number == null) {
      try {
        this.number = new Double(this.image);
      } catch (ArithmeticException e0) {
        this.number = new BigDecimal(this.image);
      }
    }
    return this.number;
  }

  public Object getValue(EvaluationContext ctx)
    throws ELException
  {
    return getFloatingPoint();
  }

  public Class<?> getType(EvaluationContext ctx)
    throws ELException
  {
    return getFloatingPoint().getClass();
  }
}

 

+ - * /实现,此处只看+的:

package org.apache.el.parser;

import javax.el.ELException;
import org.apache.el.lang.ELArithmetic;
import org.apache.el.lang.EvaluationContext;

public final class AstPlus extends ArithmeticNode
{
  public AstPlus(int id)
  {
    super(id);
  }

  public Object getValue(EvaluationContext ctx)
    throws ELException
  {
    Object obj0 = this.children[0].getValue(ctx);
    Object obj1 = this.children[1].getValue(ctx);
    return ELArithmetic.add(obj0, obj1);
  }
}

 其委托给ELArithmetic.add:

 public static final DoubleDelegate DOUBLE = new DoubleDelegate();

  public static final LongDelegate LONG = new LongDelegate();

  private static final Long ZERO = Long.valueOf(0L);

  public static final Number add(Object obj0, Object obj1) {
    if ((obj0 == null) && (obj1 == null))
      return Long.valueOf(0L);
    ELArithmetic delegate;
    if (BIGDECIMAL.matches(obj0, obj1))
      delegate = BIGDECIMAL;
    else if (DOUBLE.matches(obj0, obj1))
      if (BIGINTEGER.matches(obj0, obj1))
        delegate = BIGDECIMAL;
      else
        delegate = DOUBLE;
    else if (BIGINTEGER.matches(obj0, obj1))
      delegate = BIGINTEGER;
    else {
      delegate = LONG;
    }
    Number num0 = delegate.coerce(obj0);
    Number num1 = delegate.coerce(obj1);

    return delegate.add(num0, num1);
  }

 此处委托给了各种delegate计算,其+的实现:

public static final class LongDelegate extends ELArithmetic
  {
    protected Number add(Number num0, Number num1)
    {
      return Long.valueOf(num0.longValue() + num1.longValue());
    }

 从这里我们可以看出其实现。

 

而且其规范中都规定了具体字面量的东西:

写道
1.3 Literals
There are literals for boolean, integer, floating point, string, and null in an eval-
expression.
■ Boolean - true and false
■ Integer - As defined by the IntegerLiteral construct in Section 1.19
■ Floating point - As defined by the FloatingPointLiteral construct in
Section 1.19
■ String - With single and double quotes - " is escaped as \", ' is escaped as \',
and \ is escaped as \\. Quotes only need to be escaped in a string value enclosed
in the same type of quote
■ Null - null

 

也规定了操作符的运算规则,如+ - *:

1.3 Literals
There are literals for boolean, integer, floating point, string, and null in an eval-expression.
  ■ Boolean - true and false
  ■ Integer - As defined by the IntegerLiteral construct in Section 1.19
  ■ Floating point - As defined by the FloatingPointLiteral construct in
Section 1.19
  ■ String - With single and double quotes - " is escaped as \", ' is escaped as \',and \ is escaped as \\. Quotes only need to be escaped in a string value enclosed in the same type of quote
  ■ Null - null
  ■ If operator is -, return A.subtract(B)
  ■ If operator is *, return A.multiply(B)
  ■ If A or B is a Float, Double,or String containing ., e,or E:
  ■ If A or B is BigInteger, coerce both A and B to BigDecimal and apply operator.
  ■ Otherwise, coerce both A and B to Double and apply operator
  ■ If A or B is BigInteger, coerce both to BigInteger and then:
  ■ If operator is +, return A.add(B)
  ■ If operator is -, return A.subtract(B)
  ■ If operator is *, return A.multiply(B)
  ■ Otherwise coerce both A and B to Long and apply operator
  ■ If operator results in exception, error

如Integer型,直接交给前边介绍的IntegerLiteral。

 

即规范中其实已经规范了这些,但是就像java里的一些东西,虽然规范规定了(如排序时 很多人有时候使用 return a-b; 如果a是负数则可能溢出),但是还是很容易出错。

15
7
分享到:
评论
11 楼 飞天奔月 2013-05-23  
能整理得这么完整  不容易

我~顶
10 楼 jinnianshilongnian 2013-05-16  
jackyrong 写道
但我刚刚在GOOGLE 广告中,设置jackyrong.javaeye.com,说不能用这个格式的网址?

以前可以 现在好像只能用一级域名 我是用别的一级域名申请的
9 楼 jackyrong 2013-05-16  
但我刚刚在GOOGLE 广告中,设置jackyrong.javaeye.com,说不能用这个格式的网址?
8 楼 jinnianshilongnian 2013-05-16  
jackyrong 写道
请问你的广告条是如何申请和设置的?谢谢

设置的话:
在 博客设置 下边 把相应的Google AdSense 的一些信息粘贴上即可。
7 楼 jinnianshilongnian 2013-05-16  
jackyrong 写道
请问你的广告条是如何申请和设置的?谢谢

去google ad  申请了好久  而且刚开始老是失败 突然一天就好了
6 楼 jackyrong 2013-05-16  
请问你的广告条是如何申请和设置的?谢谢
5 楼 jinnianshilongnian 2013-05-16  
thihy 写道
看JSTL标准就理解了。

其实规范吧,说的很明白,不过有多人真正的去读规范了 ,如一个刚入行没多久的不太可能读规范吧,很多人问这个,所以就整理下发上来了,早上补充了规范上的定义和代码实现。
4 楼 jinnianshilongnian 2013-05-16  
thihy 写道
看JSTL标准就理解了。

■ The value of an IntegerLiteral ranges from Long.MIN_VALUE to
Long.MAX_VALUE
■ The value of a FloatingPointLiteral ranges from Double.MIN_VALUE to
Double.MAX_VALUE


public final class AstInteger extends SimpleNode 

  private volatile Number number; 
 
  public AstInteger(int id) 
  { 
    super(id); 
  } 
 
  protected Number getInteger() 
  { 
    if (this.number == null) { 
      try { 
        this.number = new Long(this.image); 
      } catch (ArithmeticException e1) { 
        this.number = new BigInteger(this.image); 
      } 
    } 
    return this.number; 
  } 


3 楼 jinnianshilongnian 2013-05-16  
thihy 写道
看JSTL标准就理解了。

不是jstl 而是el规范,我补充了上去
2 楼 thihy 2013-05-15  
看JSTL标准就理解了。
1 楼 jinnianshilongnian 2013-05-15  
怎么我加红色的部分全没了。。

相关推荐

    jsp el表达式详解

    JSP EL预定义了一些隐式对象,如`pageContext`、`request`、`response`等,可以直接在EL表达式中使用。这使得开发者无需编写额外的JSP脚本就能访问请求参数或响应头。 **7. EL的优点** - **简洁性**:EL的语法简洁...

    jsp中的EL表达式简介

    EL(Expression Language)是JavaServer Pages(JSP)中的一种简洁的表达式语言,设计的初衷是为了简化JSP页面的编写。EL受到ECMAScript和XPath表达式语言的启发,提供了更高效的方式来访问和操作JSP页面上的数据。 ...

    java中JSP和el表达式的隐含对象

    java中JSP和el表达式的隐含对象,能够让你对对jsp的更熟悉

    JSP-EL表达式.ppt

    JSP-EL 表达式是一种用于简化在 JSP 中访问变量的方式的表达式语言,Full Name 称为 Expression Language。JSP-EL 表达式的主要目的是简化静态 HTML 与 Java 代码的耦合,提供了一个灵活的方式来访问变量和对象。 ...

    jsp el表达式

    在JavaWeb开发中,JSP Expression Language(EL表达式)是一种简洁、强大的脚本语言,用于在JavaServer Pages(JSP)中获取和操作JavaBean或其他数据源中的数据。EL表达式的引入是为了简化JSP页面的编写,减少Java...

    EL表达式的使用详解

    EL 表达式是一种在 Java 服务器页面(JSP)中使用的表达式语言。它提供了一种简单的方式来访问和操作 Java 对象的属性。EL 表达式广泛应用于 JSP、Servlet、JSF 等 Web 开发技术中。本文将详细介绍 EL 表达式的使用...

    jsp页面中EL表达式被当成字符串处理不显示值问题的解决方法

    下面小编就为大家带来一篇jsp页面中EL表达式被当成字符串处理不显示值问题的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    jsp.el表达式.txt

    el表达式,jsp.el表达式,页面el表达式,el表达式注释 el表达式的解释,el表达式的应用,java el表达式

    jspEL表达式和el

    JSP Expression Language,简称EL,是JavaServer Pages(JSP)2.0版本引入的一种轻量级的表达式语言,用于在JSP页面中方便地访问JavaBean属性和其它Java对象。EL的目标是简化JSP中的脚本元素,提高开发效率,使...

    java jsp EL表达式

    EL表达式是Java服务器页面(JSP)的一种特殊语言,用于在Web应用程序中输出文本到页面。它的主要功能是从某个范围中找到一个JavaBean对象,并显示其某个属性值。EL表达式的基本格式为${ },它可以在JSP页面中使用,...

    EL表达式EL表达式

    **EL表达式(Expression Language)**是Java服务器页面(JSP)技术中的一个重要组成部分,它提供了一种简洁而强大的方式来访问和操作数据,如JavaBeans属性、JSP作用域中的对象等。EL表达式的设计目标是简化JSP页面的...

    jsp中的EL表达式

    JSP中EL表达式,主要介绍EL的详细用法,熟练掌握EL知识。

    JSP EL表达式 代码案例快速入门

    JSP Expression Language,简称EL,是JavaServer Pages(JSP)中的一个强大而简洁的表达式语法,用于在JSP页面中简便地访问JavaBean属性、Java集合以及Servlet上下文中的数据。EL的主要目标是将业务逻辑与页面展示...

    JSP写EL表达式所需的两个jar包

    为了在JSP页面中实现服务器端的数据处理和展示,我们有时会避免直接在页面上编写Java代码,转而使用表达式语言(Expression Language,简称EL)。EL提供了一种简洁的方式来访问和操作JavaBean或其他作用域内的对象,...

    在JSP页面用EL表达式调用一些函数

    **在JSP页面中使用EL表达式调用函数** EL(Expression Language,表达式语言)是JavaServer Pages(JSP)技术的一个重要组成部分,它的主要目的是简化JSP页面中的脚本编写,使得开发者能更专注于页面展示逻辑,而...

    EL表达式使用文档,方便快速使用EL表达式.pdf

    EL 表达式(Express Language)是一种强大的表达式语言,用于简化 JSP 页面中的编程。EL 表达式可以嵌入在 JSP 页面内部,减少 JSP 脚本的编写,目的是要替代 JSP 页面中脚本的编写。 EL 表达式的主要作用是获得四...

    EL表达式的语法介绍

    EL 表达式是 Java 服务器页面(JSP)和 Java 服务器面板(JSF)中使用的一种表达式语言,用于在 Web 应用程序中实现动态内容。EL 表达式可以被解析成数值表达式和方法表达式,其中取值表达式用于引用一个值,而方法...

    jsp el 表达式语言文档

    1. **EL上下文**:EL表达式在EL上下文中执行,它可以访问JSP作用域内的所有对象(page,request,session,application)。 2. **作用域优先级**:EL会按照page -&gt; request -&gt; session -&gt; application的顺序查找变量...

    JSTL,EL表达式语法简介

    JSTL的核心是与Java Expression Language(EL)紧密集成,EL则是一个用于在JSP页面中获取和操作数据的简洁表达式语言。 **EL(Expression Language)**是Java Servlet 2.4及更高版本中引入的一种轻量级脚本语言。它...

    EL表达式的详细使用

    例如,EL 表达式可以使用 ${pageScope.objectName} 访问一个 JSP 中页面范围的对象,还可以使用 ${pageScope.objectName.attributeName} 访问对象的属性。 requestScope 将请求范围的变量名称映射到其值。该对象...

Global site tag (gtag.js) - Google Analytics