Ⅵ.spring的点点滴滴--自定义类型转换器
承接上文
自定义类型转换器
.net篇(环境为vs2012+Spring.Core.dll v1.31)
public class CustomeConverter : TypeConverter{
public override bool CanConvertFrom(
ITypeDescriptorContext context,
Type sourceType){
return true;
}
public override object ConvertFrom(
ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value){
return new TypeCunstomer() { Name = value.ToString() };
}
}
public class TypeObject{
public TypeCunstomer Name { get; set; }
}
public class TypeCunstomer{
public string Name { get; set; }
}<object
type="Spring.Objects.Factory.Config.CustomConverterConfigurer, Spring.Core">
<property name="CustomConverters">
<dictionary>
<entry key="SpringBase.TypeCunstomer">
<object type="SpringBase.CustomeConverter"/>
</entry>
</dictionary>
</property>
</object>
<object id="typeconvert" type="SpringBase.TypeObject,SpringBase">
<property name="Name" value="2"/>
</object>
- 继承中的CanConvertFrom方法是用于判断能否转换,ConvertFrom转换的具体方法
java篇(环境为Maven+Jdk1.7+IntelliJ IDEA 12.1.4)
package springdemo;
import java.beans.PropertyEditorSupport;
public class TypeObject {
private TypeCunstomer name;
public TypeCunstomer getName() {
return name;
}
public void setName(TypeCunstomer name) {
this.name = name;
}
}
class TypeCunstomer {
private String name;
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
}
class CustomeConverter extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
TypeCunstomer val=new TypeCunstomer();
val.setName(text+"Convent");
this.setValue(val);
}
}<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="springdemo.TypeCunstomer">
<bean class="springdemo.CustomeConverter" />
</entry>
</map>
</property>
</bean>
<bean id="typeObject" class="springdemo.TypeObject">
<property name="name" value="1"/>
</bean>
- 重写setAsText方法,其中setValue的参数为object
java和Csharp的共同点
- 配置文件中的dictionary|map标签设置的key为要转换的类型,值为转换的方法类
- 都是继承一个对象,然后重写方法来自定义类型的转换
Ⅵ.spring的点点滴滴--自定义类型转换器的更多相关文章
- Ⅶ.spring的点点滴滴--自定义对象行为
承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...
- 《SpringMVC从入门到放肆》十二、SpringMVC自定义类型转换器
之前的教程,我们都已经学会了如何使用Spring MVC来进行开发,掌握了基本的开发方法,返回不同类型的结果也有了一定的了解,包括返回ModelAndView.返回List.Map等等,这里就包含了传 ...
- 自定义类型转换器 及 使用 ServletAPI 对象作为方法参数
自定义类型转换器使用场景: jsp 代码: <!-- 特殊情况之:类型转换问题 --> <a href="account/deleteAccount?date=2018- ...
- SpringMVC自定义类型转换器
SpringMVC 自定义类型转换器 我们在使用SpringMVC时,常常需要把表单中的参数映射到我们对象的属性中,我们可以在默认的spring-servlet.xml加上如下的配置即可做到普通数据 ...
- springmvc——自定义类型转换器
一.什么是springmvc类型转换器? 在我们的ssm框架中,前端传递过来的参数都是字符串,在controller层接收参数的时候springmvc能够帮我们将大部分字符串类型的参数自动转换为我们指 ...
- springmvc中如何自定义类型转换器
package com.hope.utils;import org.springframework.core.convert.converter.Converter;import org.spring ...
- Struts2框架的自定义类型转换器
前言:对于java的基本数据类型及一些系统类(如Date类.集合类),Struts2提供了内置类型转换功能,但是也有一定的限制.所以就演示出自定义类型转换器 一.应用于局部类型转换器 eg.用户登录出 ...
- sruts2 自定义类型转换器
1.1.1 Struts2中自定义类型转换器:(了解) 类型转换的过程是双向的过程: JSP---->Action参数提交:String---Date. Action---->JSP ...
- Struts2之自定义类型转换器
Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用d ...
随机推荐
- Collection和Collections的区别
Collection 是集合类的上级接口,继承它的接口主要有set和list.Collections 是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索,排序,线程安全化等操作.
- c#中怎么删除一个非空目录
System.IO.Directory.Delete(@"C:\abc\",true)
- AOT
预 (AOT) 编译器 https://angular.cn/docs/ts/latest/cookbook/aot-compiler.html To run your app in AoT mode ...
- MMU、Icache、Dcache
http://blog.csdn.net/iodoo/article/details/8954014 i-cache(instruction cache)是指令高速缓冲存储器. Cache存储体:存放 ...
- PieTTY
PieTTY 用 pietty 連上主機時 鍵盤右方數字鍵 (keypad) 失效的問題 用 pietty 連上主機時 鍵盤右方數字鍵 (keypad) 失效的問題 應該滿多人用 pietty 連上程 ...
- 定时显示提示控件 TToolTip
转载过来的,文章出自: http://www.delphifans.com/infoview/Article_3640.html { 修改者:ghs 日期:20071218 功能:在 ...
- oracle database resident connection pooling(驻留连接池)
oracle在11g中引入了database resident connection pooling(DRCP).在此之前,我们可以使用dedicated 或者share 方式来链接数据库,dedic ...
- WCF - Creating WCF Service
http://www.tutorialspoint.com/wcf/wcf_creating_service.htm Creating a WCF service is a simple task u ...
- Webform和MVC,为什么MVC更好一些?
前言 如果你看了最近微软的议程,你会发现他们现在的焦点除了MVC,还是MVC.问题在于为什么微软如此热衷于丢弃传统的APS.NET Webform而转向ASP.NET MVC?本文就主要来讨论这个问题 ...
- DedeCMS 5.7 config.php 跨站脚本漏洞
漏洞版本: DedeCMS 5.7 漏洞描述: DeDeCMS v5.7 在/include/dialog/config.php文件中存在XSS漏洞,攻击者可以利用该漏洞盗取用户Cookie.挂马等. ...