BlazeDSContactPortlet
, which is used for returning a JSP that includes Flex from doView()
method but once the Flex application is loaded it will directly communicate to MessageBrokerServlet
inside the portlet, which is disadvantage of using BlazeDS in portlet application, you wont have portlet context in any other callsYou can download the sample code for this article from here
The BlazeDS application is nothing but a Servlet that you can add to your portlet application along with set of jars and you should be able to use it for communicating to Flex
- First download the blazeds.war and copy all the jars from its WEB-INF/lib to your WEB-INF/lib folder
- Then add
HttpFlexSession
listener andMessageBrokerServlet
servlet to your web.xml like this
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping> - The copy flex folder from the blazeds.war to your WEB-INF folder and change services-config.xml file so that value of my-amf channel points to the
MessageBrokerServlet
in your environment.
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://localhost:10040/wpcert/PA_BlazeDSContact/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
The value of url will change based on your environment, in my case base URL of the server is http://localhost:10040 and the web application that has theMessageBrokerServlet
is available at http://localhost:10040/wpcert/PA_BlazeDSContact, so i add/messagebroker/amf
to it and that becomes value ofmy-amf
channel url - I am more interested in using BlazeDS to make calls to Java Objects in my portlet application from my Flex code, so first i declare the Java Class that should be invoked from my Flex class by adding this code to my remoting-config.xml file like this
<destination id="contactdao">
<properties>
<source>com.webspherenotes.flex.dao.ContactDAOImpl</source>
</properties>
</destination>
Now i can call methods ofcom.webspherenotes.flex.dao.ContactDAOImpl
Java Class directly from my Flex code - Now inside my flex code i can add this code to call methods of the
ContactDAOImpl
[Bindable]
public var contactList:ArrayCollection;
public var contactDAORO:RemoteObject;
public function getContactList():void {
contactDAORO = new RemoteObject();
contactDAORO.destination = "contactdao";
contactDAORO.endpoint ="http://localhost:10040/wpcert/PA_BlazeDSContact/messagebroker/amf"
contactDAORO.addEventListener("result",getContactListHandler);
contactDAORO.addEventListener("fault",faultHandler);
contactDAORO.getContactList();
}
public function faultHandler (event:FaultEvent):void {
Alert.show(event.fault.faultString, 'Error');
}
private function getContactListHandler(event:ResultEvent):void{
contactList = event.result as ArrayCollection;
dgEmployees.dataProvider = contactList;
}
<mx:DataGrid x="10" y="174" width="460" enabled="true" editable="false"
id="dgEmployees">
<mx:columns>
<mx:DataGridColumn headerText="First Name" dataField="firstName"/>
<mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
<mx:DataGridColumn headerText="Email" dataField="email"/>
</mx:columns>
</mx:DataGrid>
My Flex code hasdgEmployees
asDataGrid
, that displays list of contacts in spread sheet format.
I am callinggetContactList()
, flex method on the application load, this method is creating object ofRemoteObject
and pointing to URL whereMessageBrokerServlet
in my application is listening, then i am setting value ofcontactDAORO.destination
tocontactdao
, when i do thatMessageBrokerServlet
, will take value of destination and find out the java class from that is mapped to contactdao by reading remoting-config.xml, in our case it is pointing tocom.webspherenotes.flex.dao.ContactDAOImpl
, then i am settinggetContactListHandler
, as flex method which should be used to handle result of this call andfaultHandler
method for handling error cases in this flex call.
ThecontactDAORO.getContactList()
means i want to call thegetContactList()
method ofContactDAOImpl
class, this method returns ArrayList of Contact objects, on the flex side i am converting thatArrayList
toArrayCollection
and i have a Contact flex class that is mapped to the Contact java class. Inside thegetContactList()
method i am setting the list returned by Java code as dataprovider for the datagrid - This is how my Action Script Contact class looks like
package com.webspherenotes.flex
{
[Bindable]
[RemoteClass(alias="com.webspherenotes.flex.dao.Contact")]
public class Contact
{
private var _firstName:String;
private var _lastName:String;
private var _email:String;
public function Contact()
{
}
public function get email():String
{
return _email;
}
public function set email(value:String):void
{
_email = value;
}
public function get lastName():String
{
return _lastName;
}
public function set lastName(value:String):void
{
_lastName = value;
}
public function get firstName():String
{
return _firstName;
}
public function set firstName(value:String):void
{
_firstName = value;
}
}
}
Thanks for info....
ReplyDeleteSEO Company in Bangalore