Migrating from JSF 1.2 to JSF 2.0
Asked Answered
D

5

141

I am working with a rather large app written in JSF 1.2. JSF 1.2 is around 6 years old now. I need to upgrade to JSF 2.0. How painful will this be? I noticed that some attributes in custom tags have been changed etc.

Dynasty answered 14/12, 2010 at 16:44 Comment(0)
A
258

Painfulness

Painfulness of upgrading JSF 1.2 to 2.0 depends on the view technology which you are currently using and which you want to use.

  • JSP 2.x to JSP 2.x = Almost no effort.
  • Facelets 1.x to Facelets 2.0 = Little effort.
  • JSP 2.x to Facelets 2.0 = Lot of effort. Double this if you also have custom components.

Basic changes

Regardless of the view technology switch, at least the following steps should be done:

  • Remove JSF 1.2 JAR's from /WEB-INF/lib (if any).

  • Drop JSF 2.0 JAR's in /WEB-INF/lib (if JSF 1.2 was servletcontainer-supplied, you might want to change the classloading policy to load webapp libraries first before servletcontainer libraries, see also JSF2 classloading issues in application servers).

  • Update root declaration of faces-config.xml to comply JSF 2.0 spec.

      <faces-config
          xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
          version="2.0">
    

    Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XML snippet.

  • Ensure that root declaration of web.xml already complies at least Servlet 2.5. JSF 2.0 won't work on 2.4 or lower (although it's hackable).

      <web-app 
          xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          id="YourWebappID"
          version="2.5">
    

    Note: when you're using Servlet 3.0 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XML snippet.


JSP 2.x to JSP 2.x

If you're using JSP 2.x and want to keep using it, then you basically don't need to change anything else.

Gradually upgrading

If you're already using a suffix url-pattern for the FacesServlet, like *.jsf, then it's good to know that the FacesServlet will first scan for *.xhtml file and if it is not present, then scan for *.jsp file. This provides you room to gradually convert from JSP to Facelets behind the scenes without changing the URL's.

But if you're using a prefix url-pattern, like /faces/* and you want to gradually upgrade from JSP to Facelets, then you really have to change it to *.jsf and possibly also all links in the existing JSP pages.

You only need to keep in mind that the new JSF 2.0 provided implicit navigation doesn't scan for the presence of the file, it will go to outcome.xhtml anyway. So if you want to come from or go to *.jsp, then you still need to include it in the viewid the JSF 1.x way.


Facelets 1.x to Facelets 2.0

If you're using Facelets 1.x as view technology and want to use the JSF 2.0 supplied Facelets 2.0, then you need to do the following additional steps:

  • Remove Facelets 1.x JAR from /WEB-INF/lib.

  • Remove Facelets 1.x FaceletViewHandler from faces-config.xml.

  • Any custom FaceletViewHandler implementation needs to be updated to extend ViewHandlerWrapper instead.

  • Not necessary, but just for cleanup, remove any Facelets 1.x related <context-param> values from web.xml which are already default in Facelets 2.0, like the javax.faces.DEFAULT_SUFFIX with value of *.xhtml.

  • Update root declaration of existing Facelet taglib XML's to comply Facelets 2.0.

      <facelet-taglib 
          xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
          version="2.0">
    

    Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XML snippet.

That should basically be it.


JSP 2.x to Facelets 2.0

If you're using JSP 2.x as view technology and you want to upgrade to Facelets 2.0 immediately, then you need to do a lot of changes before the site can go live. You're basically changing the view technology here.

Master page changes

On every master page, you need to change the following basic JSP template..

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html>
<f:view>
    <html lang="en">
        <head>
            <title>JSP page</title>
        </head>
        <body>
            <h:outputText value="JSF components here." />
        </body>
    </html>
</f:view>

..to the following basic Facelets template:

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>XHTML page</title>
    </h:head>
    <h:body>
        <h:outputText value="JSF components here." />
    </h:body>  
</html>

Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XHTML snippets.

Include page changes

If your existing JSP pages are well designed, you should not have any line of scriptlet code and you should also have only the <jsp:include> as the sole JSP-specific tag. Any of those needs to be changed from:

<jsp:include page="include.jsp" />

to

<ui:include src="include.xhtml" />

The basic JSP include page template of..

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<f:subview id="include">
    <h:outputText value="JSF components here." />
</f:subview>

..should be changed to the following basic Facelets include page template:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:outputText value="JSF components here." />
</ui:composition>

Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XHTML snippets.

Custom component changes

You need to change the JSP TLD files to Facelets TLD files as described in this Mojarra Migration Guide.


Aftermath

Regardless of the migration approach, you can gradually eliminate the faces-config.xml by the new JSF 2.0 annotations or even CDI. Any <managed-bean> can be annotated by @ManagedBean:

@ManagedBean(name="managedBeanName")
@RequestScoped
public class SomeBean {}

Next to @RequestScoped, there are also @ViewScoped, @SessionScoped and @ApplicationScoped available. If you omit the name attribute of the @ManagedBean, then it will default to classname with the 1st char lowercased.

@ManagedBean
@RequestScoped
public class SomeBean {}

In this particular example, it will be #{someBean}.

Any <managed-property> can be annotated using @ManagedProperty:

@ManagedProperty("#{otherBean}")
private OtherBean otherBean;

Any <validator> can be annotated using @FacesValidator:

@FacesValidator("someValidator")
public class SomeValidator implements Validator {}

