Conversion and uploaded files in Trinidad

Trinidad doesn’t (currently) support conversion for uploaded files. Something like this is currently totally ignored:

<tr:inputFile value="#{bean.fileModel}" ...>
  <f:converter converterId="myCustomFileTransformationConverter" />

That means your backing bean has to have a reference to the UploadedFile model interface. This is OK… but not always desired. Sometimes, when using a special model library, you may want to have the backing bean to have a reference to your model, instead of to the Trinidad interface.

With the upcoming Trinidad 1.2.10 (and 1.1.10) release this will be supported. However, there is a convention you have to follow… The getAsObject() from the Converter interface only accepts String for the value. I guess this was done b/c the standard components only treat Strings and no other (raw) objects like an uploaded file or similar. The main problem is that Java EE has no common API for uploads, but that is a different story…

Back to the convention. The Trinidad library invokes the getAsObject with a KEY, which the converter has to use to lookup the actual uploaded file instance in the request map. This is a work around to the know limitation, but works just great. The key is some “org.apache….” namespace + the filename, but that is really an implementation detail…

A custom getAsObject(…) could look like this:

  public Object getAsObject(FacesContext context, UIComponent component, String fileKey)
    UploadedFile file = (UploadedFile)context.getExternalContext().getRequestMap().get(fileKey);
    // do transformation
    return fileThatIsMyModelFileClass;

So, stay tuned for the upcoming release 😉


Posted in apache, java, jsf, myfaces, trinidad

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: