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

tapestry 中使用枚举类型作为组件参数的方法

 
阅读更多

转自:http://tapestry.apache.org/enum-parameter-recipe.html

 

It's not uncommon to create a component that has a bit of complex behavior that you want to be able to easily control, and an enumerated type (a Java enum) seems like the right approach.

 

Our example comes from Tapestry's Select component, which has a blankOption parameter that an enum type.

 

Let's start with the enum type itself:

 


BlankOption.java

public enum BlankOption
{
    /**
     * Always include the blank option, even if the underlying property is required.
     */
    ALWAYS,

    /**
     * Never include the blank option, even if the underlying property is optional.
     */
    NEVER,

    /**
     * The default: include the blank option if the underlying property is optional.
     */
    AUTO;
}
 

 

Next, we define the parameter:

 


Select.java (partial)

/**

* Controls whether an additional blank option is provided. The blank option precedes all other options and is never

* selected. The value for the blank option is always the empty string, the label may be the blank string; the

* label is from the blankLabel parameter (and is often also the empty string).

*/

@Parameter(value = "auto", defaultPrefix = BindingConstants.LITERAL)

private BlankOption blankOption;


Note the use of literal as the default prefix; this allows us to use the name of the option in our template, e.g. <t:select blankoption="never" .../>. Without the default prefix setting, "never" would be interpreted as a property expression (and you'd see an error when you loaded the page).

 

The final piece of the puzzle is to inform Tapestry how to convert from a string, such as "never", to a BlankOption value.

 


TapestryModule.java (partial)

  public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration)
    {
       . . .
       
       add(configuration, BlankOption.class);

       . . .
    }

    private static <T extends Enum> void add(Configuration<CoercionTuple> configuration, Class<T> enumType)
    {
        configuration.add(CoercionTuple.create(String.class, enumType, StringToEnumCoercion.create(enumType)));
    }
 

 

The TypeCoercer service is ultimately responsible for converting the string to a BlankOption, but we have to tell it how, by contributing an appropriate CoercionTuple. The CoercionTuple identifies the source and target types (String and BlankOption), and an object to perform the coercion (an instance of StringToEnumCoercion, via the create() static method).

分享到:
评论

相关推荐

    tapestry4.02中封装ext的GridPanel组件

    tapestry4.02中封装ext的GridPanel组件

    tapestry4.02中封装ext的TabPanel组件

    tapestry4.02中封装ext的TabPanel组件

    tapestry hibernate Spring应用及组件的使用

    tapestry hibernate Spring应用及组件的使用的一个简单完整例子,包含form组件、table组件的一个增删改查。其中数据库创建用户和表在docs里,数据库使用oracle

    tapestry5 自定义组件

    自定义的邮件组件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    tapestry组件

    tapestry部分组件绑定参数的列表!

    Tapestry的组件及功能

    介绍Tapestry组件的使用和功能。内容还行,使用初学者入门。

    tapestry官方中文文档

    Tapestry 4 官方文档中文版本,现在中文资料比较少,和大家共享一下

    tapestry页面编辑组件

    tapestry页面编辑组件,可以实现文本框,单选框,多选框和下拉框等的自动生成,并返回改变后的数据。

    tapestry5中文文档

    tapestry5组件说明使用及登陆修改等简单实例

    tapestry官方中文文档及中文字典

    Tapestry 4 官方文档中文版本,包括Tapestry4 Quick Start(2)和Tapestry4 Users Guide(2)两个文档 还有tapestry中文字典等

    tapestry 5.1.0.5 官方组件文档 天涯浪子

    来自:http://tapestry.apache.org/tapestry5.1/tapestry-core/ref

    Tapestry5最新中文入门实例教程

    本文介绍Tapestry框架版本5。本文利用Tapestry 5开发一个简单的具有创建/读/更新/删除功能的应用,在创建这个应用的过程中,本文体会到Tapestry...还将了解如何应用Tapestry中内嵌的Ajax功能来创建支持Ajax的组件。

    Tapestry5 使用教程

    Apache Tapestry是一个使用Java语言创建web应用的面向组件的开发框架。Tapestry应用建立在根据组件构建的页面的基础上。这个框架能够提供输入验证(input validation)、本地化/国际化(localization/...

    Maven + Tapestry5.3.8 + Spring4.0.5 + Oracle10g

    2)Tapestry 各种核心组件的使用 3)自带验证,自定义验证,验证码的生成 4)Tree组件的使用,Grid的各种使用(修改样式,排序,隔行换色等等),Loop组件的使用 5)集合Spring4.0.5实现的增删改查 等等 如果你是新...

    tapestry教程资料文档合集

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

    tapestry5以上的帮助事例,帮助文档与spring衔接文档

    引用Tapestry网站上的一句话:"Tapestry用对象(objects),方法(methods),属性(PRoperties)替代以往的URLs和查询参数, 重新诠释Web应用开发.Tapestry 3.0即将发布,它有大量的改进和新的特性.你值得拥有!!!!

    Tapestry5开发文档手册.doc

    tapestry5组件说明使用等简单实例。Apache Tapestry是一个使用Java语言创建web应用的面向组件的开发框架。Tapestry应用建立在根据组件构建的页面的基础上。这个框架能够提供输入验证(input validation)、本地化/...

    Tapestry5最新中文教程

    Drobiazko和R. Zubairov合作撰写了一篇文章,详细介绍Apache Tapestry 版本5——一个...文章向读者展示了创建组件方法,并谈到了Tapestry中的IoC以及Ajax的相关特性 译者 沙晓兰 发布于 2008年7月2日 下午9时30分

    深入浅出Tapestry

    使读者在学习如何使用Tapestry框架技术的同时,还能够获得在J2EE Web应用程序中应用Tapestry框架的先进经验。本书详细介绍了Hivemind框架的原理与应用,使读者不但可以通过Hivemind来重构Tapes 资源太大,传百度网盘...

    tapestry技术

    Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。一个组件就是任意一个带有jwcid属性的html标记。其中jwc的意思是Java Web Component。Tapestry使得java代码与html...

Global site tag (gtag.js) - Google Analytics