Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
cdee5381
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,发现更多精彩内容 >>
提交
cdee5381
编写于
2月 15, 2010
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed rendering of select options for multi-list (SPR-6799)
上级
d9112d07
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
125 addition
and
23 deletion
+125
-23
org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java
...ngframework/validation/AbstractPropertyBindingResult.java
+15
-7
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/OptionWriter.java
...g/springframework/web/servlet/tags/form/OptionWriter.java
+12
-13
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/SelectTag.java
.../org/springframework/web/servlet/tags/form/SelectTag.java
+1
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java
...springframework/web/servlet/tags/form/SelectTagTests.java
+97
-2
未找到文件。
org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java
浏览文件 @
cdee5381
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
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.
...
...
@@ -145,14 +145,22 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
*/
@Override
public
PropertyEditor
findEditor
(
String
field
,
Class
valueType
)
{
if
(
valueType
==
null
)
{
valueType
=
getFieldType
(
field
);
Class
valueTypeForLookup
=
valueType
;
if
(
valueTypeForLookup
==
null
)
{
valueTypeForLookup
=
getFieldType
(
field
);
}
PropertyEditor
editor
=
super
.
findEditor
(
field
,
valueType
);
PropertyEditor
editor
=
super
.
findEditor
(
field
,
valueType
ForLookup
);
if
(
editor
==
null
&&
this
.
conversionService
!=
null
)
{
TypeDescriptor
td
=
(
field
!=
null
?
getPropertyAccessor
().
getPropertyTypeDescriptor
(
fixedField
(
field
))
:
TypeDescriptor
.
valueOf
(
valueType
));
TypeDescriptor
td
=
null
;
if
(
field
!=
null
)
{
TypeDescriptor
ptd
=
getPropertyAccessor
().
getPropertyTypeDescriptor
(
fixedField
(
field
));
if
(
valueType
==
null
||
valueType
.
isAssignableFrom
(
ptd
.
getType
()))
{
td
=
ptd
;
}
}
if
(
td
==
null
)
{
td
=
TypeDescriptor
.
valueOf
(
valueTypeForLookup
);
}
if
(
this
.
conversionService
.
canConvert
(
TypeDescriptor
.
valueOf
(
String
.
class
),
td
))
{
editor
=
new
ConvertingPropertyEditorAdapter
(
this
.
conversionService
,
td
);
}
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/OptionWriter.java
浏览文件 @
cdee5381
...
...
@@ -16,8 +16,8 @@
package
org.springframework.web.servlet.tags.form
;
import
java.beans.PropertyEditor
;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.Map
;
import
javax.servlet.jsp.JspException
;
...
...
@@ -158,17 +158,16 @@ class OptionWriter {
* @see #renderOption(TagWriter, Object, Object, Object)
*/
private
void
renderFromMap
(
final
TagWriter
tagWriter
)
throws
JspException
{
Map
optionMap
=
(
Map
)
this
.
optionSource
;
for
(
Iterator
iterator
=
optionMap
.
entrySet
().
iterator
();
iterator
.
hasNext
();)
{
Map
.
Entry
entry
=
(
Map
.
Entry
)
iterator
.
next
();
Map
<?,
?>
optionMap
=
(
Map
)
this
.
optionSource
;
for
(
Map
.
Entry
entry
:
optionMap
.
entrySet
())
{
Object
mapKey
=
entry
.
getKey
();
Object
mapValue
=
entry
.
getValue
();
BeanWrapper
mapKeyWrapper
=
PropertyAccessorFactory
.
forBeanPropertyAccess
(
mapKey
);
BeanWrapper
mapValueWrapper
=
PropertyAccessorFactory
.
forBeanPropertyAccess
(
mapValue
);
Object
renderValue
=
(
this
.
valueProperty
!=
null
?
mapKey
Wrapper
.
getPropertyValue
(
this
.
valueProperty
)
:
mapKey
.
toString
());
Object
renderLabel
=
(
this
.
labelProperty
!=
null
?
mapValue
Wrapper
.
getPropertyValue
(
this
.
labelProperty
)
:
mapValue
.
toString
());
Object
renderValue
=
(
this
.
valueProperty
!=
null
?
mapKeyWrapper
.
getPropertyValue
(
this
.
valueProperty
)
:
mapKey
.
toString
());
Object
renderLabel
=
(
this
.
labelProperty
!=
null
?
mapValueWrapper
.
getPropertyValue
(
this
.
labelProperty
)
:
mapValue
.
toString
());
renderOption
(
tagWriter
,
mapKey
,
renderValue
,
renderLabel
);
}
}
...
...
@@ -196,16 +195,15 @@ class OptionWriter {
* {@link #labelProperty} property is used when rendering the label.
*/
private
void
doRenderFromCollection
(
Collection
optionCollection
,
TagWriter
tagWriter
)
throws
JspException
{
for
(
Iterator
it
=
optionCollection
.
iterator
();
it
.
hasNext
();)
{
Object
item
=
it
.
next
();
for
(
Object
item
:
optionCollection
)
{
BeanWrapper
wrapper
=
PropertyAccessorFactory
.
forBeanPropertyAccess
(
item
);
Object
value
;
if
(
this
.
valueProperty
!=
null
)
{
value
=
wrapper
.
getPropertyValue
(
this
.
valueProperty
);
}
}
else
if
(
item
instanceof
Enum
)
{
value
=
((
Enum
<?>)
item
).
name
();
}
}
else
{
value
=
item
;
}
...
...
@@ -243,7 +241,8 @@ class OptionWriter {
* HTML-escaped as required.
*/
private
String
getDisplayString
(
Object
value
)
{
return
ValueFormatter
.
getDisplayString
(
value
,
this
.
bindStatus
.
getEditor
(),
this
.
htmlEscape
);
PropertyEditor
editor
=
(
value
!=
null
?
this
.
bindStatus
.
findEditor
(
value
.
getClass
())
:
null
);
return
ValueFormatter
.
getDisplayString
(
value
,
editor
,
this
.
htmlEscape
);
}
/**
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/SelectTag.java
浏览文件 @
cdee5381
...
...
@@ -205,7 +205,7 @@ public class SelectTag extends AbstractHtmlInputElementTag {
if
(
items
!=
null
)
{
// Items specified, but might still be empty...
if
(
items
!=
EMPTY
)
{
Object
itemsObject
=
(
items
instanceof
String
?
evaluate
(
"items"
,
(
String
)
items
)
:
items
);
Object
itemsObject
=
evaluate
(
"items"
,
items
);
if
(
itemsObject
!=
null
)
{
String
valueProperty
=
(
getItemValue
()
!=
null
?
ObjectUtils
.
getDisplayString
(
evaluate
(
"itemValue"
,
getItemValue
()))
:
null
);
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java
浏览文件 @
cdee5381
...
...
@@ -19,6 +19,7 @@ package org.springframework.web.servlet.tags.form;
import
java.beans.PropertyEditor
;
import
java.beans.PropertyEditorSupport
;
import
java.io.StringReader
;
import
java.text.ParseException
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Comparator
;
...
...
@@ -28,7 +29,6 @@ import java.util.List;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.TreeMap
;
import
javax.servlet.jsp.JspException
;
import
javax.servlet.jsp.tagext.Tag
;
...
...
@@ -40,6 +40,8 @@ import org.dom4j.io.SAXReader;
import
org.springframework.beans.TestBean
;
import
org.springframework.beans.propertyeditors.CustomCollectionEditor
;
import
org.springframework.format.Formatter
;
import
org.springframework.format.support.FormattingConversionService
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.validation.BeanPropertyBindingResult
;
import
org.springframework.validation.BindingResult
;
...
...
@@ -59,7 +61,7 @@ public class SelectTagTests extends AbstractFormTagTests {
private
SelectTag
tag
;
private
TestBean
bean
;
private
TestBean
WithRealCountry
bean
;
protected
void
onSetUp
()
{
...
...
@@ -454,9 +456,102 @@ public class SelectTagTests extends AbstractFormTagTests {
Element
e
=
(
Element
)
selectElement
.
selectSingleNode
(
"option[@value = 'UK']"
);
assertEquals
(
"UK node not selected"
,
"selected"
,
e
.
attribute
(
"selected"
).
getValue
());
assertEquals
(
"United Kingdom(UK)"
,
e
.
getText
());
e
=
(
Element
)
selectElement
.
selectSingleNode
(
"option[@value = 'AT']"
);
assertEquals
(
"AT node not selected"
,
"selected"
,
e
.
attribute
(
"selected"
).
getValue
());
assertEquals
(
"Austria(AT)"
,
e
.
getText
());
}
public
void
testWithElementConverter
()
throws
Exception
{
this
.
bean
.
setRealCountry
(
Country
.
COUNTRY_UK
);
BeanPropertyBindingResult
errors
=
new
BeanPropertyBindingResult
(
this
.
bean
,
COMMAND_NAME
);
FormattingConversionService
cs
=
new
FormattingConversionService
();
cs
.
addFormatterForFieldType
(
Country
.
class
,
new
Formatter
<
Country
>()
{
public
String
print
(
Country
object
,
Locale
locale
)
{
return
object
.
getName
();
}
public
Country
parse
(
String
text
,
Locale
locale
)
throws
ParseException
{
return
new
Country
(
text
,
text
);
}
});
errors
.
initConversion
(
cs
);
exposeBindingResult
(
errors
);
this
.
tag
.
setPath
(
"realCountry"
);
this
.
tag
.
setItems
(
"${countries}"
);
this
.
tag
.
setItemValue
(
"isoCode"
);
int
result
=
this
.
tag
.
doStartTag
();
assertEquals
(
Tag
.
SKIP_BODY
,
result
);
String
output
=
getOutput
();
output
=
"<doc>"
+
output
+
"</doc>"
;
SAXReader
reader
=
new
SAXReader
();
Document
document
=
reader
.
read
(
new
StringReader
(
output
));
Element
rootElement
=
document
.
getRootElement
();
assertEquals
(
1
,
rootElement
.
elements
().
size
());
Element
selectElement
=
rootElement
.
element
(
"select"
);
assertEquals
(
"select"
,
selectElement
.
getName
());
assertEquals
(
"realCountry"
,
selectElement
.
attribute
(
"name"
).
getValue
());
List
children
=
selectElement
.
elements
();
assertEquals
(
"Incorrect number of children"
,
4
,
children
.
size
());
Element
e
=
(
Element
)
selectElement
.
selectSingleNode
(
"option[@value = 'UK']"
);
assertEquals
(
"UK node not selected"
,
"selected"
,
e
.
attribute
(
"selected"
).
getValue
());
assertEquals
(
"United Kingdom"
,
e
.
getText
());
}
public
void
testWithMultiListAndElementConverter
()
throws
Exception
{
List
list
=
new
ArrayList
();
list
.
add
(
Country
.
COUNTRY_UK
);
list
.
add
(
Country
.
COUNTRY_AT
);
this
.
bean
.
setSomeList
(
list
);
BeanPropertyBindingResult
errors
=
new
BeanPropertyBindingResult
(
this
.
bean
,
COMMAND_NAME
);
FormattingConversionService
cs
=
new
FormattingConversionService
();
cs
.
addFormatterForFieldType
(
Country
.
class
,
new
Formatter
<
Country
>()
{
public
String
print
(
Country
object
,
Locale
locale
)
{
return
object
.
getName
();
}
public
Country
parse
(
String
text
,
Locale
locale
)
throws
ParseException
{
return
new
Country
(
text
,
text
);
}
});
errors
.
initConversion
(
cs
);
exposeBindingResult
(
errors
);
this
.
tag
.
setPath
(
"someList"
);
this
.
tag
.
setItems
(
"${countries}"
);
this
.
tag
.
setItemValue
(
"isoCode"
);
int
result
=
this
.
tag
.
doStartTag
();
assertEquals
(
Tag
.
SKIP_BODY
,
result
);
String
output
=
getOutput
();
output
=
"<doc>"
+
output
+
"</doc>"
;
SAXReader
reader
=
new
SAXReader
();
Document
document
=
reader
.
read
(
new
StringReader
(
output
));
Element
rootElement
=
document
.
getRootElement
();
assertEquals
(
2
,
rootElement
.
elements
().
size
());
Element
selectElement
=
rootElement
.
element
(
"select"
);
assertEquals
(
"select"
,
selectElement
.
getName
());
assertEquals
(
"someList"
,
selectElement
.
attribute
(
"name"
).
getValue
());
List
children
=
selectElement
.
elements
();
assertEquals
(
"Incorrect number of children"
,
4
,
children
.
size
());
Element
e
=
(
Element
)
selectElement
.
selectSingleNode
(
"option[@value = 'UK']"
);
assertEquals
(
"UK node not selected"
,
"selected"
,
e
.
attribute
(
"selected"
).
getValue
());
assertEquals
(
"United Kingdom"
,
e
.
getText
());
e
=
(
Element
)
selectElement
.
selectSingleNode
(
"option[@value = 'AT']"
);
assertEquals
(
"AT node not selected"
,
"selected"
,
e
.
attribute
(
"selected"
).
getValue
());
assertEquals
(
"Austria"
,
e
.
getText
());
}
public
void
testWithMultiListAndCustomEditor
()
throws
Exception
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录