6

We are migrating JSF 1.1 (MyFaces) project to JSF 2. The idea is to migrate periodically by keeping both JSP and XHTML together for some time. We use many ajax4jsf-1.1.1 tags in JSP pages. We don't use RichFaces. After configuring the system to JSF 2 (with all config changes mentioned in tutorial by Balusc) When tried to access the JSP page with ajax4jsf.jar in classpath, we get an exception:

Caused by: java.lang.IllegalStateException: setViewHandler may not be executed after a lifecycle request has been completed
    at org.apache.myfaces.application.ApplicationImpl.setViewHandler(ApplicationImpl.java:853)
    at org.ajax4jsf.framework.ajax.InitPhaseListener.beforePhase(InitPhaseListener.java:92)
    at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersBefore(PhaseListenerManager.java:76)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:131)

It looks ajax4jsf.jar is not compatible with JSF 2. Looks some issue with LifeCycle configuration.

Is there any way we can make a4j work with JSF 2 JSPs? I know when we use XHTML we don't need all this.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Phani Kumar
  • 412
  • 1
  • 4
  • 16
  • Have you tried using RichFaces 4 instead of ajax4jsf? – Luiggi Mendoza Jul 09 '13 at 16:45
  • Yes, have tried quickly. But facing issues with navigation and action handling with JSPs. Didn't look deep into it as we had plans to use primefaces in future and don't want to adventure into Richfaces at the moment. – Phani Kumar Jul 10 '13 at 03:54
  • Just to let you know, RichFaces absorbed ajax4jsf project. I don't understand why not to *adventure* into RichFaces. – Luiggi Mendoza Jul 10 '13 at 03:55
  • Worried we may end up having both RF and PF. Any idea if RichFaces 4 a4j tags works with JSP? – Phani Kumar Jul 10 '13 at 09:22
  • There will be no problem having both RF and PF. I would recommend you to first **try it** and **make it work** to solve your current problem, then start focusing in the new migration problem (that won't be really hard in terms of time if that what worries you) instead of keep asking and doing nothing. – Luiggi Mendoza Jul 10 '13 at 14:19

1 Answers1

13

Get rid of Ajax4jsf 1.x altogether. It's indeed not compatible with JSF2. Instead, JSF2 offers a new main ajax tag <f:ajax> which covers all the core functionality as previously offered by Ajax4jsf 1.x.

If upgrading to RichFaces 4 is not an option (because, as you said yourself, you aren't using RichFaces components anywhere), then just remove Ajax4jsf 1.x and replace all <a4j:xxx> tags by standard JSF2 equivalents.

  • <a4j:ajaxListener>: use <f:ajax listener>.
  • <a4j:keepAlive>: just put managed bean in the view scope by @ViewScoped.
  • <a4j:log>: use jsf.ajax.addOnEvent() or jsf.ajax.addOnError() in JS context.
  • <a4j:commandLink>: just nest <f:ajax> inside <h:commandLink>.
  • <a4j:outputPanel>: use <h:panelGroup> and remember to include its ID in <f:ajax render> or PrimeFaces <p:outputPanel>.
  • <a4j:repeat>: just use standard <ui:repeat>.
  • <a4j:form>: just use <h:form>, it will autorecognize <f:ajax>.
  • <a4j:htmlCommandLink>: just nest <f:ajax> inside <h:commandLink>.
  • <a4j:jsFunction>: no replacement. Consider OmniFaces <o:commandScript> or PrimeFaces <p:remoteCommand>.
  • <a4j:region>: just use <f:ajax execute>, you can even wrap <f:ajax> around a group of components.
  • <a4j:loadBundle>: just use standard <f:loadBundle>.
  • <a4j:status>: use jsf.ajax.addOnEvent() or jsf.ajax.addOnError() in JS context.
  • <a4j:actionparam>: just use standard <f:param>.
  • <a4j:loadScript>: just use standard <h:outputScript>.
  • <a4j:mediaOutput>: no replacement. Consider PrimeFaces <p:media>.
  • <a4j:poll>: no replacement. Consider OmniFaces <o:commandScript> or PrimeFaces <p:poll>.
  • <a4j:commandButton>: just nest <f:ajax> inside <h:commandButton>.
  • <a4j:include>: just use standard <ui:include>.
  • <a4j:loadStyle>: just use standard <h:outputStylesheet>.
  • <a4j:support>: just use standard <f:ajax>.

You also need to rename/rewrite JSP files to Facelets files. In simple cases, this is usually just a matter of changing root declarations and file extensions. Facelets makes it easier to replace all duplicated code by a single template. The following answer applies:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • But If I understand it right, tags won't work in JSPs right? Is there any solution which works in JSPs (instead of re-implementing JSP into XHTML)? – Phani Kumar Jul 10 '13 at 03:50
  • JSP is deprecated in JSF2. So it's indeed expected that you migrate to Facelets as well. This may be helpful then: http://stackoverflow.com/questions/4441713/migrating-from-jsf-1-2-to-jsf-2-0/4532870#4532870 – BalusC Jul 10 '13 at 04:00
  • was looking for a way where we can keep existing JSPs with a4j tags as it is for some time while we migrate to XHTML. Looks there is no way. Not sure upgrading to RichFaces an option just to support a4j tags in JSP pages. – Phani Kumar Jul 10 '13 at 04:04
  • Thanks for your detailed answer on replacing a4j tags, it will definitely help us in future. – Phani Kumar Jul 10 '13 at 04:05