在JSP页面中应用JSF技术(Using JavaServer Faces Technology in JSP Pages chapter10)《注:未完成》

在JSP页面中应用JSF技术

页面作者的职责是设计JSF应用的页面。这包含在页面上如何布置组件,并把他们和后台beans,验证器,转换器和其他服务器端对象关联起来。这一章用Duke的书店这个应用和Coffee Break应用(see Chapter  37) 来介绍页面作者如何应用JSF标签来:
    • 页面上的布局标准UI组件
    • 引用本地化消息
    • 注册转换器,验证器和组件上的监听器
    • 将组件及组件值和其服务器对象绑定
    • 引用后台bean方面进行导航处理,处理事件,进行数据验证
这一章也介绍了如何在页面上包含由应用开发者和组件作者创建的定制对象。

JSF应用的例子

JSF技术那一章已经通过Duke的书店应用的重写版本图示说明了JSF技术的基本主题。这个版本包括一些JSF技术功能:
    • JSF实现提供了FacesServlet,这个servlet接收请求并且将它们发送到处理流程。因此,应用中不需要再包含servlet(比如一个Dispatcher servlet)来根据请求参数分发请求,而其他的版本都是包含这样的Dispatcher servlet的。
    • 定制图片映射组件允许你为应用选择地点
    • 导航在应用的配置文件中配置,这些配置需要对比URL,就像其他版本一样。
    • Backing beans associated with the pages. These beans hold the component data and perform other processing associated with the components. This processing includes handling the event generated when a user clicks a button or a hyperlink.
    • The table that displays the books from the database and the shopping cart are rendered with the dataTable tag, which is used to dynamically render data in a table. The dataTable tag on bookshowcart.jsp also includes input components.
    • The table that displays the books from the database uses a c:forEach JSTL tag, demonstrating that you can easily use JavaServer Faces component tags with JSTL tags.
    • A custom validator and a custom converter are registered on the credit card field of the bookcashier.jsp page.
    • A value-change listener is registered on the Name field of bookcashier.jsp. This listener saves the name in a parameter so that bookreceipt.jsp can access it.

This version of Duke's Bookstore includes the same pages listed in Table 4-1. It also includes the chooselocale.jsp page, which displays the custom image map that allows you to select the locale of the application. This page is displayed first and advances directly to the bookstore.jsp page after the locale is selected.

The packages of the Duke's Bookstore application are:

    • backing: Includes the backing bean classes
    • components: Includes the custom UI component classes
    • converters: Includes the custom converter class
    • listeners: Includes the event handler and event listener classes
    • model: Includes a model bean class
    • renderers: Includes the custom renderers
    • resources: Includes custom error messages for the custom converter and validator
    • taglib: Includes custom tag handler classes
    • util: Includes a message factory class
    • validators: Includes a custom validator class

Chapter 11 describes how to program backing beans, custom converters and validators, and event listeners. Chapter 12 describes how to program event handlers, custom components, renderers, and tag handlers.

The source code for the application is located in the <INSTALL>/javaeetutorial5/examples/web/bookstore6/ directory. To build and package the example, follow these steps:

    • Go to <INSTALL>/javaee5utorial/examples/web/bookstore6/ and run ant.
    • Start the Application Server.

To deploy the example run ant deploy.

To learn how to configure the example, refer to the web.xml file, which includes the following configurations:

    •   A display-name element that specifies the name that tools use to identify the application.
    • A context-param element that specifies that the javax.faces.STATE_SAVING_METHOD parameter has a value of client, meaning that state is saved on the client.
    • A listener element that identifies the ContextListener class used to create and remove the database access.
    • A servlet element that identifies the FacesServlet instance.
    • A servlet-mapping element that maps FacesServlet to a URL pattern.

To run the example, open the URL http://localhost:8080/bookstore6 in a browser.

Setting Up a Page

A typical JavaServer Faces page includes the following elements:

    • A set of tag library declarations that declare the two JavaServer Faces tag libraries.
    • A view tag
    • A form tag

This section tells you how to add these elements to your pages and briefly describes the subview tag for including JavaServer Faces pages inside other pages.

To use the JavaServer Faces UI components in your JSP page, you need to give the page access to the two standard tag libraries: the JavaServer Faces HTML render kit tag library and the JavaServer Faces core tag library. The JavaServer Faces standard HTML render kit tag library defines tags that represent common HTML user interface components. The JavaServer Faces core tag library defines tags that perform core actions and are independent of a particular render kit.

Using these tag libraries is similar to using any other custom tag library. This chapter assumes that you are familiar with the basics of using custom tags in JSP pages (see Using Custom Tags, page 137).

As is the case with any tag library, each JavaServer Faces tag library must have a TLD that describes it. The html_basic TLD describes the The JavaServer Faces standard HTML render kit tag library. The jsf_core TLD describes the JavaServer Faces core tag library.

Please refer to the TLD documentation at http://java.sun.com/javaee/javaserverfaces/1.2/docs/tlddocs/index.html for a complete list of the JavaServer Faces tags and their attributes.

Your application needs access to these TLDs in order for your pages to use them. The Application Server includes these TLDs in jsf-impl.jar, located in <JavaEE_HOME>/lib.

To use any of the JavaServer Faces tags, you need to include these taglib directives at the top of each page containing the tags defined by these tag libraries:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

