Tuesday, August 10, 2010

Get Full Filepath from IceFaces Webapp

Just a quick little snippet - if you need the "real path" for a resource residing within your webapp directory tree, here's how I've done it in IceFaces:
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        ServletContext servletCtx = (ServletContext)ec.getContext();
        String filePath = servletCtx.getRealPath("/directory-under-webapp-root/somefile.pdf");
In principle the same idea applies regardless of IceFaces or any other web framework - you just need to get the ServletContext. That said, it appears that J2EE servers are not required to return anything beyond null from the getRealPath method.

My setting is with an exploded WAR - but if your webapp remains unexploded, here are some tips to get around that.

2 comments:

  1. IceFaces is not actually a JSF implementation. It's a JSF add-on (ajax component library and then some more). Apache MyFaces and Sun's RI are the implementations.

    I was consulting/contracting JSF full time for about two years, and saw relatively little evidence of IceFaces being used in the field. Thinking seemed to be: "JSF is complex enough as it is so we don't want to add any extra stability-risking complexity on top". This is not at all isolated to IceFaces, which may well be the best of the bunch, but all JSF Ajax stuff seems to be retrofits on top of a framework that simply was not designed for Ajax.

    As of JSF itself; I'v moved to GWT about a year ago (and would NEVER move back to JSF), but to tell the truth JSF served me relatively well. Some architectural and design deficiencies are balanced by the wide industry adaptation.

    ReplyDelete
  2. Your comments come at an interesting time for me - my initial enthusiasm for JSF has faded (considerably), and I'll soon be moving on to GWT. Could you elaborate on the GWT deficiencies you mention?

    ReplyDelete