新建Web Project,引入JSF的jar文件
commons-beanutils.jar
commons-collections.jar
commons-digester.jar
commons-logging.jar
jsf-api.jar
jsf-impl.jar
jstl.jar
standard.jar
注意这里引入了JSTL的jar包
Web.xml在web-app标签里需要加入
<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>/faces/*</url-pattern>
</servlet-mapping>
javax.faces.webapp.FacesServlet是初始化加载的类,servlet-name可自定义
<load-on-startup>标签为设置加载的优先级
Servlet-mapping配置jsf访问的映射信息,我这里配置的是访问页面前需加入faces,如faces/index.jsp
在Web-Info下新建faces-config.xml,这是jsf的配置文件,名称固定,如果你映入了jsf的jar包,xml的图标应该会有F字样,我们看看JSF的配置文件:
<?xml version="1.0"
encoding="UTF-8"?>
<!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>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>
com.bruceleey.bean.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
<faces-config>是JSF的父标签,JSF的其余标签需被包含在此标签内
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<from-view-id>表示设置从此页面传输信息到<to-view-id>里
<from-outcome>为action跳转的标识名称,如jsf的标签可这样写<h:commandButton value="发送" action="login"
/>
<managed-bean>里设置bean的访问信息与bean的值域,这里设置的session范围
入门很简单,详细代码见附件
JSF01