5

I'm trying to convert inputText to java.net.URL in JSF page:

...
<h:form>
  <h:inputText value="${myBean.url}" />
  <h:commandButton type="submit" value="go" />
</h:form>
...

My backed bean is:

import java.net.URL;
@ManagedBean public class MyBean {
  public URL url;
}

Should I implement the converter from scratch or there is some other way?

yegor256
  • 102,010
  • 123
  • 446
  • 597

2 Answers2

7

Yes, you need to implement a Converter. It's not that hard for this particular case:

@FacesConverter(forClass=URL.class)
public class URLConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return null;
        }

        try {
            return new URL(value);
        }
        catch (MalformedURLException e) {
            throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to URL", value)), e);
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return "";
        }

        return value.toString();
    }

}

Put it somewhere in your project. Thanks to the @FacesConverter it'll register itself automagically.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You're welcome. Please note the edit; I was forgotten the `FacesMessage`. It is the one which is to be displayed in a `h:message(s)`. – BalusC Aug 05 '10 at 14:39
  • You may want to handle null and empty string as well. return (value == null || value.isEmpty()) ? null : new URL(value); and return value == null ? "" : value.toString(); respectively – cyberoblivion Jan 31 '14 at 16:51
  • You are amazing! : ) – user1156544 Sep 06 '18 at 12:31
  • By the way, I had to add a `if (value == null || value.isEmpty()) return null` because otherwise it complained when a URL field was not filled – user1156544 Sep 06 '18 at 14:26
  • @user1156544: `getAsString()` must return an empty string when value is null. See javadoc. I've updated the answer. – BalusC Sep 06 '18 at 15:20
-2

you can implement a Custom Converter

http://www.roseindia.net/jsf/customconverter.shtml

Hope this helps

Community
  • 1
  • 1
ErVeY
  • 1,524
  • 4
  • 16
  • 26
  • 5
    That site is however the [worst Java/JSF source](http://balusc.blogspot.com/2008/06/what-is-it-with-roseindia.html) one can ever recommend to a beginner. Rather suggest another sources. – BalusC Aug 05 '10 at 14:20
  • Sorry about that O.o i didn't know this I'm a begginer too and i trying to implement jsf and icefaces and don't find goods code examples, i'll try hard for the next time ;D – ErVeY Aug 05 '10 at 14:37
  • No worries. You are yourself a beginner as well. That site is maintained by amateurs targeting on advertisement incomes only. Almost every code snippet shows/introduces bad practices in one or other way. That customconverter article for example should better have used a `SimpleDateFormat` to convert between Date and String. Also, the fact that JSF has builtin date/time converters is not mentioned at all. – BalusC Aug 05 '10 at 14:45
  • @BalusC you are so right. I tought JSF to me myself and unfortunately that site pops up quite often if you google for solutions. 90% of the time, i cursed that site for showing me real bullsh**, I can only advise everybody to avoid it. – f1sh Aug 05 '10 at 15:25