jsf - Creating custom tag for Converter with attributes -


i have found converter online , changed needs far could. problem though need add flag (i.e. string) must checked , converter must apply pattern string.

custom converter:

@facesconverter("convtest.urlconverter") public class urlconverter implements converter {     @override    public object getasobject(facescontext facescontext, uicomponent component, string value) {        stringbuilder url = new stringbuilder();        if(value!=null){           if(value.length()==13){               string tempstring;               tempstring=value.tostring();               string finalstring= tempstring.substring(0, 4) + "-" + tempstring.substring(4, 8) + "-" + tempstring.substring(8, 13);               url.append(finalstring);            }else{               url.append(value);           }       }else      url.append("");        try {          new uri(url.tostring());                  } catch (urisyntaxexception e) {          return null;       }        urldata urldata = new urldata(url.tostring());        return urldata;    }     @override    public string getasstring(facescontext facescontext,       uicomponent component, object value) {          try {             return value.tostring();         } catch (exception e) {             return null;         }    }   } 

xhtml:

 <h:inputtext value="#{userdata.data}">     <f:converter converterid="convtest.urlconverter" />  </h:inputtext>       

now problem example have 2 conversion types:

  • hju
  • zurt

let's hju have output format xxxx-xxxx-xxxxx , zurt has output format xx-xx-xx-xx-xx-xx-x.

now call converter example:

 <f:converter converterid="convtest.urlconverter" type="hju" /> 

or , use correct pattern.

any ideas on how this?

you need register custom converter new tag in *.taglib.xml wherein can specify many attributes want mapped bean properties of converter instance.

so, given new property type:

@facesconverter("convtest.urlconverter") public class urlconverter implements converter {      private string type; // +getter+setter  } 

and /web-inf/my.taglib.xml (assuming jsf 2.x on facelets):

<?xml version="1.0" encoding="utf-8"?> <facelet-taglib     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"     version="2.0" >     <namespace>http://example.com/ui</namespace>      <tag>         <tag-name>urlconverter</tag-name>         <converter>             <converter-id>convtest.urlconverter</converter-id>         </converter>         <attribute>             <name>type</name>             <type>java.lang.string</type>         </attribute>     </tag> </facelet-taglib> 

which registered below in /web-inf/web.xml:

<context-param>     <param-name>javax.faces.facelets_libraries</param-name>     <param-value>/web-inf/my.taglib.xml</param-value> </context-param> 

then usage should do:

<html ... xmlns:my="http://example.com/ui"> ... <h:inputtext ...>     <my:urlconverter type="hju" /> </h:inputtext>       
<html ... xmlns:my="http://example.com/ui"> ... <h:inputtext ...>     <my:urlconverter type="zurt" /> </h:inputtext>       

alternatively, if happen use jsf utility library omnifaces, can save above xml boilerplate using <o:converter> below:

<html ... xmlns:o="http://omnifaces.org/ui"> ... <h:inputtext ...>     <o:converter converterid="convtest.urlconverter" type="hju" /> </h:inputtext>       
<html ... xmlns:o="http://omnifaces.org/ui"> ... <h:inputtext ...>     <o:converter converterid="convtest.urlconverter" type="zurt" /> </h:inputtext>       

it transparently set attributes converter properties.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -