Tutorials to .com

Tutorials to .com » Jsp » Tapestry-hivemind » Tapestry data format conversion and storage of the study between the 2

Tapestry data format conversion and storage of the study between the 2

Print View , by: iSee ,Total views: 8 ,Word Count: 1114 ,Date: Thu, 16 Apr 2009 Time: 2:19 AM

An article on the tapestry of data format conversion and storage are summarized in this paper will give specific examples.

A converter (translator)
The realization of a digital converter NumberTranslator, it inherited from org.apache.tapestry.form.translator.NumberTranslator, the realization of the function is the string with the figure of each conversion, you can set if the number is 0 or NaN displayed to the user when it could be Other strings can also be set up, such as number of decimal places.
As the javascript to the client on the number of types of calibration (using the function isNaN), so the number of back-end model (such as the form of this complex ###,###%) the time being not to support it. The form of support such as: 00123.450
public class NumberTranslator extends
org.apache.tapestry.form.translator.NumberTranslator (

public NumberTranslator () (
)

protected String formatObject (IFormComponent field, Locale locale, Object object) (
Number number = (Number) object;
if (number.doubleValue () == 0) (
if (! HiveMind.isBlank (this._zero))
return this._zero;
)

if (number.equals (new Double (Double.NaN))) (
if (! HiveMind.isBlank (this._nan))
return this._nan;
)

return super.formatObject (field, locale, object);
)

public NumberTranslator (String initializer) (
PropertyUtils.configureProperties (this, initializer);
)

protected Object parseText (IFormComponent field,
ValidationMessages messages, String text) throws ValidatorException (
if (text.equals (this._zero)) (
return new Long (0);
)

if (text.equals (this._nan)) (
return new Double (Double.NaN);
)
return super.parseText (field, messages, text);
)

protected String defaultScript () (
return null;
)

public void renderContribution (IMarkupWriter writer, IRequestCycle cycle,
FormComponentContributorContext context, IFormComponent field) (
context. includeClasspathScript ( "/ mycom / web / tapestry / translator / NumberValidator.js");

String message = TapestryUtils.enquote (buildMessage (context, field, getMessageKey ()));

if (this.getSubmit () == null) (context.addSubmitHandler (Script.getFormNumberScript (field,
this._zero, this._nan, message));
) Else (
TapestryUtils.getPageRenderSupport (cycle, field)
. addInitializationScript (Script.getSubmitNumberScript (this.getSubmit (), field, this._zero, this._nan, TapestryUtils.enquote (message)));
)
)

private String _zero = "0";
private String _nan = "NaN";
private String _submit = null;
public void setZero (String zero) (
this._zero = zero;
)
public String getZero () (
return this._zero;
)
public void setNan (String nan) (
this._nan = nan;
)
public String getNan () (
return this._nan;
)
public String getSubmit () (
return this._submit;
)
public void setSubmit (String submit) (
this._submit = submit; [AutoPage]
)
)

Usage:
Setting input field (such as: TextField) attributes of the translator:
<binding name="translator" value="translator:number2,zero=--,submit=login,pattern=###.00"/>
Note: with the validator similar converter also needs to support the form and submit button class class two-level conversion.

Browser formatted (format)
1, the digital format devices
Add a binding numberformat, please refer to another article tapestry of research and application binding.
public class NumberFormatBinding extends AbstractBinding (

private final IComponent _component;

private final String _format;

protected NumberFormatBinding (String description,
ValueConverter valueConverter, Location location,
IComponent component, String format) (
super (description, valueConverter, location);
this._component = component;
this._format = format;
)

public Object getObject () (
if (_format == null)
throw new BindingException (BindingMessages.missingAsset (_component,
_format), _component, getLocation (), this, null);
return new DecimalFormat (_format);
)
)

public class NumberFormatBindingFactory extends AbstractBindingFactory (

public IBinding createBinding (IComponent root, String bindingDescription,
String expression, Location location) (
return new NumberFormatBinding (bindingDescription, getValueConverter (),
location, root, expression);
)

)

Usage:
Insert component format set attributes:
<binding name="format" value="numberformat:###,###"/>

2, the date formatter
public class DateFormatBinding extends AbstractBinding (

private final IComponent _component;

private final String _format;

protected DateFormatBinding (String description,
ValueConverter valueConverter, Location location,
IComponent component, String format) (
super (description, valueConverter, location);
this._component = component;
this._format = format;
)

public Object getObject () (
if (_format == null)
throw new BindingException (BindingMessages.missingAsset (_component, _format), _component, getLocation (), this, null);
return new SimpleDateFormat (_format);
)
)

public class DateFormatBindingFactory extends AbstractBindingFactory (

public IBinding createBinding (IComponent root, String bindingDescription, String expression, Location location) (
return new DateFormatBinding (bindingDescription, getValueConverter (), location, root, expression);
)
)
Usage:
Insert component format set attributes:
<binding name="format" value="dateformat:yyyy-MM-dd"/>

3, the registration of these two bindings.
<! - Add a binding named "numberformat" to format a number
usage: <binding name="format" value="numberformat:###,###.00"/>
->
<service-point id="NumberFormatBindingFactory" interface="org.apache.tapestry.binding.BindingFactory">
<invoke-factory>
<construct class="mycom.web.tapestry.binding.NumberFormatBindingFactory">
<set-object property="valueConverter" value="infrastructure:valueConverter"/>
</ construct>
</ invoke-factory>
</ service-point>
<contribution configuration-id="tapestry.bindings.BindingFactories">
<binding prefix="numberformat" service-id="NumberFormatBindingFactory"/>
</ contribution>

<! - Add a binding named "dateformat" to format a date
usage: <binding name="format" value="dateformat:yyyy-MM-dd"/> [AutoPage]
->
<service-point id="DateFormatBindingFactory" interface="org.apache.tapestry.binding.BindingFactory">
<invoke-factory>
<construct class="mycom.web.tapestry.binding.DateFormatBindingFactory">
<set-object property="valueConverter" value="infrastructure:valueConverter"/>
</ construct>
</ invoke-factory>
</ service-point>
<contribution configuration-id="tapestry.bindings.BindingFactories">
<binding prefix="dateformat" service-id="DateFormatBindingFactory"/>
</ contribution>


Somewhat arbitrary code, please forgive me.


tapestry & hivemind Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.