S2JSFでのカスタムコンポーネント作成

S2JSFのカスタムコンポーネント作成方法をメモ書きしておく

s2-framework-2.4.7.jar
s2-extension-2.4.7.jar
s2-jsf-1.1.1.jar
teeda-core-1.0.3.jar
teeda-extension-1.0.3.jar

既存のJSFクラスと同じ作業

1.UIコンポーネントクラスを作成

UINow.java

package now;

import javax.faces.component.UIComponentBase;

public class UINow extends UIComponentBase {
    public String getFamily() {
        return "MyFamily";
    }
}

2.タグハンドラクラス

NowTag.java

package now;

import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;

public class NowTag extends UIComponentTag {

    private String format;

    public String getComponentType() {
        return "Now";
    }

    public String getRendererType() {
        return "NowRenderer";
    }

    protected void setProperties(UIComponent component) {
        super.setProperties(component);
        UINow now = (UINow)component;
        now.getAttributes().put("format", format);
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }
}

3. レンダラ

NowRenderer.java

package now;

import java.util.Date;
import java.text.DateFormat;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

public class NowRenderer extends Renderer {

    public void encodeBegin(
                FacesContext context, UIComponent component)
                throws IOException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }

        Date d = new Date();
        DateFormat df;
        UINow now = (UINow)component;        
        String format = (String)now.getAttributes().get("format");
        
        if (format.equals("FULL")) {
            df = DateFormat.getDateTimeInstance(
                DateFormat.FULL, DateFormat.FULL);
        } else if (format.equals("LONG")) {
            df = DateFormat.getDateTimeInstance(
                DateFormat.LONG, DateFormat.LONG);
        } else if (format.equals("MEDIUM")) {
            df = DateFormat.getDateTimeInstance(
                DateFormat.MEDIUM, DateFormat.MEDIUM);
        } else if (format.equals("SHORT")) {
            df = DateFormat.getDateTimeInstance(
                DateFormat.SHORT, DateFormat.SHORT);
        } else {
            df = DateFormat.getDateTimeInstance();
        }

        ResponseWriter writer = context.getResponseWriter();
        writer.write(df.format(d));
    }
}

4. タグライブラリディスクリプタ (TLD)

now.tld

<?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>now</short-name>
    <tag>
        <name>now</name>
        <tag-class>now.NowTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>format</name>
            <required>true</required>
        </attribute>
    </tag>
</taglib>

5. faces-config.xml の設定

faces-config.xml

<?xml version="1.0"?>

<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>

    <component>
        <component-type>Now</component-type>
        <component-class>now.UINow</component-class>
    </component>

    <render-kit>
        <renderer>
            <component-family>MyFamily</component-family>
            <renderer-type>NowRenderer</renderer-type>
            <renderer-class>now.NowRenderer</renderer-class>
        </renderer>
    </render-kit>

</faces-config>

参考URL
http://www.wakhok.ac.jp/~tomoharu/jsf2004/text/index_c11.html

Seasar2用実装

1. jsf.dicon

<component>
           :
           :
	<initMethod name="addTaglibUri">
	     <arg>"z"</arg>
	     <arg>"http://example.com/hoge/taglib"</arg>
	</initMethod>
           :
           :
</component>

2. HTML

<html xmlns:z="http://example.com/hoge/taglib" />
        :
        :
<span m:inject="z:now" m:format="FULL" />
<span m:inject="z:now" m:format="LONG" />
<span m:inject="z:now" m:format="MEDIUM" />
<span m:inject="z:now" m:format="SHORT" />
        :
        :
</html>