网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  使用java server faces技术自定义组件     
  文章作者:未知  文章来源:水木森林  
  查看:114次  录入:管理员--2007-11-17  
 
  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:
public class uistockchoiceoutput extends uioutput {

public static final string stock_family = "stockfamily";

public uistockchoiceoutput() {
super();
}

public string getfamily() {
return stock_family;
}

}

create a custom renderer

the next step is to create a renderer for how this jsf component should appear in the client browser. this is a custom view class that extends the javax.faces.render.renderer class. the purpose of this renderer is to create the output html table for the stock information display. although it is not the case here, renderers can also be responsible for rendering input. hence, a renderer can perform two types of translation services: decoding any input data into a format that can be passed on to the controller, and encoding any data from the controller object and rendering it to the browser. decoding is done by the decode() method in the renderer. encoding can be done by a number of encoding methods. this example uses the encodeend() method. here is an extract of the source code for simplestockrenderer, the renderer (and custom view component):

public class simplestockrenderer extends renderer {

public void encodeend(facescontext context,
uicomponent component) throws ioexception
{
if (!component.isrendered())
return;

string selectedstock =
(string)component.getattributes().get("selectedstock");
string currentvalue =
(string)component.getattributes().get("currentvalue");
string dailychange =
(string)component.getattributes().get("dailychange");
string id = (string)component.getclientid(context);

responsewriter out = context.getresponsewriter();

out.startelement("table", component);
out.writeattribute("border", "0", "border");

out.startelement("tr", component);
out.writeattribute("bgcolor", "#000077", "bgcolor");

writetableheader(out, component, "stock symbol");
writetableheader(out, component, "current value");
writetableheader(out, component, "daily change");

out.endelement("tr");

out.startelement("tr", component);
out.writeattribute("bgcolor", "#ffffff", "bgcolor");

writetableentry(out, component, selectedstock);
writetableen
 
 
上一篇: 使用hibernate进行对象的关系映射(1)    下一篇: 使用java.util.calendar返回间隔天数
  相关文档
java语言编程技术中汉字问题的分析及解决 04-10
如何让apache支持frontpage 2000 11-17
如何在恰当的时间处理恰当的bug一(图) 11-17
jbuilder2006破解补丁 11-17
java相关基础知识(5) 11-17
使用ejb组件你需要了解些什么呢? 11-17
用java取得本机的ip和机器名 11-17
虚拟机监视器xen和虚拟化技术(一) 11-17
java中finalize()的另类用法(1) 11-16
java技巧:简化jdbc的开发 11-17
java入门教程:运算符和表达式 11-17
ibm发布的europa推21个eclipse开源项目 11-16
jsp 动态网站技术入门与提高 11-17
科学计算器 11-17
关于java编程语言中ejb容器存取和实现说明 04-02
jbuilder9+weblogic platfrom 8.1+oracle 9i安装和配置 11-17
java反编译的研究 11-16
一位通过scjd的外国人的心得 11-17
java2下applet数字签名(1) 11-17
javascript实例教程(19) 使用hotmetal(3) 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息