用Java Server Faces建立交互式WEB站点

步骤1:开发组件的Java类
   组件类将负责管理代表组件状态的属性,因此,我们必须根据组件的行为(如输入组件或输出组件),为组件选择适当的基类。
   在列表A中描述的组件扩展了javax.faces.component.UIOutput,以显示指向某个样式表文件的URL,或内联式样式表的内容。
  列表A
  import javax.faces.component.*;
  public class CSSComponent extends UIOutput {
  private Boolean link;
  public String getFamily() {
  return "faces.CSSFamily";
  }
  public boolean isLink() {
  if (link != null)
  return link.booleanValue();
  ValueBinding vb = getValueBinding("link");
  if (vb != null) {
  Boolean bvb = (Boolean) vb.getValue(FacesContext.
  getCurrentInstance());
  if (bvb != null)
  return bvb.booleanValue();
  }
  return false;
  }
  public void setLink(boolean link) {
  this.link = new Boolean(link);
  }
  public Object saveState(FacesContext context) {
  return new Object[] { super.saveState(context), link };
  }
  public void restoreState(FacesContext context,
  Object stateObj) {
  Object[] state = (Object[]) stateObj;
  super.restoreState(context, state[0]);
  link = (Boolean) state[1];
  }
  }
   代码中“关联”的属性规定了值的类型:要么是一个URL,要么是内联样式。该组件还必须能够在向服务器发送请求期间,使用经过JSF架构处理过的对象,来存储并恢复自己的状态。JSF架构自动调用saveState和restoreState方法,我们可以在组件中实现这两种方法来达到这一目标。
  步骤2:编写渲染器
   渲染程序有两个作用。首先,渲染程序负责发送适当的HTML程序段,该程序段能在客户端中渲染组件。通常情况下,这个HTML程序段由一些适于渲染整个网络浏览器的HTML标签组成,这个渲染阶段还能发送增强客户端交互性的JavaScript代码。
   渲染程序的第二个作用是对来自客户端的数据进行解码,从而对服务器端的组件状态进行更新(比如用户在文本字段输入的文本)。标准渲染程序软件包具有强制 性,但也可以提供其他渲染程序软件包,用于提供其他的客户端表示方法或SVG之类的语言。
   通过检验组件的链接属性,在列表2中实现的渲染程序将选择在HTML页面中发送的CSS样式类别。
  列表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>http://www.ilog.com/jviews/tlds/css.tld</uri>
  <description>This tag library contains a tag for a sample custom JSF Component.</description>
  <tag>
  <name>css</name>
  <tag-class>path.to.CSSTag</tag-class>
  <description>A component that displays the style inline or a link a to a css fi le</description>
  <attribute>
  <name>id</name>
  <required>false</required>
  <rtexprvalue>false</rtexprvalue>
  <type>java.lang.String</type>
  <description>The id of this component.</description>
  </attribute>
  <attribute>
  <name>binding</name>
  <required>false</required>
  <rtexprvalue>false</rtexprvalue>
  <type>java.lang.String</type>
  <description>The value binding expression linking this component to a
  property in a backing bean. If this attribute is set, the tag does not
  create the component itself but retrieves it from the bean property.
  This attribute must be a value binding.</description>
  </attribute>
  <attribute>
  <name>value</name>
  <required>true</required>
  <rtexprvalue>false</rtexprvalue>
  <type>java.lang.String</type>
  <description>The inline css text or the url to the css fi le to link.</description>
  </attribute>
  <attribute>
  <name>link</name>
  <required>false</required>
  <rtexprvalue>false</rtexprvalue>
  <type>java.lang.String</type>
  <description>Whether the value is a link or the inline style.</description>
  </attribute>
  </tag>
  </taglib>
  步骤5:JSF配置文件
   为了将一个JSF组件集成到架构中,必须提供一个名为faces-config.xml的配置文件。这个文件将用于JSP自定义标 签处理器的组件类型和渲染器类型与对应的Java类关联起来。它还能描述与每个组件一同使用的渲染器。
   列表E定义了faces.CSSFamily组件家族。
  列表E
  <!DOCTYPE faces-confi g PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Confi g 1.0//EN"
  "http://java.sun.com/dtd/Web-facesconfi g_1_0.dtd">
  <faces-confi g>
  <component>
  <component-type>faces.CSSComponent</component-type>
  <component-class>path.to.CSSComponent</component-class>
  <component-extension>
  <component-family>faces.CSSFamily</component-family>
  <renderer-type>HTML.LinkOrInlineRenderer</renderer-type>
  </component-extension>
  </component>
  <render-kit>
  <renderer>
  <component-family>faces.CSSFamily</component-family>
  <renderer-type> HTML.LinkOrInlineRenderer </renderer-type>
  <renderer-class>path.to.CSSRenderer</renderer-class>
  </renderer>
  </render-kit>
  </faces-confi g>
   在这个例子中,该家族由faces.CSSComponent类型的单一组件组成,该类型是与CSSComponent类绑定在一起的。最后, 由CSSRenderer类实现的HTML.LinkOrInlineRenderer类型的渲染器与faces.CSSFamily家族相关联。
   开发者还可以通过提供额外的信息将组件整合到支持JSF的集成开发环境(IDE)中,以Sun Creator IDE为例,必须提供一个名为sun-faces-config.xml的XML配置文件,这个配置文件描述了应当显示给IDE的组件属性和一些组件的设计信息。关于Sun Studio Creator的更详细的使用说明和构建自定义JSF组件的信息,我强烈推荐SUN的开发者网站。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值