3

I have found a Converter online and changed it to my needs as far as I could. The problem is though that I need to add a flag (i.e. a string) that must be checked and than the converter must apply a certain pattern to a 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 the problem is that for example I have 2 conversion types:

  • hju
  • zurt

Let's say that hju have the output format XXXX-XXXX-XXXXX and zurt has the output format XX-XX-XX-XX-XX-XX-X.

Now I would like to call the converter like for example:

 <f:converter converterId="convtest.UrlConverter" type="hju" />

Or something like that and get it to use the correct pattern.

Any ideas on how to do this?

2 Answers 2

9

You need to register the custom converter as a new tag in *.taglib.xml wherein you can specify as many attributes as you want which will then be mapped as bean properties of the converter instance.

So, given a new property type:

@FacesConverter("convtest.UrlConverter")
public class UrlConverter implements Converter {

    private String type; // +getter+setter

}

And this /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 is registered as 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 this 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 you happen to use JSF utility library OmniFaces, then you can also save the above XML boilerplate by using <o:converter> as 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 will transparently set those attributes as converter properties.

2
  • Great to know this omnifaces feature.
    – djmj
    Feb 19, 2017 at 18:01
  • Great to know you can do this using OmniFaces, but in the end I opted to create the taglib xml to keep my IDE happy (it complained about missing / invalid attributes when I was using o:converter). Jul 18, 2017 at 8:37
1

You should use <f:attribute/>

<h:outputText value="#{userData.data}" >
  <f:converter converterId="convtest.UrlConverter" />
  <f:attribute name="myCoolFlag" value="hju"/>
</h:outputText>

In the converter you can call component.getAttributes().get("myCoolFlag");

6
  • But how can I can the converter with hju or zurt. I need to set the flag when I call the converter. The next step will be to call the converter from another program so I can't enter any text at that point. That is why I got the <f:converter converterId="convtest.UrlConverter('hju')" /> idea. I call the converter with the textstring and the flag. Jul 14, 2015 at 12:50
  • Why don't you use the same converter for both and calculate the attribute?
    – VWeber
    Jul 14, 2015 at 13:02
  • That is what I am trying to do. I just don't want to assign the attribute to the component but to have it in the calling of the converter. Jul 14, 2015 at 13:09
  • I doubt I'm not getting your problem right. <f:attribute name="myCoolFlag" value="someBean.methodToCalculateYourPattern()"/> does not do the job?
    – VWeber
    Jul 14, 2015 at 13:11
  • In the end I don't have something like inputText or outputText but the converter will be used to populate forms. The data needs to be patterned differently and the cell is not databound so one time zurt can be in the cell the other time hju. There for I need to call the converter with a parameter, as much as my understanding is. If you can tell me it works otherwise as well I am glad to hear it :-) Could you give me a snipplet on how it would work? Jul 14, 2015 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.