从JSF 1.2迁移到JSF 2.0 [英] Migrating from JSF 1.2 to JSF 2.0

查看:151
本文介绍了从JSF 1.2迁移到JSF 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JSF 1.2 编写的相当大的应用程序。
JSF 1.2现在已经有6年了。我需要升级到JSF 2.0。这会有多痛苦?我注意到自定义标签中的一些属性已被更改等。

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.

推荐答案

痛苦



将JSF 1.2升级到2.0的痛苦程度取决于您当前使用的视图技术以及您要使用的视图技术。

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到JSP 2.x =几乎没有努力。

  • Facelets 1.x到Facelets 2.0 =很少努力。

  • JSP 2。 x到Facelets 2.0 =很多努力。如果您还有自定义组件,请加倍。

无论视图技术开关如何,至少应该执行以下步骤:

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


  • / WEB-INF / lib (如果有的话)中删除JSF 1.2 JAR。

  • 删除JSF 2.0 JAR / WEB-INF / lib (如果JSF 1.2是servletcontainer提供的,您可能希望更改类加载策略以在servletcontainer库之前首先加载webapp库,另请参阅应用程序服务器中的JSF2类加载问题)。

  • 更新 faces-config.xml 的根声明以符合JSF 2.0规范。

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


  • 确保 web.xml的根声明已经符合至少 Servlet 2.5。 JSF 2.0无法在2.4或更低版本上运行(虽然它可以破解)。

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


  • 如果你正在使用 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.

    如果您已使用后缀 url-pattern 对于 FacesServlet ,如 *。jsf ,那么很高兴知道 FacesServlet 将首先扫描 *。xhtml 文件,如果不存在,则扫描 * .jsp 文件。这为您提供了在不更改URL的情况下逐步从JSP转换为Facelets的空间。

    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.

    但如果您使用前缀 url-pattern ,例如 / faces / * 并且您希望逐步从JSP升级到Facelets,然后您真的必须将其更改为 *。jsf 以及可能还需要将其更改为现有的JSP页面。

    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.

    你只需要记住,新的JSF 2.0提供的隐式导航不会扫描文件是否存在,它将转到<无论如何,code> outcome.xhtml 。因此,如果你想来或者去 * .jsp ,那么你仍然需要将它包含在JSF 1.x方式的viewid中。

    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 作为视图技术并且想要使用JSF 2.0提供的 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:


    • / WEB-INF / lib 中删除​​Facelets 1.x JAR。

    • 删除Facelets 1.x FaceletViewHandler 来自 faces-config.xml

    • 任何自定义 FaceletViewHandler 实现需要更新以扩展 ViewHandlerWrapper

    • 没必要,但只是为了清理,删除任何Facelets 1.x与< context-param> 相关的值来自 web.xml e已在Facelets 2.0中默认使用,例如 javax.faces.DEFAULT_SUFFIX ,其值为 *。xhtml

    • 更新现有Facelet taglib XML的根声明以符合Facelets 2.0。

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


    基本上应该是它。

    如果你正在使用 JSP 2.x 作为视图技术,您希望立即升级到 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.

    在每个母版页上,您需要更改以下基本JSP模板..

    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>
    

    ..到以下基本Facelets模板:

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



    包含页面更改



    如果您现有的JSP页面设计得很好,你不应该有任何 scriptlet 代码行,你也应该只有< jsp:include> 作为唯一的JSP特定标记。其中任何一个都需要更改:

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

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

    基本的JSP包含页面模板..

    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>
    

    ..应更改为以下基本Facelets包含页面模板:

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



    自定义组件更改



    您需要按照此 Mojarra Migration Guide <中所述将JSP TLD文件更改为Facelets TLD文件/ a>。

    Custom component changes

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

    无论迁移如何方法,您可以逐步消除 faces-config.xml 由新的JSF 2.0注释。任何< managed-bean> 都可以通过 @ManagedBean

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

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

    <$ c旁边$ c> @RequestScoped ,还有 @ViewScoped @SessionScoped @ApplicationScoped 可用。如果省略 @ManagedBean name 属性,那么它将默认为classname,并且第一个char为小写。

    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 {}
    

    在这个特定的例子中,它将是#{someBean}

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

    任何< managed-property> 都可以使用 @ManagedProperty

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

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

    任何< validator> 都可以注释使用 @FacesValidator

    Any <validator> can be annotated using @FacesValidator:

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

    任何< converter> 都可以注释使用 @FacesConverter

    Any <converter> can be annotated using @FacesConverter

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

    任何< renderer> 都可以使用注释 @FacesRenderer

    Any <renderer> can be annotated using @FacesRenderer

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

    Any < navigation-case> 使用XHTML页面的文件名作为< from-outcome> 和<可以删除code>< to-view-id> ,因为这将是隐式地完成了。这可以通过更改所有结果值以匹配目标视图的文件名来逐步完成。

    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.

    最后,任何会话范围内的bean都已放入会话中在同一个选项卡/窗口中保留bean数据的后续请求的原因可以更好地标记为 @ViewScoped ,因为这样在最终用户打开时bean不会受到影响不同标签/窗口中的相同页面。

    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.

    请注意,我不会在这个答案中考虑PrimeFaces / RichFaces / IceFaces等任何第三方组件库,因此它不可能写出可靠的答案,因为它基本上归结为它取决于。通常,只需按照说明将组件库升级为自己验证的JSF 2.0兼容版本就足够了。最好是编写单元测试,在升级之前和之后运行它们并单独解决任何问题。

    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:

    • RichFaces Migration Guide - 3.3.x to 4.x migration
    • IceFaces 2 Wiki - IceFaces 1.x Compatibility Guide

    PrimeFaces没有PrimeFaces 1.x到2.x的迁移指南,因为PrimeFaces 1.x需要Facelets 1 .x已经,所以你只需要遵循Facelets 1.x到2.x的迁移步骤。但是,有一个可能适用的PrimeFaces 2.x到3.x迁移指南以及从PrimeFaces 1.x迁移到3.x.战斧也没有移民指南。基本上,您需要更改的唯一内容是JAR,如果需要,通过使bean视图作用域,删除所有< t:saveState> 对请求范围bean的引用。

    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 migration guide which might apply as well on migrating from PrimeFaces 1.x to 3.x. 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.

    这篇关于从JSF 1.2迁移到JSF 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