Programming

JSF 1.2에서 JSF 2.0으로 마이그레이션

procodes 2020. 6. 27. 15:12
반응형

JSF 1.2에서 JSF 2.0으로 마이그레이션


JSF 1.2로 작성된 다소 큰 앱으로 작업하고 있습니다. JSF 1.2는 6 세 정도입니다. JSF 2.0으로 업그레이드해야합니다. 이것이 얼마나 고통 스러울까요? 맞춤 태그의 일부 속성이 변경되었습니다.


고통 스러움

JSF 1.2를 2.0으로 업그레이드하는 데 따르는 고통은 현재 사용중인 뷰 기술에 따라 다릅니다.

  • JSP 2.x에서 JSP 2.x로 = 거의 노력이 없습니다.
  • Facelets 1.x-Facelets 2.0 = 적은 노력.
  • Facelets 2.0에 대한 JSP 2.x = 많은 노력. 사용자 컴포넌트가있는 경우이를 두 배로 늘리십시오.

기본 변경

뷰 기술 스위치에 관계없이 최소한 다음 단계를 수행해야합니다.

  • JSF 1.2 JAR을 /WEB-INF/lib(있는 경우) 제거하십시오 .
  • JSF 2.0 JAR을 삭제하십시오 /WEB-INF/lib(JSF 1.2가 서블릿 컨테이너 제공 인 경우, 서블릿 컨테이너 라이브러리보다 먼저 웹 애플리케이션 라이브러리를로드하도록 클래스로드 정책을 변경하고자 할 수도 있습니다 . 애플리케이션 서버의 JSF2 클래스로드 문제 참조 ).
  • faces-config.xmlJSF 2.0 사양을 준수하도록 루트 선언을 업데이트하십시오 .

    <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">
    

    참고 : JSF 2.2 이상을 사용하는 경우 위의 XML 스 니펫 http://xmlns.jcp.org대신 네임 스페이스 도메인을 사용하십시오 http://java.sun.com.

  • 루트 선언이 web.xml이미 서블릿 2.5 이상을 준수하는지 확인하십시오 . JSF 2.0은 2.4 이하에서 작동하지 않습니다 ( 해킹 가능하지만 ).

    <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">
    

    참고 : Servlet 3.0 이상을 사용하는 경우 위의 XML 스 니펫 http://xmlns.jcp.org대신 네임 스페이스 도메인을 사용하십시오 http://java.sun.com.


JSP 2.x에서 JSP 2.x로

당신이 사용하는 경우 JSP 2.x를을 하고 싶어 유지 를 사용하여, 당신은 기본적으로 다른 변경 아무것도 필요하지 않습니다.

점차 업그레이드

와 같은 접미사 url-pattern이미 사용 하고 있다면 먼저 파일을 스캔하고 파일이 없으면 파일을 스캔 한다는 것을 아는 것이 좋습니다 . 이를 통해 URL을 변경하지 않고 JSP에서이면 뒤에서 Facelets로 점진적으로 변환 할 수 있습니다.FacesServlet*.jsfFacesServlet*.xhtml*.jsp

당신이 접두사를 사용하는 경우 그러나 url-pattern처럼, /faces/*당신은 점차적으로 JSP에서 Facelets의로 업그레이드하려면, 당신은 정말 그것을 변경해야 할 *.jsf도 가능 기존의 JSP 페이지에있는 모든 링크를합니다.

새로운 JSF 2.0 제공 암시 적 탐색은 파일의 존재를 스캔하지 않으며 outcome.xhtml어쨌든 진행된다는 것을 명심해야 합니다. 따라서에서 오거나 나가려면 *.jspJSF 1.x 방식으로 viewid에 포함시켜야합니다.


Facelets 1.x에서 Facelets 2.0으로

당신이 사용하는 경우 Facelets의 1.x에서 보기 기술 등을하고 JSF 2.0 공급 사용할 Facelets의 2.0 , 당신은 다음과 같은 추가 단계를 수행해야합니다

  • 에서 Facelets 1.x JAR을 제거하십시오 /WEB-INF/lib.
  • FaceletViewHandler에서 Facelets 1.x 제거하십시오 faces-config.xml.
  • 대신 FaceletViewHandler확장하려면 모든 사용자 지정 구현을 업데이트해야 ViewHandlerWrapper합니다.
  • 필요하지는 않지만 정리의 경우 with 값이 있는 Facelets 2.0에서 이미 기본값 인 Facelets 1.x 관련 <context-param>값을 제거하십시오 .web.xmljavax.faces.DEFAULT_SUFFIX*.xhtml
  • Facelets 2.0을 준수하도록 기존 Facelet taglib XML의 루트 선언을 업데이트하십시오.

    <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">
    

    참고 : JSF 2.2 이상을 사용하는 경우 위의 XML 스 니펫 http://xmlns.jcp.org대신 네임 스페이스 도메인을 사용하십시오 http://java.sun.com.

기본적으로 그렇습니다.


Facelets 2.0에 대한 JSP 2.x

JSP 2.x 를 뷰 기술로 사용 하고 Facelets 2.0으로 즉시 업그레이드하려는 경우 사이트를 활성화하기 전에 많은 변경을 수행해야합니다. 기본적으로 뷰 기술을 변경하고 있습니다.

마스터 페이지 변경

모든 마스터 페이지에서 다음 기본 JSP 템플리트를 변경해야합니다.

<%@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>

.. 다음 기본 Facelets 템플릿으로 :

<!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>

참고 : JSF 2.2 이상을 사용하는 경우 위의 XHTML 스 니펫 http://xmlns.jcp.org대신 네임 스페이스 도메인을 사용하십시오 http://java.sun.com.

페이지 변경 사항 포함

기존 JSP 페이지가 잘 디자인 된 경우 스크립틀릿 코드 이 없어야 <jsp:include>하며 유일한 JSP 특정 태그 만 있어야합니다 . 다음 중 하나를 변경해야합니다.

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

<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.


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


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?


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">

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.

참고URL : https://stackoverflow.com/questions/4441713/migrating-from-jsf-1-2-to-jsf-2-0

반응형