您的位置:首页 > Web前端 > JavaScript

JSF环境搭建web.xml和faces-config.xml配置

2016-04-28 10:38 597 查看

转载:https://www.geek-share.com/detail/2551959320.html

 

JSF提供的核心控制器是javax.faces.webapp.FacesServlet。

web.xml中的welcome-file-list标签作用:当用户在浏览器中输入的URL不包含某个servlet名或JSP页面时,welcome-file-list元素可指定显示的默认文件。 welcome-file子元素用于指定默认文件的名称。welcome-file-list元素可以包含一个或多个welcome-file子元素。如果在第一个welcome-file元素中没有找到指定的文件,Web容器就会尝试显示第二个,以此类推。

在web.xml中配置FacesServlet核心控制器:

<!-- JSF的核心控制器 FacesServlet -->
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

这里设置了拦截.jsf结尾的请求。

需要为JSF配置一些额外的参数,可以在web.xml中使用<context-param>元素进行配置。下面是常用的一些配置:



<!-- 配置JSF程序状态的保存位置,如果设置成server则保存在session中,
如果保存在client中可以保证服务器重启应用状态也不会丢失 -->
<context-param>
<description>
指示是在客户端(client)还是在服务器端(server)保存UI组件的状态和
session信息, server表示保存到服务器端,client表示保存到客户端,
默认为server。
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<!-- 程序状态保存在客户端 -->
<param-value>client</param-value>
</context-param>

<!-- 指定JSF映射资源时的默认后缀,默认为.jsp -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsp</param-value>
</context-param>

<!-- 指定JSF所管理的生命周期实例的标识符 -->
<context-param>
<description>
当在这个应用中处理JSF请求时,
所使用的生命周期实例的标识符.
默认的生命周期模型(没看懂)
</description>
<param-name>javax.faces.LIFECYCLE_ID</param-name>
<param-value></param-value>
</context-param>

<!-- 指定JSF配置文件的保存位置 -->
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config-beans.xml,/WEB-INF/faces-config-nav.xml</param-value>
</context-param>

<!-- 指定是否需要验证自定义组件 -->
<context-param>
<description>
Set this flag to true if you want the JSF
Reference Implementation to verify that all of the application
objects you have configured (components, converters,
renderers, and validators) can be successfully created.
Default value is false.
</description>
<param-name>javax.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>

<!-- 指定是否需要验证XML文件 -->
<context-param>
<description>
Set this flag to true if you want the JavaServer Faces
Reference Implementation to validate the XML in your
faces-config.xml resources against the DTD. Default
value is false.

</description>
<param-name>javax.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>

<!-- 当设置在服务器端保存状态时,控制session保存的视图数量,-1表示没有限制 -->
<context-param>
<param-name>javax.faces.NUMBER_OF_VIEWS_IN_SESSION</param-name>
<param-value>-1</param-value>
</context-param>

JSF的配置文件faces-config.xml:

        最常用的两个配置元素:

       <managed-bean>:JSF应用中所有的托管Bean都放在该元素下        
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>
onlyfun.caterpillar.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
       <navigation-rule>:用于管理JSF应用的导航规则       
<navigation-rule>
<from-view-id>/pages/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/welcome.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/pages/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>

 

       其他的配置元素:
       应用管理相关配置:

       <application>:用于管理JSF应用相关配置
       <referenced-bean>:配置被引用Bean

       注册自定义组件的相关配置:

       <converter>:注册自定义转换器
       <validator>:注册自定义输入校验器
       <component>:注册自定义组件
       <render-kit>:注册自定义组件绘制器和绘制器包

       高级扩展配置:

       <phase-listener>:注册生命周期监听器
       <factory>:配置实例化JSF核心类的工厂   参考:http://www.itzhai.com/jsf-notes-jsf-configuration-file-configuration-instructions-and-common-elements.html         http://baike.baidu.com/view/1002819.htm         http://javaee.blog.51cto.com/941919/20285   阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: