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

查看:41
本文介绍了从 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">

注意:当您使用 JSF 2.2 或更新版本时,请使用 http://xmlns.jcp.org 命名空间域而不是 http://java.sun.com 贯穿上述 XML 片段.

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.

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

注意:当您使用 Servlet 3.0 或更新版本时,请使用 http://xmlns.jcp.org 命名空间域而不是 http://java.sun.com 贯穿上述 XML 片段.

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 并希望继续使用它,那么您基本上不需要更改任何其他内容.

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

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

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 提供的隐式导航不会扫描文件是否存在,它无论如何都会转到 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.
  • faces-config.xml 中删除 Facelets 1.x FaceletViewHandler.
  • 任何自定义FaceletViewHandler 实现都需要更新以扩展ViewHandlerWrapper 代替.
  • 没有必要,但只是为了清理,从 web.xml 中删除任何与 Facelets 1.x 相关的 值,这些值在 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">

注意:当您使用 JSF 2.2 或更新版本时,请使用 http://xmlns.jcp.org 命名空间域而不是 http://java.sun.com 贯穿上述 XML 片段.

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.

基本上应该是这样.

如果您使用 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>

注意:当您使用 JSF 2.2 或更新版本时,请使用 http://xmlns.jcp.org 命名空间域而不是 http://java.sun.com 贯穿上述 XHTML 片段.

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.

如果你现有的 JSP 页面设计得很好,你不应该有任何 scriptlet 代码行,你也应该只有 作为唯一的 JSP 特定标签.其中任何一个都需要从:

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>

注意:当您使用 JSF 2.2 或更新版本时,请使用 http://xmlns.jcp.org 命名空间域而不是 http://java.sun.com 贯穿上述 XHTML 片段.

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.

您需要按照此中所述将 JSP TLD 文件更改为 Facelets TLD 文件Mojarra 迁移指南.

无论采用何种迁移方式,您都可以通过新的 JSF 2.0 注释甚至 CDI.任何 都可以通过 @ManagedBean:

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

@RequestScoped,还有@ViewScoped@SessionScoped@ApplicationScoped 可用.如果省略 @ManagedBeanname 属性,那么它将默认为第一个字符小写的类名.

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

任何 都可以使用 @ManagedProperty:

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

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

可以使用 rel="noreferrer">@FacesValidator:

Any <validator> can be annotated using @FacesValidator:

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

可以使用 rel="noreferrer">@FacesConverter

Any <converter> can be annotated using @FacesConverter

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

可以使用 rel="noreferrer">@FacesRenderer

Any <renderer> can be annotated using @FacesRenderer

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

任何 使用 XHTML 页面的文件名作为 可以删除,因为这将是 隐式完成.这可以通过更改所有结果值以匹配目标视图的文件名来逐步完成.

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.

请注意,在此答案中,我没有考虑任何 3rd 方组件库,例如 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:

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(或更高版本).Tomahawk 也没有迁移指南.基本上,您唯一需要更改的是 JAR,如有必要,可以通过使 bean 视图成为范围来摆脱请求范围 bean 上的所有 <t:saveState> 引用.

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.

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

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