- 浏览: 73185 次
- 来自: ...
文章分类
最新评论
-
qingquan_11205:
[color=red][/color] [size=small ...
shell -
aimbin:
数据库并发操作存在的异常情况 觉得讲的不怎么清楚,推算着想不过 ...
【转】事务隔离级别 -
fandayrockworld:
哇,博主总结的太好了,厉害。
最近遇到一个Image加载图片过 ...
Flex内存泄露问题
4.1) Introduction
In most of the situations, the Converter Implementations that are bundled with JSF implementation is often enough. However, for application specific purposes, there may be a need to customize and convert the user entered string into some other data-type. This can be achieved with ease by using JSF Pluggable Converter architecture.
4.2) Phone Number
Let us assume that our Web Application prompts the user to enter phone number which is of the format 'CountryCode-AreaCode-Number'. Note that whatever user enters is just a string and we want this string to be converted into some kind of format so that it can be stored in the model.
PhoneNumber.java
The above model class for Phone Number encapsulates 'countryCode', 'areaCode' and 'number' properties. The rest of the code merely contains setters and getters for setting and getting the appropriate values.
4.3) Phone Number Converter
All the Converter classes in JSF must implement the Converter interface and they should override the getAsObject() and getAsString() methods. The method getAsObject() will be called as soon as the user enters the data and conforms it by clicking the submit button. This triggers the Apply Request Values Phase and this method will be called with the user-entered value as one of its arguments. Here we can parse and format the data entered by the user and store it in some model object and return it so that it will be set to the appropriate UI Component.
The getAsString() method will be called during Render Response Phase. Now, it's time to display the data in the view from the model object. And usually this method will contain logic for extracting the data from the model, format it and return it to the view.
PhoneNumberConverter.java
4.4) Registering the Custom Converter
To make the Custom Converter visible to JSF Application, we have to register the Converter class by making an entry in the Faces Configuration file. The following is the xml code snippet for the same,
We have given an identifier 'PhoneNumberConverter' for the PhoneNumberConverter class and this identifier should be referenced elsewhere in the Application.
4.5) Using the Custom Converter
To set this Phone Number Converter to a component, make use of the 'converter' tag as the following listing does,
In most of the situations, the Converter Implementations that are bundled with JSF implementation is often enough. However, for application specific purposes, there may be a need to customize and convert the user entered string into some other data-type. This can be achieved with ease by using JSF Pluggable Converter architecture.
4.2) Phone Number
Let us assume that our Web Application prompts the user to enter phone number which is of the format 'CountryCode-AreaCode-Number'. Note that whatever user enters is just a string and we want this string to be converted into some kind of format so that it can be stored in the model.
PhoneNumber.java
package net.javabeat.articles.jsf.converters; import java.io.Serializable; public class PhoneNumber implements Serializable { private int countryCode; private int areaCode; private long number; public PhoneNumber() { } public PhoneNumber(int countryCode, int areaCode, long number) { this.setCountryCode(countryCode); this.setAreaCode(areaCode); this.setNumber(number); } public int getCountryCode() { return countryCode; } public void setCountryCode(int countryCode) { this.countryCode = countryCode; } public int getAreaCode() { return areaCode; } public void setAreaCode(int areaCode) { this.areaCode = areaCode; } public long getNumber() { return number; } public void setNumber(long number) { this.number = number; } }
The above model class for Phone Number encapsulates 'countryCode', 'areaCode' and 'number' properties. The rest of the code merely contains setters and getters for setting and getting the appropriate values.
4.3) Phone Number Converter
All the Converter classes in JSF must implement the Converter interface and they should override the getAsObject() and getAsString() methods. The method getAsObject() will be called as soon as the user enters the data and conforms it by clicking the submit button. This triggers the Apply Request Values Phase and this method will be called with the user-entered value as one of its arguments. Here we can parse and format the data entered by the user and store it in some model object and return it so that it will be set to the appropriate UI Component.
The getAsString() method will be called during Render Response Phase. Now, it's time to display the data in the view from the model object. And usually this method will contain logic for extracting the data from the model, format it and return it to the view.
PhoneNumberConverter.java
package net.javabeat.articles.jsf.converters; import java.util.StringTokenizer; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; public class PhoneNumberConverter implements Converter { public PhoneNumberConverter() { } public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null || (value.trim().length() == 0)) { return value; } PhoneNumber phoneNumber = new PhoneNumber(); boolean conversionError = false; int hyphenCount = 0; StringTokenizer hyphenTokenizer = new StringTokenizer(value, "-"); while (hyphenTokenizer.hasMoreTokens()) { String token = hyphenTokenizer.nextToken(); try { if (hyphenCount == 0) { phoneNumber.setCountryCode(Integer.parseInt(token)); } if (hyphenCount == 1) { phoneNumber.setAreaCode(Integer.parseInt(token)); } if (hyphenCount == 2) { phoneNumber.setNumber(Long.parseLong(token)); } hyphenCount ++; } catch (Exception exception) { conversionError = true; } } if (conversionError || (hyphenCount != 3)) { throw new ConverterException(); } return phoneNumber; } public String getAsString(FacesContext context, UIComponent component, Object value) { PhoneNumber phoneNumber = null; if (value instanceof PhoneNumber) { phoneNumber = (PhoneNumber)value; StringBuilder phoneNumberAsString = new StringBuilder(); phoneNumberAsString.append(phoneNumber.getCountryCode() + "-"); phoneNumberAsString.append(phoneNumber.getAreaCode() + "-"); phoneNumberAsString.append(phoneNumber.getNumber()); return phoneNumberAsString.toString(); } return ""; } }
4.4) Registering the Custom Converter
To make the Custom Converter visible to JSF Application, we have to register the Converter class by making an entry in the Faces Configuration file. The following is the xml code snippet for the same,
<converter> <description>A Converter for phone number</description> <converter-id>PhoneNumberConverter</converter-id> <converter-class> net.javabeat.articles.jsf.converters.PhoneNumberConverter </converter-class> </converter>
We have given an identifier 'PhoneNumberConverter' for the PhoneNumberConverter class and this identifier should be referenced elsewhere in the Application.
4.5) Using the Custom Converter
To set this Phone Number Converter to a component, make use of the 'converter' tag as the following listing does,
<h:inputText id="phoneNumberInput" value="#{phoneNumberBean.phoneNumber}" required="true"> <f:converter converterId="PhoneNumberConverter"></f:converter> </h:inputText>
发表评论
-
记录nginx+tomcat+memcached+msm负载均衡,session共享
2013-03-06 18:33 3970环境centos5.5,jdk 1.7.0 ... -
[转]Red5源代码分析 – 关键类及其初始化过程
2011-09-28 16:15 1541Red5如何响应rmpt的请求,中间涉及哪些关键类? 响应请求 ... -
[转]Maven常用命令
2011-09-23 16:11 829Maven常用命令: 1. 创建Maven的普通java项目: ... -
[转]java内存回收机制
2010-08-09 15:07 1131引言 Java的堆是一个运 ... -
【转】两阶段提交
2010-04-08 18:14 967两阶段提交协议 实现分布式事务的关键就是两阶段提交协议。在此 ... -
【转】事务隔离级别
2010-04-08 16:35 855事务的四个属性:原子性(atomicity)、一致性(cons ... -
rsync同步镜像配置
2009-03-12 12:25 2047转自:http://www.liyue.org/tec ... -
java.nio.Buffer缓冲区基础[转]
2009-01-06 14:22 7763缓冲区基础 抽象类Buffer是java.nio包支持缓冲区 ... -
Java:重写equals()和hashCode()
2008-11-28 17:26 10151.何时需要重写equals() 当一个类有自己特有的“逻辑相 ... -
再论Java Swing线程
2008-11-26 16:58 1849不正确的Swing线程是运 ... -
[转]细说Java GUI:AWT,SWT,Swing
2008-11-25 16:00 1497Overview概述 Java GU ... -
[转]Seam增强了JSF
2008-09-25 15:09 1512Seam采用的JSF作为表现层技术,但是标准的jsf有很多的不 ... -
[转][opencms]在OpenCms中利用Java代码删除、创建、更新用户!
2008-01-11 17:51 1535<% org.opencms.file.Cms ... -
[转][opencms]移动文件的简单示例
2008-01-11 15:42 1080下面的简单示例实现了将目录/home01/中的多于8个的文件移 ... -
开始接触SEAM
2007-07-26 18:28 1062开始接触SEAM。记下一些东西: Each Seam com ...
相关推荐
Google Blog Converters(博客数据转换器)1.0 Google Blog Converters(博客数据转换器)1.0
Switched-capacitor (SC) DC-DC power converters are a subset of DC-DC power con- verters that use a network of switches and capacitors to e ciently convert one voltage to another. Unlike traditional ...
As a consequence, the grid converters should be able to exhibit advanced functions like: dynamic control of active and reactive current injection during faults, and grid services support.This book ...
Power electronics-converters applications and design
数据转换方面的资料CMOS Data Converters for Communication
POWER ELECTRONICS CONVERTERS,APPLICATIONS,AND DESIGN,电力电子经典书籍,花了很长时间才找到的。
Digital Control of High-Frequency Switched-Mode Power Converters
JESD204C-01 2022 SERIAL INTERFACE FOR DATA CONVERTERS.pdf
Understanding Delta-Sigma Data Converters
Book_Springer-2011-Robust Sigma Delta Converters
Fundamentals of Power Electronics 第一部分 PartⅠ Converters in Equilibrium PPT
power electronics, classical notebook.
一本不错关于Delta-Sigma转换器的书 Understanding Delta Sigma Data Converters
2015 Power Electronics Converters and Regulators, 国外的教材
small signal analysi sof parallel power converters
microsoft_office _FileConverters _3_PConline
一本经典的sigma delta调制器的外文书籍,对于理解其工作原理和设计有很大帮助
Advanced_Data_Converters--[248].pdf
CMOS Telecom Data Converters--2003 [609].pdf