手工搭建纯JSF二三事(facelet实现)

首先,用向导建立Web Project;

 

然后,引入必须包如下(版本不需要一致):

commons-beanutils-1.6.1.jar

commons-codec-1.2.jar

commons-collections-2.1.1.jar

commons-digester.jar

commons-lang-2.3.jar

el-api.jar

el-ri.jar

jsf-facelets-1.1.12.jar

jstl.jar

standard.jar

另外,可选使用myfaces实现:

myfaces-api-1.1.5.jar

myfaces-impl-1.1.5.jar

 

再在web.xml里设置JSF的配置文件:

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- context-param区 start --> <context-param> <param-name>webAppRootKey</param-name> <param-value>jsfDemo.root</param-value> </context-param> <!-- faces配置 start --> <context-param> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value> /WEB-INF/classes/facesConfig/faces-config.xml, /WEB-INF/classes/facesConfig/faces-config-myTagLib.xml </param-value> </context-param> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>server</param-value> </context-param> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/myTagLib.xml</param-value> </context-param> <!-- faces配置 end --> <!-- context-param区 end --> <!-- listener区 start --> <!-- myfaces start --> <listener> <listener-class> org.apache.myfaces.webapp.StartupServletContextListener</listener-class> </listener> <!-- myfaces end --> <!-- listener区 end --> <!-- servlet区 start --> <!-- JSF Servlet start --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <!-- JSF Servlet end --> <!-- servlet区 end --> <!-- servlet-mapping区 start --> <!-- JSF servlet-mapping start --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> <!-- JSF servlet-mapping end --> <!-- servlet-mapping区 end --> <!-- welcome-file-list区 start --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- welcome-file-list区 end --> </web-app>

请养成好习惯,注意web.xml的规范,防止某些严格执行规范的中间件无法识别。

 

需要特别注意的是,如果设置了默认后缀如:

<context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param>

就需要设置他的解释器。

而解释器是在刚刚定义的face-config.xml这个JSF的配置文件里设置的:

<?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> <application> <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> </application> <managed-bean> <managed-bean-name>TestJsf</managed-bean-name> <managed-bean-class>test.TestJsf</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> </faces-config>

需要设置的是application部分。

 

最后按照myface或者其他中间件开发商的实现进行页面编码后,就能以通过http://host:port/project/path/xxx.faces的后缀名访问了。

你可能感兴趣的:(Web,servlet,中间件,JSF,application,encoding)