首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

使用ognl in表达式可能会遇到的有关问题

2013-11-14 
使用ognl in表达式可能会遇到的问题package cnpublic class A {private Integer idpublic Integer getId

使用ognl in表达式可能会遇到的问题
package cn;public class A {private Integer id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((id == null) ? 0 : id.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;A other = (A) obj;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;return true;}}

?

<%A a1 = new A();a1.setId(1);A a2 = new A();a2.setId(2);List<A> list = new ArrayList();list.add(a1);list.add(a2);request.setAttribute("list", list);%><s:iterator value="#request.list" var="a"><s:property value="#a in #request.list"/><br/></s:iterator></body></html>

?

期望输出:
true
true

但是实际是:
true

如果配置struts.el.throwExceptionOnFailure=true,那么会得到如下异常:?

invalid comparison: cn.A and cn.A - Class: ognl.OgnlOps
File: OgnlOps.java
Method: compareWithConversion

?

原因在其ognl LanguageGuide 中也说了:

case NONNUMERIC: if ( ( t1 == NONNUMERIC ) && ( t2 == NONNUMERIC ) ) { if ( ( v1 instanceof Comparable ) && v1.getClass().isAssignableFrom( v2.getClass() ) ) { result = ( (Comparable) v1 ).compareTo( v2 ); break; } throw new IllegalArgumentException( "invalid comparison: " + v1.getClass().getName() + " and " + v2.getClass().getName() ); } // else fall through

?

可以改造为如下来解决:

case NONNUMERIC:                    if ( ( t1 == NONNUMERIC ) && ( t2 == NONNUMERIC ) )                    {                        if ( ( v1 instanceof Comparable ) && v1.getClass().isAssignableFrom( v2.getClass() ) )                        {                            result = ( (Comparable) v1 ).compareTo( v2 );                            break;                        }                        result = -1;                    }

?即最后不抛出异常,而是为-1。

?

提交问题后(https://issues.apache.org/jira/browse/WW-4230),人家觉得没改的必要,那就无所谓了,知道问题即可。

   不想太累了

热点排行