The uri attribute value uniquely identifies the TLD. The prefix attribute value is used to distinguish tags belonging to the tag library. You can use other prefixes rather than the h or f prefixes. However, you must use the prefix you have chosen when including the tag in the page. For example, the form tag must be referenced in the page via the h prefix because the preceding tag library directive uses the h prefix to distinguish the tags defined in html_basic.tld:

<h:form ...>  

A page containing JavaServer Faces tags is represented by a tree of components. At the root of the tree is the UIViewRoot component. The view tag represents this component on the page. Therefore, all component tags on the page must be enclosed in the view tag, which is defined in the jsf_core TLD:

<f:view>
  ... other JavaServer Faces tags, possibly mixed with other
  content ...
</f:view>

You can enclose other content, including HTML and other JSP tags, within the view tag, but all JavaServer Faces tags must be enclosed within the view tag.

The view tag has four optional attributes:

    • A locale attribute. If this attribute is present, its value overrides the Locale stored in the UIViewRoot component. This value is specified as a String and must be of this form:

    :language:[{-,_}:country:[{-,_}:variant]

    The :language:, :country:, and :variant: parts of the expression are as specified in java.util.Locale.

    • A renderKitId attribute. A page author uses this attribute to refer to the ID of the render kit used to render the page, therefore allowing the use of custom render kits. If this attribute is not specified, the default HTML render kit is assumed. The process of creating custom render kits is outside the scope of this tutorial.
    • A beforePhase attribute. This attribute references a method that takes a PhaseEvent object and returns void, causing the referenced method to be called before each phase (except restore view) of the life cycle begins.
    • An afterPhase attribute. This attribute references a method that takes a PhaseEvent object and returns void, causing the referenced method to be called after each phase (except restore view) in the life cycle ends.

An advanced developer might implement the methods referenced by beforePhase and afterPhase to perform such functions as initialize or release resources on a per-page basis. This feature is outside of the scope of this tutorial.

The form tag is nested inside of the view tag. As its name suggests, the form tag represents a form, which is submitted when a button or hyperlink on the page is clicked. For the data of other components on the page to be submitted with the form, the tags representing the components must be nested inside the form tag. See Adding a Form Component for more details on using the form tag.

If you want to include a page containing JavaServer Faces tags within another JSP page tht includes JavaServer Faces tags, you must enclose the entire nested page in a subview tag. You can add the subview tag on the parent page and nest a jsp:include inside it to include the page:

<f:subview id="myNestedPage">
  <jsp:include page="theNestedPage.jsp" />
</f:subview>

You can also include the subview tag inside the nested page, but it must enclose all the JavaServer Faces tags on the nested page.

The subview tag has two optional attributes: binding and rendered. The binding attribute binds to a component that implements NamingContainer. One potential use case of binding a subview component to a bean is if you want to dynamically add components to the subview in the backing bean.

The rendered attribute can be set to true or false, indicating whether or not the components nested in the subview tag should be rendered.

In summary, a typical JSP page that uses JavaServer Faces tags will look somewhat like this:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:view>
  <h:form>
    other JavaServer Faces tags and core tags,
    including one or more button or hyperlink components for
    submitting the form
  </h:form>
</f:view>

The sections Using the Core Tags and Adding UI Components to a Page Using the HTML Component Tags describe how to use the core tags from the JavaServer Faces core tag library and the component tags from the JavaServer Faces standard HTML render kit tag library.

Using the Core Tags

The tags included in the JavaServer Faces core tag library are used to perform core actions that are independent of a particular render kit. These tags are listed in Table 10-1.

Table 10-1 The jsf_core Tags 
Tag Categories
Tags
Functions
Event-handling
tags
actionListener
Registers an action listener on a parent component
phaseListener
Registers a PhaseListener instance on a UIViewRoot component
setPropertyActionListener
Registers a special action listener whose sole purpose is to push a value into a backing bean when a form is submitted
valueChangeListener
Registers a value-change listener on a parent component
Attribute
configuration tag
attribute
Adds configurable attributes to a parent component
Data conversion tags
 
converter
Registers an arbitrary converter on the parent component
convertDateTime
Registers a DateTime converter instance on the parent component
convertNumber
Registers a Number converter instance on the parent component
Facet tag
facet
Signifies a nested component that has a special relationship to its enclosing tag
Localization tag
loadBundle
Specifies a ResourceBundle that is exposed as a Map
Parameter
substitution tag
param
Substitutes parameters into a MessageFormat instance and adds query string name-value pairs to a URL
Tags for representing items in a list
selectItem
Represents one item in a list of items in a UISelectOne or UISelectMany component
selectItems
Represents a set of items in a UISelectOne or UISelectMany component
Container tag
subview
Contains all JavaServer Faces tags in a page that is included in another JSP page containing JavaServer Faces tags
Validator tags
validateDoubleRange
Registers a DoubleRangeValidator on a component
validateLength
Registers a LengthValidator on a component
validateLongRange
Registers a LongRangeValidator on a component
validator
Registers a custom validator on a component
Output tag
verbatim
Generates a UIOutput component that gets its content from the body of this tag
Container
for form
tags
view
Encloses all JavaServer Faces tags on the page

These tags are used in conjunction with component tags and are therefore explained in other sections of this tutorial. Table 10-2 lists the sections that explain how to use specific jsf_core tags.

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值