JBoss Development is brought to you in partnership with:

Max Katz heads Developer Relations for Appery.io, a cloud-based mobile app platform. He loves trying out new and cool REST APIs in mobile apps. Max is the author of two books “Practical RichFaces” (Apress 2008, 2011), and is a frequent speaker at developer conferences. You can find out what Max is up to on his blog: mkblog.exadel.com and Twitter: @maxkatz. Max is a DZone MVB and is not an employee of DZone and has posted 80 posts at DZone. You can read more from them at their website. View Full User Profile

Using JSF and Flex Components Together

09.16.2008
| 101785 views |
  • submit to reddit

Decoders and Encoders

First, why do we even need these decoders and encoders? Well, when using the Flex HttpService, the data is being sent to the server and returned in XML format. The JSF managed bean that we are using doesn't know anything about XML. It simply returns a string value. So, in order to convert the request into a format that the managed bean understands, we use a decoder. When we invoke a method, the value returned has to be encoded in order for the Flash module to understand it. I know, it sounds complicated, but it's really not.

The decoder shown below will be called by Fiji and the value returned will be passed to the greeting(Object) method inside a JSF managed bean. The decoder will just pull the value from the XML sent by the request.

public class HelloDecoder implements FlexDecoder , Serializable {

private static final String ENDPOINT_PARAM = "hello";

public Object decodeRequest(FacesContext context, UIComponent component) {
return context.getExternalContext().getRequestParameterMap().get(ENDPOINT_PARAM);
}
}

When the greeting(Object) method inside the JSF managed bean returns a value, this encoder will be called to convert the response back to XML format.

public class HelloEncoder implements FlexEncoder {

private static final String VALUE = "value";

public void encodeObject(FacesContext context, UIComponent component,
Object object) throws IOException {
ResponseWriter responseWriter = context.getResponseWriter();
responseWriter.startElement(VALUE, component);
responseWriter.writeText(object, null);
responseWriter.endElement(VALUE);

}
}

 

You can implement different encode and decode methods in case your Flash module requires the XML request/response to be in a specific format.


Summary

This article showed how you can use JSF and Flex component together while using standard JSF managed beans to provide data for the Flash components. That's what makes this framework unique. As a developer, you can continue using the standard component-centric JSF approach, but now you are able to inject even richer content using Flash.

To learn more, please visit the Fiji home page: http://exadel.com/web/portal/fiji

About the Author

Max Katz is a Senior Systems Engineer at Exadel. He has been helping customers jump-start their RIA development as well as providing mentoring, consulting, and training. Max is a recognized subject matter expert in the JSF developer community. He has provided JSF/RichFaces training for the past three years, presented at many conferences, and written several published articles on JSF-related topics. Max also leads Exadel's RIA strategy and writes about RIA technologies in his blog, http://mkblog.exadel.com. He is an author of Practical RichFaces book (Apress). Max holds a BS in computer science from the University of California, Davis.

Published at DZone with permission of Max Katz, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Agata Przybyszewska replied on Mon, 2009/02/02 - 8:19pm

I have tried to follow the tutorial, and make Fiji work with Seam and Flex.

I have tried several approaches:
1) Followed the tutorial  here:
 Result - server crash from the helloworld on page 3 :-(

2) I tried to follow the tutorials advice that fiji is just a rich faces project - just copied the 2 files fiji-api.jar and fiji-ui.jar into the lib
folder of my HelloWorld Seam project, and added the following facelet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:rich="http://richfaces.org/rich"
          xmlns:a4j="http://richfaces.org/a4j"
      xmlns:fiji="http://exadel.com/fiji">

        <rich:panel>
                <f:facet name="header">
                        Fiji
                </f:facet>
                Ready for Fiji...

                <fiji:swf src="hello.swf" width="400" height="400"
bgcolor="#EFEFEF"
                        application="HelloWorld"/>
        </rich:panel>
  </ui:composition>

The hello.swf file liest in the same folder as the facelet file.
This didn't crash the server. But the result is not what I wanted - no
Flash component.

 Any ideas what I do wrong?

Best,

Agata

