Java 从 JSF 1.2 迁移到 JSF 2.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4441713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Migrating from JSF 1.2 to JSF 2.0
提问by mkoryak
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编写的相当大的应用程序。JSF 1.2 现在大约 6 岁了。我需要升级到 JSF 2.0。这会有多痛苦?我注意到自定义标签中的某些属性已更改等。
采纳答案by BalusC
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.
将 JSF 1.2 升级到 2.0 的痛苦取决于您当前使用的视图技术以及您想要使用的视图技术。
- 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.
- JSP 2.x 到 JSP 2.x = 几乎不费吹灰之力。
- Facelets 1.x 到 Facelets 2.0 = 很少努力。
- JSP 2.x 到 Facelets 2.0 = 很多努力。如果您还有自定义组件,则加倍。
Basic changes
基本变化
Regardless of the view technology switch, at leastthe 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 ofhttp://java.sun.com
throughout the above XML snippet.Ensure that root declaration of
web.xml
already complies at leastServlet 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 ofhttp://java.sun.com
throughout the above XML snippet.
- 从
/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 规范。<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 片段中。确保根声明至少
web.xml
符合Servlet 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 或更新版本时,请使用
http://xmlns.jcp.org
命名空间域,而不是在http://java.sun.com
整个上述 XML 片段中。
JSP 2.x to JSP 2.x
JSP 2.x 到 JSP 2.x
If you're using JSP 2.xand want to keepusing it, then you basically don't need to change anything else.
如果您正在使用JSP 2.x并希望继续使用它,那么您基本上不需要更改任何其他内容。
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.
如果您已经使用后缀url-pattern
的FacesServlet
,喜欢*.jsf
的话,那是很好的知道,FacesServlet
会先扫描*.xhtml
文件,如果它不存在,然后扫描*.jsp
文件。这为您提供了在幕后逐步从 JSP 转换为 Facelets 的空间,而无需更改 URL。
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.
但是,如果您正在使用前缀url-pattern
, like/faces/*
并且您希望逐渐从 JSP 升级到 Facelets,那么您真的必须将其更改*.jsf
为现有 JSP 页面中的所有链接,并且可能还包括所有链接。
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.
您只需要记住,新的 JSF 2.0 提供的隐式导航不会扫描文件的存在,outcome.xhtml
无论如何它都会去。所以如果你想来自或去*.jsp
,那么你仍然需要将它包含在 JSF 1.x 方式的 viewid 中。
Facelets 1.x to Facelets 2.0
Facelets 1.x 到 Facelets 2.0
If you're using Facelets 1.xas view technology and want to use the JSF 2.0 supplied Facelets 2.0, then you need to do the following additional steps:
如果您使用Facelets 1.x作为视图技术并希望使用 JSF 2.0 提供的Facelets 2.0,那么您需要执行以下附加步骤:
- Remove Facelets 1.x JAR from
/WEB-INF/lib
. - Remove Facelets 1.x
FaceletViewHandler
fromfaces-config.xml
. - Any custom
FaceletViewHandler
implementation needs to be updated to extendViewHandlerWrapper
instead. - Not necessary, but just for cleanup, remove any Facelets 1.x related
<context-param>
values fromweb.xml
which are already default in Facelets 2.0, like thejavax.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 ofhttp://java.sun.com
throughout the above XML snippet.
- 从
/WEB-INF/lib
. - 删除Facelets的1.x的
FaceletViewHandler
距离faces-config.xml
。 - 任何自定义
FaceletViewHandler
实现都需要更新以进行扩展ViewHandlerWrapper
。 - 没有必要,但只是为了清理,删除任何与 Facelets 1.x 相关的
<context-param>
值,web.xml
这些值在 Facelets 2.0 中已经是默认javax.faces.DEFAULT_SUFFIX
值,例如with 值*.xhtml
。 更新现有 Facelet taglib XML 的根声明以符合 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 片段中。
That should basically be it.
基本上应该是这样。
JSP 2.x to Facelets 2.0
JSP 2.x 到 Facelets 2.0
If you're using JSP 2.xas view technology and you want to upgrade to Facelets 2.0immediately, then you need to do a lot of changes before the site can go live. You're basically changing the view technology here.
如果您使用JSP 2.x作为视图技术并且想要立即升级到Facelets 2.0,那么您需要在站点上线之前进行大量更改。你基本上改变了这里的视图技术。
Master page changes
母版页更改
On every master page, you need to change the following basic JSP template..
在每个母版页上,您需要更改以下基本 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>
..to the following basic Facelets template:
..到以下基本 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>
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.
注意:当您使用 JSF 2.2 或更新版本时,请使用http://xmlns.jcp.org
命名空间域,而不是http://java.sun.com
在上述 XHTML 片段中。
Include page changes
包括页面更改
If your existing JSP pages are well designed, you should not have any line of scriptletcode and you should also have only the <jsp:include>
as the sole JSP-specific tag. Any of those needs to be changed from:
如果您现有的 JSP 页面设计得很好,那么您不应该有任何脚本代码行,并且您还应该只有<jsp:include>
作为唯一的 JSP 特定标记。其中任何一个都需要从:
<jsp:include page="include.jsp" />
to
到
<ui:include src="include.xhtml" />
The basic JSP include page template of..
基本的 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"%>
<f:subview id="include">
<h:outputText value="JSF components here." />
</f:subview>
..should be changed to the following basic Facelets include page template:
.. 应该改为以下基本的 Facelets 包含页面模板:
<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.
注意:当您使用 JSF 2.2 或更新版本时,请使用http://xmlns.jcp.org
命名空间域,而不是http://java.sun.com
在上述 XHTML 片段中。
Custom component changes
自定义组件更改
You need to change the JSP TLD files to Facelets TLD files as described in this Mojarra Migration Guide.
您需要将 JSP TLD 文件更改为 Facelets TLD 文件,如本Mojarra 迁移指南中所述。
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
:
无论采用何种迁移方法,您都可以faces-config.xml
通过新的 JSF 2.0 注释甚至CDI逐渐消除。Any<managed-bean>
可以通过@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.
旁边@RequestScoped
,还有@ViewScoped
,@SessionScoped
和@ApplicationScoped
可用。如果省略 的name
属性@ManagedBean
,那么它将默认为第一个字符小写的类名。
@ManagedBean
@RequestScoped
public class SomeBean {}
In this particular example, it will be #{someBean}
.
在这个特定的例子中,它将是#{someBean}
.
Any <managed-property>
can be annotated using @ManagedProperty
:
任何<managed-property>
可以使用注释@ManagedProperty
:
@ManagedProperty("#{otherBean}")
private OtherBean otherBean;
Any <validator>
can be annotated using @FacesValidator
:
任何<validator>
可以使用注释@FacesValidator
:
@FacesValidator("someValidator")
public class SomeValidator implements Validator {}
Any <converter>
can be annotated using @FacesConverter
任何<converter>
可以使用注释@FacesConverter
@FacesConverter("someConverter")
public class SomeConverter implements Converter {}
Any <renderer>
can be annotated using @FacesRenderer
任何<renderer>
可以使用注释@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 implicitlydone. This can be gradually done by changing all outcome values to match the filename of the target view.
任何<navigation-case>
同时使用 XHTML 页面的文件名<from-outcome>
并且<to-view-id>
可以删除的,因为这将被隐式完成。这可以通过更改所有结果值以匹配目标视图的文件名来逐步完成。
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.
最后,为了在同一选项卡/窗口中的后续请求中保留 bean 数据的唯一原因而放入会话中的任何会话范围 bean 可以更好地标记@ViewScoped
,因为这样当最终用户打开时 bean 不会受到影响不同选项卡/窗口中的同一页面。
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.
请注意,在此答案中,我没有考虑任何 3rd 方组件库,例如 PrimeFaces/RichFaces/IceFaces,因此不可能写出可靠的答案,因为它基本上归结为“视情况而定”。一般来说,只需按照他们的说明将组件库升级到一个由他们自己验证的 JSF 2.0 兼容版本就足够了。最好是编写单元测试,在升级前后运行它们并单独修复任何问题。
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 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 guidewhich 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.
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,如有必要,可以<t:saveState>
通过使 bean 视图具有范围来摆脱对请求范围 bean的所有引用。
回答by mvg
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.
JSF 2.0 有许多新特性和组件,我不觉得迁移会很痛苦。您会发现唯一困难的地方是使用第三方库。如果您的应用程序严重依赖于 Richfaces 之类的库,那么您将面临问题。并非 Richfaces 3 中的所有组件都移植到 Richfaces 4。
This also might help JSF 1.2 application migration to JSF 2.0
这也可能有助于 JSF 1.2 应用程序迁移到 JSF 2.0
Also check this What is new in JSF 2?
还要检查这个JSF 2中有什么新东西?
回答by designatevoid
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.
如果您使用的是 Apache Trinidad,您还必须将其升级到 2.0 版,以便它支持 JSF 2.0。Hacker's Valhalla 中有更多信息。
回答by Rafal G.
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:
需要提及的一件事是,如果有人在 JSF 1.2 中使用 JSTL,那么在升级到 JSF2 时,您应该将命名空间从:
to:
到:
回答by Pravin
Web.xml
网页.xml
Add the jars
1. jsf-api-2.0.jar
2. jsf-impl.2.0.2.jar
Step 1: Change web.xml
第 1 步:更改 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
第二步: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
第三步: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">