使用Java Server Faces技术自定义组件

2008-02-23 10:15:00来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

CUSTOM COMPONENTS WITH JavaSERVER FACES TECHNOLOGY

The March 24, 2004 Tech Tip Improving Designs With the MVC Design Pattern introduced the architectural pattern known as Model, View, Controller (MVC or Model2). MVC is a pervasive pattern throughout the world of computer science, and is fundamental to understanding JavaServer Faces (JSF) technology. The pattern separates the data and business logic of an application from its visual representation. The data and business logic is stored in an object called the Model. The visual representation is stored in a separate object called the View. The two objects are linked together with a third object called the Controller. The controller reacts to input from the view and updates the model data accordingly. The advantage of using this design is that any changes to the business logic or data can be isolated to the model without affecting the view. You can create multiple views without affecting the model.

A second tip in the March 24, 2004, titled Introducing JavaServer Faces Technology showed how to create a JSF application that includes GUI components that are modeled by the JSF Framework. In this tip, you'll learn how to create custom components using JSF technology. More specifically, you'll learn how to create a custom JSF technology component that represents a simple stock display. Through an accompanying JavaServer Pages (JSP) page, a user can enter a stock symbol into a input text field and then press the Submit button. In response, the custom component displays a table below the text field. The table contains the stock's symbol, the current price of the stock, and the daily change in the stock price.

This tip assumes that you are familiar with the basics of JSF technology, and you know how to create JSP technology custom tag libraries. For information on the basics of JSF technology, see An Introduction to JavaServer Faces. For information on creating your own JSP custom tag libraries, see Using Custom Tags in the J2EE 1.4 Tutorial.

To create the custom component, you need to:

  • Create a JavaBean Model class that can be used to retrieve the stock data

  • Create a custom JSF View output component class that extends javax.faces.component.UIComponent.

  • Create a custom JSF View class that extends javax.faces.render.Renderer.

  • Integrate the custom component into the JSF framework using a custom tag.

  • Create a JSP page, including an input text field, and a backing bean for the input text field to assist the custom component.

Here is a visual representaion of how these objects fit in the MVC architecture.

Create a JavaBean Model

The model for the stock component is simple JavaBean class that has get and set Accessor methods to store and retrieve data. The bean also has a method, retrieveStock(), that the UI component calls when the user enters the stock symbol. This method takes the entered stock symbol and updates the model information accordingly.

The sample archive that accompanies this tip includes all the source code for the stock component. In it you'll find the following source code for the class StockModel, the Model component:
   public class StockModel {



       private String selectedStock = "UNKNOWN";

       private String currentValue = "0";

       private String dailyChange = "0";



       public String getSelectedStock() { return selectedStock; }

       public String getCurrentValue() { return currentValue; }

       public String getDailyChange() { return dailyChange; }



       public void retrieveStock(String stockSubmitted) {

           selectedStock = stockSubmitted;

           currentValue = "23.5";

           dailyChange = " 2.5";

       }

   }

Typically a method such as retrieveStock() would query a database to obtain the data, but for simplicity, the method in this class updates the rest of the model with the constant strings specified in the class.

Create a UI Component

The primary purpose of the UI Component, which is part of the view, is to take data from the model and either display it itself, or relay it to a custom renderer to display the data as necessary. A custom UI component extends javax.faces.component.UIComponent. This class extends javax.faces.component.UIOutput, where the UIOutput class is a subclass of the UIComponent class. With UIOutput, most of the view functionality that is needed by the custom component is already provided. The only addition that this class needs is the getFamily() method, which returns a String that describes the component family (this must match an entry in the resource descriptor later). Here is the source code for the class UIStockChoiceOutput:

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:关于Date和Calendar类的基础用法

下一篇:JbuilderX使用Junit学习笔记