JSF-JavaFX

JSF is specification from Sun Micro Systems. Popular implementations are MyFaces from Apache, ADF from Oracle, Reference implementation from sun etc.

I would like to discuss about creating a custom JavaFX component in JSF. Using this you can embedd JavaFX easily in jsp pages. You can

use these component just like any other tag.
Example
  <jfx:applet id:"TestApplet"  code="test.class" archive="test.jar" draggable="true" width="400" height="400"/>

How to create a custom JavaFX component:

Step1 : Creating JavaFXCompoent by extending UIComponentBase.

public class JavaFXComponent extends UIComponentBase {

    @Override
    public String getFamily() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void encodeEnd(FacesContext context) throws IOException {      
       String archive = (String)getAttributes().get("archive");
       String draggable = (String)getAttributes().get("draggable");
       String width = (String)getAttributes().get("width");
       ...
              String script = "<script src=" + javafx_script +"></script>\n" +
                "<script> \n" +
                "javafx(\n" +
                    "{\n" +
                          "archive:"+archive +",\n"+
                          "draggable:"+ draggable +",\n"+
         .....
                ");\n" +
                "</script>";
     }
Here is the complete JavaFXComponent classPlace this file under source directory.
Register this class name in faces-config.xml step3.

Step 2: Create the Tag handler by extending UIComponentELTag .

public class JavaFXTagHandler extends UIComponentELTag {

    private String id;
    private String archive;
    private String code;
   ...

    ValueExpression ve;
    FacesContext fc = FacesContext.getCurrentInstance();
    ExpressionFactory factory =  fc.getApplication().getExpressionFactory();


    private void setValue(UIComponent component, String name, String value) {
        if (value != null) {
            ve = factory.createValueExpression(fc.getELContext(), value, String.class);
            component.setValueExpression(name, ve);
        }
    }

    @Override
    protected void setProperties(UIComponent component) {
        super.setProperties(component);
        setValue(component, "id", id);
        setValue(component, "archive", archive);
....
Register this class in tld file  see step4.
Here is complete JavaFXTagHandler class

Step3: Adding JavaFXComponet in faces-config.xml file.

<faces-config>

    <component>
        <component-type>JavaFX</component-type>
        <component-class>com.sun.training.JavaFXComponent</component-class>
    </component>
  ....
</faces-config>
faces-config.xml should be placed WEB-INF directory.

Step4: Creating the javafx.tld file .

<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>javafx</short-name>
  <uri>http://java.sun.com/jsf/javafx/</uri>
    <tag>
        <name>JavaFX</name>
        <tagclass>com.sun.training.JavaFXTagHandler</tagclass>
        <bodycontent>JSP</bodycontent>
        <info>
            This is a custom JavaFX applet component
        </info>
        <attribute>
            <name>archive</name>
            <required>true</required>
        </attribute>
.......
Refer here for the complete tld file .
Create this file under WEB-INF or under its subdirectories.

I thought this would be usefull for your web pages.

Comments:

Good morning Raghu,

Your approach is great since this is the first concrete example I saw on the Internet for integrating JavaFX with JSF.

However, the document seems to be broken. For example, there is no link to complete tld and <jfx:applet> is not refered somewhere else. So I cannot follow the example.

Would you please provide more details? Thanks a lot.

Posted by Sheng Huang on August 12, 2009 at 07:08 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by Raghu Nair