Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
8eca898d
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8eca898d
编写于
9月 24, 2009
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
removed outdated "cacheJspExpressions" feature from ExpressionEvaluationUtils
上级
c761082e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
12 addition
and
163 deletion
+12
-163
org.springframework.web/src/main/java/org/springframework/web/util/ExpressionEvaluationUtils.java
...g/springframework/web/util/ExpressionEvaluationUtils.java
+9
-105
org.springframework.web/src/test/java/org/springframework/web/util/ExpressionEvaluationUtilsTests.java
...ingframework/web/util/ExpressionEvaluationUtilsTests.java
+3
-58
未找到文件。
org.springframework.web/src/main/java/org/springframework/web/util/ExpressionEvaluationUtils.java
浏览文件 @
8eca898d
/*
* Copyright 2002-200
8
the original author or authors.
* Copyright 2002-200
9
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -16,14 +16,9 @@
package
org.springframework.web.util
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.servlet.ServletContext
;
import
javax.servlet.jsp.JspException
;
import
javax.servlet.jsp.PageContext
;
import
javax.servlet.jsp.el.ELException
;
import
javax.servlet.jsp.el.Expression
;
import
org.springframework.util.Assert
;
...
...
@@ -31,12 +26,6 @@ import org.springframework.util.Assert;
* Convenience methods for accessing JSP 2.0's
* {@link javax.servlet.jsp.el.ExpressionEvaluator}.
*
* <p>This class will by default use standard <code>evaluate</code> calls.
* If your application server happens to be inefficient in that respect,
* consider setting Spring's "cacheJspExpressions" context-param in
* <code>web.xml</code> to "true", which will use <code>parseExpression</code>
* calls with cached Expression objects instead.
*
* <p>The evaluation methods check if the value contains "${" before
* invoking the EL evaluator, treating the value as "normal" expression
* (i.e. a literal String value) else.
...
...
@@ -45,28 +34,14 @@ import org.springframework.util.Assert;
* @author Alef Arendsen
* @since 11.07.2003
* @see javax.servlet.jsp.el.ExpressionEvaluator#evaluate
* @see javax.servlet.jsp.el.ExpressionEvaluator#parseExpression
*/
public
abstract
class
ExpressionEvaluationUtils
{
/**
* JSP 2.0 expression cache parameter at the servlet context level
* (i.e. a context-param in <code>web.xml</code>): "cacheJspExpressions".
*/
public
static
final
String
EXPRESSION_CACHE_CONTEXT_PARAM
=
"cacheJspExpressions"
;
public
static
final
String
EXPRESSION_PREFIX
=
"${"
;
public
static
final
String
EXPRESSION_SUFFIX
=
"}"
;
private
static
final
String
EXPRESSION_CACHE_FLAG_CONTEXT_ATTR
=
ExpressionEvaluationUtils
.
class
.
getName
()
+
".CACHE_JSP_EXPRESSIONS"
;
private
static
final
String
EXPRESSION_CACHE_MAP_CONTEXT_ATTR
=
ExpressionEvaluationUtils
.
class
.
getName
()
+
".JSP_EXPRESSION_CACHE"
;
/**
* Check if the given expression value is an EL expression.
* @param value the expression to check
...
...
@@ -90,7 +65,7 @@ public abstract class ExpressionEvaluationUtils {
* the result class
*/
public
static
Object
evaluate
(
String
attrName
,
String
attrValue
,
Class
resultClass
,
PageContext
pageContext
)
throws
JspException
{
throws
JspException
{
if
(
isExpressionLanguage
(
attrValue
))
{
return
doEvaluate
(
attrName
,
attrValue
,
resultClass
,
pageContext
);
...
...
@@ -113,7 +88,7 @@ public abstract class ExpressionEvaluationUtils {
* @throws JspException in case of parsing errors
*/
public
static
Object
evaluate
(
String
attrName
,
String
attrValue
,
PageContext
pageContext
)
throws
JspException
{
throws
JspException
{
if
(
isExpressionLanguage
(
attrValue
))
{
return
doEvaluate
(
attrName
,
attrValue
,
Object
.
class
,
pageContext
);
...
...
@@ -132,7 +107,7 @@ public abstract class ExpressionEvaluationUtils {
* @throws JspException in case of parsing errors
*/
public
static
String
evaluateString
(
String
attrName
,
String
attrValue
,
PageContext
pageContext
)
throws
JspException
{
throws
JspException
{
if
(
isExpressionLanguage
(
attrValue
))
{
return
(
String
)
doEvaluate
(
attrName
,
attrValue
,
String
.
class
,
pageContext
);
...
...
@@ -170,7 +145,7 @@ public abstract class ExpressionEvaluationUtils {
* @throws JspException in case of parsing errors
*/
public
static
boolean
evaluateBoolean
(
String
attrName
,
String
attrValue
,
PageContext
pageContext
)
throws
JspException
{
throws
JspException
{
if
(
isExpressionLanguage
(
attrValue
))
{
return
(
Boolean
)
doEvaluate
(
attrName
,
attrValue
,
Boolean
.
class
,
pageContext
);
...
...
@@ -202,14 +177,14 @@ public abstract class ExpressionEvaluationUtils {
try
{
if
(
resultClass
.
isAssignableFrom
(
String
.
class
))
{
StringBuilder
resultValue
=
null
;
int
exprPrefixIndex
=
-
1
;
int
exprPrefixIndex
;
int
exprSuffixIndex
=
0
;
do
{
exprPrefixIndex
=
attrValue
.
indexOf
(
EXPRESSION_PREFIX
,
exprSuffixIndex
);
if
(
exprPrefixIndex
!=
-
1
)
{
int
prevExprSuffixIndex
=
exprSuffixIndex
;
exprSuffixIndex
=
attrValue
.
indexOf
(
EXPRESSION_SUFFIX
,
exprPrefixIndex
+
EXPRESSION_PREFIX
.
length
());
String
expr
=
null
;
String
expr
;
if
(
exprSuffixIndex
!=
-
1
)
{
exprSuffixIndex
+=
EXPRESSION_SUFFIX
.
length
();
expr
=
attrValue
.
substring
(
exprPrefixIndex
,
exprSuffixIndex
);
...
...
@@ -253,79 +228,8 @@ public abstract class ExpressionEvaluationUtils {
private
static
Object
evaluateExpression
(
String
exprValue
,
Class
resultClass
,
PageContext
pageContext
)
throws
ELException
{
Map
<
ExpressionCacheKey
,
Expression
>
expressionCache
=
getJspExpressionCache
(
pageContext
);
if
(
expressionCache
!=
null
)
{
// We are supposed to explicitly create and cache JSP Expression objects.
ExpressionCacheKey
cacheKey
=
new
ExpressionCacheKey
(
exprValue
,
resultClass
);
Expression
expr
=
expressionCache
.
get
(
cacheKey
);
if
(
expr
==
null
)
{
expr
=
pageContext
.
getExpressionEvaluator
().
parseExpression
(
exprValue
,
resultClass
,
null
);
expressionCache
.
put
(
cacheKey
,
expr
);
}
return
expr
.
evaluate
(
pageContext
.
getVariableResolver
());
}
else
{
// We're simply calling the JSP 2.0 evaluate method straight away.
return
pageContext
.
getExpressionEvaluator
().
evaluate
(
exprValue
,
resultClass
,
pageContext
.
getVariableResolver
(),
null
);
}
}
/**
* Determine whether JSP 2.0 expressions are supposed to be cached
* and return the corresponding cache Map, or <code>null</code> if
* caching is not enabled.
* @param pageContext current JSP PageContext
* @return the cache Map, or <code>null</code> if caching is disabled
*/
@SuppressWarnings
(
"unchecked"
)
private
static
Map
<
ExpressionCacheKey
,
Expression
>
getJspExpressionCache
(
PageContext
pageContext
)
{
ServletContext
servletContext
=
pageContext
.
getServletContext
();
Map
<
ExpressionCacheKey
,
Expression
>
cacheMap
=
(
Map
<
ExpressionCacheKey
,
Expression
>)
servletContext
.
getAttribute
(
EXPRESSION_CACHE_MAP_CONTEXT_ATTR
);
if
(
cacheMap
==
null
)
{
Boolean
cacheFlag
=
(
Boolean
)
servletContext
.
getAttribute
(
EXPRESSION_CACHE_FLAG_CONTEXT_ATTR
);
if
(
cacheFlag
==
null
)
{
cacheFlag
=
Boolean
.
valueOf
(
servletContext
.
getInitParameter
(
EXPRESSION_CACHE_CONTEXT_PARAM
));
servletContext
.
setAttribute
(
EXPRESSION_CACHE_FLAG_CONTEXT_ATTR
,
cacheFlag
);
}
if
(
cacheFlag
)
{
cacheMap
=
Collections
.
synchronizedMap
(
new
HashMap
<
ExpressionCacheKey
,
Expression
>());
servletContext
.
setAttribute
(
EXPRESSION_CACHE_MAP_CONTEXT_ATTR
,
cacheMap
);
}
}
return
cacheMap
;
}
/**
* Cache key class for JSP 2.0 Expression objects.
*/
private
static
class
ExpressionCacheKey
{
private
final
String
value
;
private
final
Class
resultClass
;
private
final
int
hashCode
;
public
ExpressionCacheKey
(
String
value
,
Class
resultClass
)
{
this
.
value
=
value
;
this
.
resultClass
=
resultClass
;
this
.
hashCode
=
this
.
value
.
hashCode
()
*
29
+
this
.
resultClass
.
hashCode
();
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(!(
obj
instanceof
ExpressionCacheKey
))
{
return
false
;
}
ExpressionCacheKey
other
=
(
ExpressionCacheKey
)
obj
;
return
(
this
.
value
.
equals
(
other
.
value
)
&&
this
.
resultClass
.
equals
(
other
.
resultClass
));
}
@Override
public
int
hashCode
()
{
return
this
.
hashCode
;
}
return
pageContext
.
getExpressionEvaluator
().
evaluate
(
exprValue
,
resultClass
,
pageContext
.
getVariableResolver
(),
null
);
}
}
org.springframework.web/src/test/java/org/springframework/web/util/ExpressionEvaluationUtilsTests.java
浏览文件 @
8eca898d
/*
* Copyright 2002-200
6
the original author or authors.
* Copyright 2002-200
9
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -26,17 +26,14 @@ import javax.servlet.jsp.el.VariableResolver;
import
junit.framework.TestCase
;
import
org.junit.Ignore
;
import
org.springframework.mock.web.MockExpressionEvaluator
;
import
org.springframework.mock.web.MockPageContext
;
import
org.springframework.mock.web.MockServletContext
;
/**
* @author Aled Arendsen
* @author Juergen Hoeller
* @since 16.09.2003
*/
@Ignore
// calls to deprecated getVariableResolver() are throwing UOEs
public
class
ExpressionEvaluationUtilsTests
extends
TestCase
{
public
void
testIsExpressionLanguage
()
{
...
...
@@ -156,7 +153,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertTrue
(
ExpressionEvaluationUtils
.
evaluateBoolean
(
"test"
,
"true"
,
ctx
));
}
public
void
test
EvaluateWithoutCaching
()
throws
Exception
{
public
void
test
RepeatedEvaluate
()
throws
Exception
{
PageContext
ctx
=
new
CountingMockPageContext
();
CountingMockExpressionEvaluator
eval
=
(
CountingMockExpressionEvaluator
)
ctx
.
getExpressionEvaluator
();
ctx
.
setAttribute
(
"bla"
,
"blie"
);
...
...
@@ -175,29 +172,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertEquals
(
4
,
eval
.
evaluateCount
);
}
public
void
testEvaluateWithCaching
()
throws
Exception
{
PageContext
ctx
=
new
CountingMockPageContext
();
CountingMockExpressionEvaluator
eval
=
(
CountingMockExpressionEvaluator
)
ctx
.
getExpressionEvaluator
();
ctx
.
setAttribute
(
"bla"
,
"blie"
);
ctx
.
setAttribute
(
"blo"
,
"blue"
);
MockServletContext
sc
=
(
MockServletContext
)
ctx
.
getServletContext
();
sc
.
addInitParameter
(
ExpressionEvaluationUtils
.
EXPRESSION_CACHE_CONTEXT_PARAM
,
"true"
);
assertEquals
(
"blie"
,
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
"${bla}"
,
String
.
class
,
ctx
));
assertEquals
(
1
,
eval
.
parseExpressionCount
);
assertEquals
(
"blue"
,
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
"${blo}"
,
String
.
class
,
ctx
));
assertEquals
(
2
,
eval
.
parseExpressionCount
);
assertEquals
(
"blie"
,
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
"${bla}"
,
String
.
class
,
ctx
));
assertEquals
(
2
,
eval
.
parseExpressionCount
);
assertEquals
(
"blue"
,
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
"${blo}"
,
String
.
class
,
ctx
));
assertEquals
(
2
,
eval
.
parseExpressionCount
);
}
public
void
testEvaluateWithConcatenationWithoutCaching
()
throws
Exception
{
public
void
testEvaluateWithComplexConcatenation
()
throws
Exception
{
PageContext
ctx
=
new
CountingMockPageContext
();
CountingMockExpressionEvaluator
eval
=
(
CountingMockExpressionEvaluator
)
ctx
.
getExpressionEvaluator
();
ctx
.
setAttribute
(
"bla"
,
"blie"
);
...
...
@@ -224,36 +199,6 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertEquals
(
8
,
eval
.
evaluateCount
);
}
public
void
testEvaluateWithConcatenationWithCaching
()
throws
Exception
{
PageContext
ctx
=
new
CountingMockPageContext
();
CountingMockExpressionEvaluator
eval
=
(
CountingMockExpressionEvaluator
)
ctx
.
getExpressionEvaluator
();
ctx
.
setAttribute
(
"bla"
,
"blie"
);
ctx
.
setAttribute
(
"blo"
,
"blue"
);
MockServletContext
sc
=
(
MockServletContext
)
ctx
.
getServletContext
();
sc
.
addInitParameter
(
ExpressionEvaluationUtils
.
EXPRESSION_CACHE_CONTEXT_PARAM
,
"true"
);
String
expr
=
"text${bla}text${blo}text"
;
Object
o
=
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
expr
,
String
.
class
,
ctx
);
assertEquals
(
"textblietextbluetext"
,
o
);
assertEquals
(
2
,
eval
.
parseExpressionCount
);
expr
=
"${bla}text${blo}text"
;
o
=
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
expr
,
String
.
class
,
ctx
);
assertEquals
(
"blietextbluetext"
,
o
);
assertEquals
(
2
,
eval
.
parseExpressionCount
);
expr
=
"${bla}text${blo}"
;
o
=
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
expr
,
String
.
class
,
ctx
);
assertEquals
(
"blietextblue"
,
o
);
assertEquals
(
2
,
eval
.
parseExpressionCount
);
expr
=
"${bla}text${blo}"
;
o
=
ExpressionEvaluationUtils
.
evaluate
(
"test"
,
expr
,
Object
.
class
,
ctx
);
assertEquals
(
"blietextblue"
,
o
);
assertEquals
(
2
,
eval
.
parseExpressionCount
);
}
private
static
class
CountingMockPageContext
extends
MockPageContext
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录