您的位置:首页 > Web前端 > JavaScript

JSF2自定义组件编程系列 第六部分

2011-01-07 15:10 387 查看
 在前一个例子中,我创建了一个Backing class:HtmlInput2,这是用来代表Composite
Component的顶层对象NamingContainer的类。这给了一个机会让我可以覆盖encode/decode方法,从而用Java代码增强
Composite Compnent的行为。

  本例子更进一步,再创建一个Managed Bean,用来接收用户的输入,拦截点击按钮事件,并显示用户的输入。注意,这个新的Managed
Bean在我的jsfex项目内,因此实际上是Composite
Compnent内部实现的一部分,并不要求用户定义。创建这个例子只是为了说明其实添加Managed Bean和普通的JSF开发没有区别。

现在修改一下htmlinput2.xhtml代码:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:f="http://java.sun.com/jsf/core"

      xmlns:h="http://java.sun.com/jsf/html"

      xmlns:ui="http://java.sun.com/jsf/facelets"

      xmlns:composite="http://java.sun.com/jsf/composite">

  <composite:interface componentType="HtmlInput2">

    <!—editableValueHolder is desigend for validator —>

    <composite:editableValueHolder name="inputField" target="in"/>

    <composite:valueHolder name="outputField" target="out"/>

  </composite:interface>

  <composite:implementation>

    <h:inputText id="in" value="#{inputBean.value}" required="true"/>

    <h:commandButton id="clickButton" value="Click Me!" actionListener="#{inputBean.print}"/>

    <h:outputText id="out" value="#{inputBean.value}"/>

  </composite:implementation>

</html>

在com.freebird.component包下创建一个InputBean类。

package com.freebird.component;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.faces.event.ActionEvent;

/**

  * Describe class InputBean here.

  *

  *

  * Created: Fri Jan 7 14:15:36 2011

  *

  * @author <a href="mailto:chenshu@csdesktop">chenshu</a>

  * @version 1.0

  /

@ManagedBean(name="inputBean")

@RequestScoped

public class InputBean {

  /*

    * Describe value here.

    /

  private String value;

  /*

    * Creates a new
InputBean
instance.

    *

    /

  public InputBean() {

  }

  private Logger getLogger(){

    return Logger.getLogger(getClass().getName());

  }

  public void print(ActionEvent event){

    getLogger().info("enter print method");

  }

  /*

    * Get the
Value
value.

    *

    * @return a
String
value

    /

  public final String getValue() {

    getLogger().info("enter getValue:"+value);

    return value;

  }

  /*

    * Set the
Value
value.

    *

    * @param newValue The new Value value.

    /

  public final void setValue(final String newValue) {

    getLogger().info("enter setValue:"+newValue);

    this.value = newValue;

  }

}

同时,把原来HtmlInput2.java中的encodeBegin代码去掉。 注意,这里的拦截点击按钮事件的print方法只是打印了一个日志。只是为了说明我们能够在内部轻松的处理事件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息