Any <converter> can be annotated using @FacesConverter

@FacesConverter("someConverter")
public class SomeConverter implements Converter {}

Any <renderer> can be annotated using @FacesRenderer

@FacesRenderer(componentFamily="someComponentFamily", rendererType="someRendererType")
public class SomeRenderer extends Renderer {}

Any <navigation-case> which uses the filename of the XHTML page as both <from-outcome> and <to-view-id> can be removed since this will be implicitly done. This can be gradually done by changing all outcome values to match the filename of the target view.

Finally, any session scoped bean which was been put in the session with the sole reason to retain the bean data in subsequent requests in the same tab/window can better be marked @ViewScoped, because this way the bean won't be affected when the enduser opens the same page in different tabs/windows.


Component libraries

Note that I don't take any 3rd party componant libraries like PrimeFaces/RichFaces/IceFaces into account in this answer, it would then be impossible to write a reliable answer since it basically boils down to "it depends". In general it's sufficient to just upgrade the component library to a -by themselves verified- JSF 2.0 compatible version as per their instructions. Best is to just write unit tests, run them before and after the upgrade and fix any issues individually.

Here are at least some useful links with regard to migration of the specific component library:

PrimeFaces has no migration guide for PrimeFaces 1.x to 2.x as PrimeFaces 1.x requires Facelets 1.x already, so you just have to follow Facelets 1.x to 2.x migration steps. However, there's a PrimeFaces 2.x to 3.x (and higher) migration guide which might apply as well on migrating from PrimeFaces 1.x to 3.x (or higher). Tomahawk has also no migration guide. Basically the only which you need to change are the JARs and if necessary get rid of all <t:saveState> references on a request scoped bean by making the bean view scoped.

Advisable answered 26/12, 2010 at 5:27 Comment(7)
@ManagedBean(name="managedBeanName") @RequestScoped It is :)Andvari
great post, helped me a lot. Something to note: when moving from jsf 1.2 to jsf 2 you can be almost certain people have used a4j from richfaces 3.3.x. I decided to use richfaces 3.3.3 together with jsf 2 since it seemed like a mediocre change to perform to upgrade to richfaces 4.x . So I did your guide (undid all facelets related things in faces-config (activated viewhandler removed taglig annotation), then followed community.jboss.org/wiki/RichFaces333AndJSF20 and finally had to do this stackoverflow.com/questions/85532/…Lemur
Great answer. In my case I also had to set the javax.faces.VALIDATE_EMPTY_FIELDS parameter to false to get the validation sorted. See also: stackoverflow.com/questions/6113935/…Conductor
I can also recommend everybody to read balusc.blogspot.nl/2011/09/communication-in-jsf-20.htmlConductor
I'm trying to change the TLD files over, but the Mojarra Migration Guide is gone. Any suggestions?Newmint
@Cfold: I fixed the link.Advisable
@Advisable Does it support Tomahawk ?Therrien
D
7

One thing to mention is that if anyone is using JSTL with JSF 1.2 then when upgrading to JSF2 you should change the namespace from:

http://java.sun.com/jstl/core

to:

http://java.sun.com/jsp/jstl/core

Dislimn answered 24/5, 2013 at 11:39 Comment(2)
Note: this applies only when you're migrating from Facelets 1.x to 2.x.Advisable
And for 2.2 and up, read stackoverflow.com/questions/31068678/…Burned
W
6

JSF 2.0 have many new features and components and I don't feel migration will be painful. Only area you will find difficult is in using thrid party libraries. If your application is heavily dependant upon libraries like Richfaces then you will face problem. Not all the components from Richfaces 3 is ported to Richfaces 4.

This also might help JSF 1.2 application migration to JSF 2.0

Also check this What is new in JSF 2?

Wagtail answered 22/12, 2010 at 7:35 Comment(1)
It would have been the same when you use Richfaces with JSF 1.x- you go through all the "pain" to find out how to integrate third party components with JSF. The approach to JSF 2.x is no difference. That's the "joy" of programming isn't it? :)Syzygy
G
4

Web.xml

 Add the jars
    1. jsf-api-2.0.jar 
    2. jsf-impl.2.0.2.jar

Step 1: Change web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            id="WebApp_ID" version="2.5">


    <servlet>
            <servlet-name>facesServlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>facesServlet</servlet-name>
            <url-pattern>/faces/*</url-pattern>
        </servlet-mapping>
        <servlet-mapping>

            <servlet-name>facesServlet</servlet-name>
            <url-pattern>*.jsf</url-pattern>
        </servlet-mapping>

        <servlet-mapping>
            <servlet-name>facesServlet</servlet-name>
            <url-pattern>*.faces</url-pattern>
        </servlet-mapping>

        <servlet-mapping>
            <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
        </servlet-mapping>

Step 2: webmvc-config.xml

<!-- Handles requests mapped to the Spring Web Flow system -->
    <bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
        <property name="flowExecutor" ref="flowExecutor" />
        <property name="ajaxHandler">
            <bean class="org.springframework.faces.webflow.JsfAjaxHandler" />
        </property>
</bean>

Step3:facess-config.xml

<faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0">
Gruver answered 20/1, 2014 at 6:45 Comment(0)
L
0

If you are using Apache Trinidad you'll also have to upgrade it to version 2.0 so that it will support JSF 2.0. There's more info at Hacker's Valhalla.

Lection answered 16/11, 2011 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.