JSF1与JSF2的区别

很多年前用过JSF,当时还是1.2版本。现在JSF最新的版本是2.2.11

http://mvnrepository.com/artifact/com.sun.faces/jsf-impl

JSF1和JSF2在使用上有很大区别,JSF2做了很多改进,简化了开发人员的工作。

写这篇文档的目的是为那些维护legacy系统,想迁移到JSF2的同学提供参考。

1)现在大都使用maven作为编译和打包的工具,同时也是项目管理的工具。在pom.xml文件中,JSF2只需要jsf-api.jar和jsf-impl.jar两个jar包就够了。配置如下

 	<dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.11</version>
            <scope>compile</scope>
        </dependency>

而JSF1不仅需要这两个jar包,还需要jstl.jar和servlet-api.jar

              <dependency>
		  <groupId>javax.faces</groupId>
		  <artifactId>jsf-api</artifactId>
		  <version>1.2_14</version>
		</dependency>
		
		<dependency>
		  <groupId>javax.faces</groupId>
		  <artifactId>jsf-impl</artifactId>
		  <version>1.2_14</version>
		</dependency>
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>

 这里要注意jsf jar包的groupId在JSF1和JSF2中是不同的。

JSF1.2

<groupId>javax.faces</groupId>

JSF2.2

<groupId>com.sun.faces</groupId>

2)JSF2支持annotation,而JSF1不支持,只能在faces-config.xml中配置manangedBean。

@ManagedBean

@SessionScoped

在faces-config.xml中,JSF1和JSF2也有不同:JSF1使用dtd来定义xml文件格式,而JSF2使用xsd来定义文件格式。

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>

</faces-config>
<?xml version="1.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">

</faces-config>
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

</faces-config>

3) JSF1前端页面只支持jsp,并且使用<f:view>标签,例如

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<html>
<head>
<title>JSF in Action - Hello, world!</title>
</head>
<body>
	<h:form id="welcomeForm">
		<h:outputText id="welcomeOutput" value="Welcome to JavaServer Faces!"
			style="font-family: Arial, sans-serif; font-size: 24;
color: green;" />
		<p>
			<h:message id="errors" for="helloInput" style="color: red" />
		</p>
		<p>
			<h:outputLabel for="helloInput">
				<h:outputText id="helloInputLabel"
					value="Enter number of controls to display:" />
			</h:outputLabel>
			<h:inputText id="helloInput" value="#{helloBean.numControls}"
				required="true">
				<f:validateLongRange minimum="1" maximum="500" />
			</h:inputText>
		</p>
		<p>
			<h:panelGrid id="controlPanel" binding="#{helloBean.controlPanel}"
				columns="20" border="1" cellspacing="0" />
		</p>
		<h:commandButton id="redisplayCommand" type="submit" value="Redisplay"
			actionListener="#{helloBean.addControls}" />
		<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
			action="#{helloBean.goodbye}" immediate="true" />
	</h:form>
</body>
</html>
</f:view>

 JSF2中支持xhtml,也就是说,前端页面只负责显示,没有任何java代码。例如

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
	<title>Hello World</title>
	<meta http-equiv="keywords" content="enter,your,keywords,here" />
	<meta http-equiv="description"
		content="A short description of this page." />
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
	<h:form id="form">
		<div
			style="padding: 100px 0 0 100px; font-size: 22px; font-weight: bold">
			<h:commandButton value="Say Hello To" />
			<h:inputText value="#{hello.name}" />
			<br></br> Hello, #{hello.name}!
		</div>
	</h:form>
</h:body>
</html>

4)在web.xml中JSF1和JSF2都可以这样配置

  <servlet>
    <servlet-name>faces</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>faces</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>

 JSF2还可以指定文件类型为xhtml,例如

	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>

以上就是JSF1和JSF2使用过程中的主要区别。注意这些,可以帮助你在升级的过程中少走弯路。

推荐学习资源

http://www.coreservlets.com/JSF-Tutorial/jsf2/

http://www.tutorialspoint.com/jsf/

猜你喜欢

转载自freesky-zh.iteye.com/blog/2213103
jsf