1

I need build some jsf urls in a @WebListener.

I thought I could use a snippet of code like this one

    final FacesContext currentInstance = FacesContext.getCurrentInstance();
    final String actionURL = currentInstance.getApplication().getViewHandler()
            .getActionURL(currentInstance, viewId);

because the javadoc of the .getCurrentInstance() asserts it can be "[...] called during application initialization or shutdown" but it doesn't work because it returns null.

Do I miss somenthing? Any other way to build a url given the viewId?

Thanks

  • What is the real problem for which you think this might be a solution? – Kukeltje Nov 19 '15 at 10:18
  • This is what I need. At the start of the context I need to generate some urls for another webapp (completely unaware of jsf, but this shouldn't be relevenat) in a configuration agnostic manner. I know I could do something like`servletContext.getContextPath() + "/file.xthml"` but I _hope_ there is a better way. – Marco Mondini Nov 19 '15 at 10:32
  • Why a 'better' way? What is wrong with the way you do it? Maybe explain a little more – Kukeltje Nov 19 '15 at 10:44
  • I don't want to deal with facelets extensions and mappings and let jsf do the job. Quite straightforward – Marco Mondini Nov 19 '15 at 11:31
  • Describe how and where you want/need this viewId to be presented to the other webapp.... That is what your real 'problem' is, you seem to be 'stuck' in this solution.... FacesContext current instance is available after the FacesServlet is reached, not before (as can be read in other posts). So that is why it is null. But what for you is quite straight forward is for me totally blurry, but that might be cause by my limited vision/view/knowledge/... Maybe others can help out.... – Kukeltje Nov 19 '15 at 11:43

1 Answers1

1

The FacesContext.getCurrentInstance() method does return a valid facesContext instance between the linstener (com.sun.faces.config.ConfigureListener in my case) initialization and the first run of the FacesServlet, in which the facesContext set up by the listener is released. My problem was I was letting Wildfly add the listener and it was added just after mine. Forcing its loading in the web-fragment.xml/web.xml

<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>
    <listener-class>com.company.project.somepackages.Listener</listener-class>
</listener>

let my listener have the context initialized.

That code above however didn't work because the viewHandler when tries to resolve the contextPath uses the externalContext.getRequestContextPath() method, that obviously returns null, and not the externalContext.getApplicationContextPath() that could return the correct value.