Java Server Faces建立交互式WEB站点(2)

2008-02-23 08:05:22来源:互联网 阅读 ()

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

列表B
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
public class CSSRenderer extends Renderer {
public void encodeEnd(FacesContext context,
UIComponent component)
throws IOException {
super.encodeEnd(context, component);
if (component instanceof CSSComponent) {
CSSComponent cssComponent =
(CSSComponent) component;
String css = (String)cssComponent.getValue();
boolean isLink = cssComponent.isLink();
if (css != null)
if (isLink)
context.getResponseWriter().write("<link type='text/
css' rel='stylesheet' href='" css "'/>");
else
context.getResponseWriter().write("<style>\n" css
"\n<style/>\n");
}
}
}

步骤3:编写标签类

  同样,JSF架构提供了用于扩展的基类,来编写与组件相关的标签。该标签类将负责:

定义将在faces-config.xml文件中应用的组件类型和渲染类型,我们将在下一部分具体介绍这个XML文件。

创建JSF组件(由JSF架构来处理)并传递JSF标签中所包含的属性来初始化组件。

  在列表C中的标签提供了setter和getter来管理链接和值的属性。

列表C
import javax.faces.Webapp.UIComponentTag;
public class CSSTag
extends UIComponentTag {
private String value;
private String link;
public String getComponentType() {
return "faces.CSSComponent";
}
public String getRendererType() {
return "HTML.LinkOrInlineRenderer";
}
protected void setProperties(UIComponent component)
{
super.setProperties(component);
Application app = getFacesContext().getApplication();
if (value != null)
if (isValueReference(value))
component.setValueBinding("value",
app.createValueBinding(value));
else
component.getAttributes().put("value", value);
if (link != null)
if (isValueReference(link))
component.setValueBinding("link",
app.createValueBinding(link));
else
component.getAttributes().put("link",
new Boolean(link));
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

组件一旦创建,便会调用setPropertie方法,对标签属性进行初始化。每个标签属性要么是文字值,要么是bean属性的一个绑定。

步骤4:编写一个标签库定义(TLD)

TLD是一个XML文件,它通过将标签名与相应的Java类相关联来描述标签。TLD还描述了标签所允许的属性。

在列表D中的这个TLD定义了一个名为“css”的标签,该标签被绑定到CSSTag类。它还声明了链接和值标签的属性。

列表D
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/Web-jsptaglibrary_1_
2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>custom</short-name>
<uri>

标签:

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

上一篇:使用Annotations设计一个MVC框架

下一篇:J2EE中使用Spring AOP框架和EJB组件