Joe Greene replied on Tue, 2009/03/03 - 12:47pm

Max -

You give little guidance on how to update an amChart wrapped inside a fiji:swf component.  Looking at the amCharts docs, it looks like the data wants to be either csv or xml format.  If I want to use an am line chart wrapped in the fiji:swf component, I need to know how to bind the chart data to a bean instead of xml or csv data.  Can this be done?  If not, I think this is important and should be stated.

Max Katz replied on Mon, 2009/03/09 - 12:39pm in response to: Joe Greene

For anyone else looking for an answer, Joe posted it here

Santhosh Kumar replied on Wed, 2010/01/06 - 11:23am

your article on fiji chart was great..

I am using fiji line chart.. i need  a help on line chart..

i need to highlight the  y axis based on input given,,,,

for exampl if y-axis value is 45... i want to show highlihted y-axis region 40-50...

 

 

thanks in advance... 

Dani Holiday replied on Thu, 2010/02/18 - 4:30am

Hi! I saw that in RichFaces i have to read the data from the xml/csv. I bring the data from the database. if so, how can i integrate RichFaces + AMCharts + reading the data from the database and sending it to the bean? thanks!

Devika Nara replied on Fri, 2010/02/19 - 12:07am

is it possible to add the flex components dynamically to JSF page???

Design Geeks replied on Sat, 2010/02/27 - 12:51am

hi,

good example.

I have tried it with Netbeans 6.8 & glassfish 2.0 wirh jsf-richfaces

//jsp code

<%@taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@taglib uri="http://richfaces.org/rich" prefix="rich"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="fiji" uri="http://exadel.com/fiji"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<f:view locale="#{skinBean.currentlang}">
    <html>
        <h:form>

            <fiji:stackedColumnChart   id="stackedColumnChart" value="#{oneSeriesBar.data}"
                                     title="Simple Stacked Column Chart" subtitle="Display Resolution Statistics"
                                     captionX="Years" captionY="Percent" legendCaption="Resolution"
                                     barColors="#{oneSeriesBar.colors}" width="400" height="400"
                                     rulersValuesHighlight="none">
                <fiji:chartData type="name" value="#{oneSeriesBar.names}" />
            </fiji:stackedColumnChart>
        </h:form>
    </body>
</html>
</f:view>

at runtime it gives xml error like Retrieved XML is invalid

 stacktrace

<ROOT XMLNS:FIJI='HTTP://WWW.EXADEL.COM/FIJI'>#b0343c<FIJI:BARCOLORS>#dd9f2c</FIJI:BARCOLORS><FIJI:BARCOLORS>#5db2c2</FIJI:BARCOLORS><FIJI:CAPTIONX>Years</FIJI:CAPTIONX><FIJI:CAPTIONY>Percent</FIJI:CAPTIONY><FIJI:LEGENDCAPTION>Resolution</FIJI:LEGENDCAPTION><FIJI:RULERSVALUESHIGHLIGHT>none</FIJI:RULERSVALUESHIGHLIGHT><FIJI:SUBTITLE>Display Resolution Statistics</FIJI:SUBTITLE><FIJI:TITLE>Simple Stacked Column Chart</FIJI:TITLE><FIJI:CHARTDATA><FIJI:SERIES><FIJI:DATA><FIJI:X>2006<FIJI:Y>17<FIJI:DATA><FIJI:X>2007<FIJI:Y>26<FIJI:DATA><FIJI:X>2008<FIJI:Y>38<FIJI:NAME>Higher<FIJI:SERIES><FIJI:DATA><FIJI:X>2006<FIJI:Y>57<FIJI:DATA><FIJI:X>2007<FIJI:Y>54<FIJI:DATA><FIJI:X>2008<FIJI:Y>48<FIJI:NAME>1024x768<FIJI:SERIES><FIJI:DATA><FIJI:X>2006<FIJI:Y>20<FIJI:DATA><FIJI:X>2007<FIJI:Y>14<FIJI:DATA><FIJI:X>2008<FIJI:Y>8<FIJI:NAME>800x600</ROOT>

any idea?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.