1

Class:

public class Booking {
    String foo1;
    String foo2;
}

Converter:

@FacesConverter(forClass = Booking.class)
public class BookingDisplayConverter implements Converter {

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)     {        
        Booking booking = (Booking) arg2;        
        return (booking.getFoo1() +" ("+ booking.getFoo2() +")");
    }

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

This works fine:

<h:outputText value="#{ViewReservation.booking}"/>

This does not (it uses toString()):

<h:commandButton value="Email" onclick="location.href = 'mailto:me@example.com?subject=Reservation: #{ViewReservation.booking})'; return false;"/>  

Is there a way to get the converter to work in the 2nd example?

EDIT: I'm aware that I could create a ApplicationBean scoped formatter that could do something like #{ApplicationBean.formatBooking(booking)} but I'm looking for something a little more elegant.

wsaxton
  • 1,030
  • 15
  • 34

1 Answers1

1

Is there a way to get the converter to work in the 2nd example?

Not via the standard JSF API.

JSF utility library OmniFaces has however an <o:param> for exactly this purpose, extending the standard <f:param> with support for a converter. However, this works only in cases where a <f:param> would be applicable, which isn't the case in an onclick attribute.

If it's affordable to replace the <h:commandButton> by a <h:outputLink>, then you could use it as follows:

<h:outputLink value="mailto:me@example.com">
    Email
    <o:param name="subject" value="#{viewReservation.booking}" />
</h:outputLink>

You can always throw in some CSS to make it to look like a button.

Noted should be that you should in first place really be using a <f:param> for this, because it also URL-encodes the values for usage in query string. E.g. the : should be encoded as %3A. Inlining an EL expression won't do that and may potentially malform the URL or even the JavaScript syntax if it contains quotes and such.

Also noted should be that the <h:commandButton> with a return false; is after all a fairly clumsy choice for the job. You'd better have grabbed <h:button> if you really needed a dead button.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yes, this could be an acceptable answer but...when I try it, it doesn't seem to encode spaces properly (it puts a + sign in place instead of %20). Should this be able to handle that? – wsaxton Jan 20 '15 at 21:35
  • Here is the URL your code produces: mailto:me@example.com?subject=1064+%28asdfqwer%29 – wsaxton Jan 20 '15 at 21:37
  • That's indeed what `URLEncoder` does. See also http://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20/ – BalusC Jan 20 '15 at 21:49
  • It actually looks like it is a JSF issue: https://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1019 . Fixed in JSF 2.2. Unfortunately, I'm stuck using JSF 2.1 for the near future. Will mark this as the answer though. – wsaxton Jan 21 '15 at 12:32
  • Why exactly is that a problem? In your specific case you're encoding an URL query string parameter, not the URL path. The `+` in a query string parameter is perfectly valid. See also the link in my previous comment. The `+` is only not valid in an URL path like `http://example.com/foo+bar`, which should indeed have been `http://example.com/foo%20bar`. – BalusC Jan 21 '15 at 12:35
  • The problem is, when you click a mailto link encoded using params, it converts spaces to + signs and mail clients do not re-interpret the + back to spaces. They do re-interpret %20. I mean...they changed the encoding behavior in 2.2 because of this, right? – wsaxton Jan 21 '15 at 13:06
  • The issue is about spaces in URL paths (where `+` is indeed invalid), not in URL query strings (where `+` is valid). Which mail client exactly has problems with it? – BalusC Jan 21 '15 at 13:08
  • Specifically, Thunderbird. Here is where I found the bug referenced: https://community.oracle.com/thread/2235427?tstart=0 . User had same issue I did. BTW, you may be technically correct throughout this entire conversation. I'm not an encoding "expert". I just want my mailto links to work with spaces :) – wsaxton Jan 21 '15 at 13:29