`
sbfivwsll
  • 浏览: 63503 次
  • 性别: Icon_minigender_1
  • 来自: 四川
社区版块
存档分类
最新评论

tapestry 自定义参数转换器 ----Type Coercion

 
阅读更多

转自:http://tapestry.apache.org/typecoercer-service.html

 

Type Coercion

Type Coercion is the conversion of one type of object to a new object of a different type with similar content. Tapestry frequently must coerce objects from one type to another. A common example is the coercion of a string into an integer or a double.

Although type coercions happen more inside tapestry-core (including coercions ofcomponent parameters), they may also happen inside tapestry-ioc, such as when injecting a value, rather than a service, into a builder method.

Like everything else in Tapestry, type coercions are extensible. At the root is theTypeCoercer service. Its configuration consists of a number of CoercionTuples. Each tuple defines how to coerce from one type to another. The initial set of coercions is focused primarily on coercions between different numeric types:

Default Type Coercions

There are a few special coercions related to null there; Object -->List wraps a lone object as a singleton list, we then neednull -->List to ensure that null stays null (rather than a singleton list whose lone element is anull).

Tapestry can interpolate necessary coercions. For example, say it is necessary to coerce aStringBuffer to anInteger; the TypeCoercer service will chain together a series of coercions:

  • Object --> String
  • String --> Long
  • Long --> Integer

Coercing from null

Coercing from null is special; it is not a spanning search as with the other types. Either there is a specific coercion fromnull to the desired type, or no coercion takes places (and the coerced value isnull).

The only built-in null coercion is from null to boolean (which is always false).

List of Coercions

As of Tapestry versions 5.1 and 5.2, the following coercions are available:

Double --> Float
Float --> Double
Long --> Boolean
Long --> Byte
Long --> Double
Long --> Integer
Long --> Short
Number --> Long
Object --> Object[]
Object --> String
Object --> java.util.List
Object[] --> java.util.List
String --> Boolean
String --> Double
String --> Long
String --> java.io.File
String --> java.math.BigDecimal
String --> java.math.BigInteger
String --> java.text.DateFormat
String --> java.util.regex.Pattern
String --> org.apache.tapestry5.Renderable
String --> org.apache.tapestry5.SelectModel
String --> org.apache.tapestry5.corelib.ClientValidation
String --> org.apache.tapestry5.corelib.LoopFormState
String --> org.apache.tapestry5.corelib.SubmitMode
String --> org.apache.tapestry5.corelib.data.BlankOption
String --> org.apache.tapestry5.corelib.data.GridPagerPosition
String --> org.apache.tapestry5.corelib.data.InsertPosition
String --> org.apache.tapestry5.ioc.Resource
String --> org.apache.tapestry5.ioc.util.TimeInterval
boolean[] --> java.util.List
byte[] --> java.util.List
char[] --> java.util.List
double[] --> java.util.List
float[] --> java.util.List
int[] --> java.util.List
java.math.BigDecimal --> Double
java.util.Collection --> Boolean
java.util.Collection --> Object[]
java.util.Collection --> org.apache.tapestry5.grid.GridDataSource
java.util.Date --> java.util.Calendar
java.util.List --> org.apache.tapestry5.SelectModel
java.util.Map --> org.apache.tapestry5.SelectModel
long[] --> java.util.List
null --> Boolean
null --> org.apache.tapestry5.grid.GridDataSource
org.apache.tapestry5.ComponentResources --> org.apache.tapestry5.PropertyOverrides
org.apache.tapestry5.PrimaryKeyEncoder --> org.apache.tapestry5.ValueEncoder
org.apache.tapestry5.Renderable --> org.apache.tapestry5.Block
org.apache.tapestry5.Renderable --> org.apache.tapestry5.runtime.RenderCommand
org.apache.tapestry5.ioc.util.TimeInterval --> Long
org.apache.tapestry5.runtime.ComponentResourcesAware --> org.apache.tapestry5.ComponentResources
short[] --> java.util.List

Contributing New Coercions

TypeCoercer is extensible; you may add new coercions as desired. For example, let's say you have aMoney type that represents an amount of some currency, and you want to be able to convert fromBigDecimal toMoney. Further, let's assume that Money has a constructor that accepts aBigDecimal as its parameter. We'll use a little Tapestry IOC configuration jujitsu to inform the TypeCoercer about this coercion.

public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration)
{
    Coercion<BigDecimal, Money> coercion = new Coercion<BigDecimal, Money>()
    {
        public Money coerce(BigDecimal input)
        {
           return new Money(input);
        }
    };
 
    configuration.add(new CoercionTuple<BigDecimal, Money>(BigDecimal.class, Money.class, coercion));     
}

Further, since TypeCoercer knows how to convert Double to BigDecimal, or evenInteger (toLong to Double) to BigDecimal, all of those coercions would work as well.

When creating a coercion from null, use Void.class as the source type. For example, the built-in coercion fromnull toBoolean is implemented as:

    configuration.add(new CoercionTuple(void.class, Boolean.class,
        new Coercion<Void, Boolean>()
    {
        public Boolean coerce(Void input)
        {
            return false;
        }
    }));
分享到:
评论

相关推荐

    tapestry-bin-5.1.0.5

    【标题】"tapestry-bin-5.1.0.5" 指的是Apache Tapestry的一个特定版本的二进制发行包。Tapestry是一个开源的Java Web应用程序框架,它提供了一种基于组件的开发方式,用于构建动态、交互式的Web应用。 【描述】...

    tapestry-ioc-5.0.3-src

    在"tapestry-ioc-5.0.3-src"这个压缩包中,我们可能找到Tapestry IOC框架的源代码,这对于开发者深入理解其工作原理和定制功能非常有价值。 1. **依赖注入(DI)**:Tapestry IOC的核心概念就是DI,它允许对象在...

    tapestry5 自定义组件

    在 Tapestry 5 框架中,自定义组件是扩展其功能的关键方式,它允许开发者根据特定需求创建个性化和可重用的 UI 元素。Tapestry 5 是一个强大的 Java Web 应用程序开发框架,它强调组件化、模块化以及声明式编程模型...

    Tapestry简单入门.rar_java Tapestry_tapestry

    Tapestry是一款强大的Java Web应用程序框架,由Apache软件基金会维护,它提供了一种基于组件的模型来构建动态、数据驱动的Web应用。本教程将帮助初学者了解Tapestry的基本概念,带你逐步入门并掌握其核心组件的使用...

    tapestry5 build web application-alexander

    通过阅读此书,读者能够深入理解Tapestry 5如何将MVC(模型-视图-控制器)模式与组件编程思想结合,实现高度模块化和可重用的Web应用设计。 1. **组件模型**:Tapestry 5的核心是组件模型,它允许开发者创建可复用...

    tapestry-clojure-5.4-beta-3.zip

    android-priority-jobqueue.zip,一种专为android编写的作业队列,用于方便地调度在后台运行的作业(任务),提高用户体验和应用程序稳定性。一种专为android编写的作业队列,用于方便地调度在后台运行的作业(任务)...

    tapestry-project-4.1.5

    在"tapestry-project-4.1.5"这个压缩包中,我们有机会深入探索 Tapestry 4.1.5 版本的特性和应用。 首先,让我们关注"Tapestry-libraries"这个文件夹。它包含了Tapestry 框架运行所需的各种库文件,这些库文件是...

    tapestry教程资料文档合集

    Tapestry5最新中文教程.doc 作者 Renat Zubairov & Igor Drobiazko译者 沙晓兰 发布于 2008年7月2日 下午9时30分 社区 Java 主题 Web框架 ----------------------------------------- Tapestry5.1实例教程.pdf ...

    Tapestry-3-Getting Started.doc

    - **项目名称**:Tapestry-3-Getting Started - **项目目标**:开发一个基于Web的货币转换器应用。 - **技术栈**:Tapestry 3、Eclipse IDE、Apache Tomcat。 #### 二、开发环境搭建 ##### 1. Eclipse IDE安装 - **...

    apache-tapestry-5.3.8-bin.zip

    `apache-tapestry-5.3.8-bin.zip` 是Apache Tapestry 5.3.8版本的二进制发行包,包含了运行和开发Tapestry应用所需的各种库文件。 1. **Tapestry框架**:Tapestry是由Howard Lewis Ship创建的一个MVC(Model-View-...

    apache-tapestry-5.3.7-bin.zip

    `tapestry-jpa-5.3.7.jar`和`tapestry-hibernate-5.3.7.jar`则与持久层集成有关,它们支持JPA(Java Persistence API)和Hibernate ORM框架,使开发者能够轻松地在Tapestry应用中处理数据库操作。 总的来说,Apache...

    (Tapestry)An infrastructure for Fault-Tolerant Wide-area Location and Routing.ppt

    Tapestry是一个分布式系统基础设施,专门设计用于实现容错性的广域定位和路由。这个系统由Ben Y. Zhao、John Kubiatowicz和Anthony D. Joseph等人在加州大学伯克利分校的计算机科学部开发,旨在应对无处不在计算的...

    tapestry-with-all-funtionlity

    您可以看到以下日志1: : -&gt; org.apache.tapestry:quickstart(Tapestry 5 快速入门项目) 2: -&gt; org.apache.tapestry:tapestry-archetype (Tapestry 4.1.6 Archetype) 所以你想创建 Tapestry 5 quickStart 项目...

    优化大使tapestry-bin-5.0.18.zip

    这个压缩包包含了多个以"Tapestry"开头的子文件,如 "tapestry-core", "tapestry-hibernate", "tapestry-ioc", "tapestry-upload", 和 "tapestry-spring",这些都是Tapestry框架的不同模块或插件。这些文件的后缀是...

    tapestry-bin-5.0.14.zip

    标题中的“tapestry-bin-5.0.14.zip”指的是Tapestry的最新版本5.0.14的二进制发行包,它包含了开发人员构建基于Tapestry应用所需的所有组件和库。 Tapestry 5.0.14的发布旨在提供更加稳定和高效的开发环境,为...

    Tapestry5和jQuery集成tapestry5-jquery.zip

    Tapestry5和jQuery集成.使用jQuery以极少的兼容问题完全替换Prototype 和 Scriptaculous库 标签:tapestry5

    Tapestry4.1.2 入门-介绍

    此外,Tapestry还提供了诸如页面转换、错误处理、国际化支持、表单验证、AJAX集成等特性。它强调代码的组织和可维护性,使开发者能更专注于应用程序的业务逻辑,而不是底层的Web基础设施。总的来说,Tapestry4.1.2...

    Tapestry - Round Beach Towel-crx插件

    mandala-the-wall-tapestry-cave.com- 挂毯专家Mandala壁挂毯洞穴可帮助您将墙壁装饰变成真正的壁画,因此快来探索我们令人难以置信的圆形沙滩巾,壁挂毯和曼陀罗挂毯系列。 请从另一个角度考虑墙面设计,因为墙面...

    tapestry-spring-5.2.6.jar

    tapestry-spring-5.2.6.jar,java spring框架依赖,可以直接使用

Global site tag (gtag.js) - Google Analytics