WCS AJAX Framework
WCS out of the box supports AJAX controllers which is essentially the Struts extensions. The starter stores use the WCS DOJO extension for client side Web2.0 capabilities.
In this blog I wanted to demonstrate the WCS server side AJAX capabilities, although all of the sample stores extensively make use of DOJO and it's extensions, the server side AJAX controllers are really independent of the technology / AJAX library used on the client, this means you can use JQuery, YUI or any of your favorite AJAX library.
Before we get started with the server side coding, we need to understand how exactly WCS server side AJAX components work.
WCS AJAX Framework
WCS Struts framework provides an important class named "AjaxAction" which is an extension of Action class.
This class file is packaged in Enablement-StrutsBaseLogic.jar
AjaxAction class which extends the Action class.
You will define the controller the regular way you create any custom controller.
i.e
public class MyControllerCmdImpl extends ControllerCmdImpl implements MyControllerCmd
AJAX Controllers written using AjaxAction class will automatically call AjaxActionResponse.jsp as successview and AjaxActionErrorResponse.jsp as errorview, these two VIEW components will compose the response data in JSON format and send it back to client side.
1. For the sake of this example we will invoke a Controller in which we will set two parameters in response object.
In my performExecute method I set two parameters in the response object as follows.
TypedProperty rspProp = new TypedProperty();
rspProp.put("message1", "Message 1 Data");
rspProp.put("message1", "Message 2 Data");
3. Make an entry in Struts Config to define this as an AJAX controller
As you can see the only difference is in the type parameter, for a regular controller you would define this as type="com.ibm.commerce.struts.BaseAction"
<action parameter="com.ibm.commerce.sample.commands.MyControllerCmd" path="/AJAXController" type="com.ibm.commerce.struts.AjaxAction">
<set-property property="authenticate" value="10551:0"/>
<set-property property="https" value="10551:0"/>
</action>
4. Make an entry in cmdreg table as follows.
insert into CMDREG (STOREENT_ID, INTERFACENAME, DESCRIPTION, CLASSNAME, TARGET)
values (10551,'com.ibm.commerce.sample.commands.MyNewControllerCmd',
'AJAX test',
'com.ibm.commerce.sample.commands.MyNewControllerCmdImpl','Local');
5. Restart you server instance and hit the new controller URL, following message will be printed in the browser.
The data that get's printed is the JSON representation of all response parameters you set from your controller. in an ideal world some AJAX client code would read this JSON data and paint it on the UI.
/* { "langId": ["-1"], "message1": "Message 2 Data", "storeId": ["10551"] } */
Resources
WCS Best Practices in Web 2.0 stores
In this blog I wanted to demonstrate the WCS server side AJAX capabilities, although all of the sample stores extensively make use of DOJO and it's extensions, the server side AJAX controllers are really independent of the technology / AJAX library used on the client, this means you can use JQuery, YUI or any of your favorite AJAX library.
Before we get started with the server side coding, we need to understand how exactly WCS server side AJAX components work.
WCS AJAX Framework
WCS Struts framework provides an important class named "AjaxAction" which is an extension of Action class.
This class file is packaged in Enablement-StrutsBaseLogic.jar
AjaxAction class which extends the Action class.
You will define the controller the regular way you create any custom controller.
i.e
public class MyControllerCmdImpl extends ControllerCmdImpl implements MyControllerCmd
AJAX Controllers written using AjaxAction class will automatically call AjaxActionResponse.jsp as successview and AjaxActionErrorResponse.jsp as errorview, these two VIEW components will compose the response data in JSON format and send it back to client side.
1. For the sake of this example we will invoke a Controller in which we will set two parameters in response object.
In my performExecute method I set two parameters in the response object as follows.
TypedProperty rspProp = new TypedProperty();
rspProp.put("message1", "Message 1 Data");
rspProp.put("message1", "Message 2 Data");
3. Make an entry in Struts Config to define this as an AJAX controller
As you can see the only difference is in the type parameter, for a regular controller you would define this as type="com.ibm.commerce.struts.BaseAction"
<action parameter="com.ibm.commerce.sample.commands.MyControllerCmd" path="/AJAXController" type="com.ibm.commerce.struts.AjaxAction">
<set-property property="authenticate" value="10551:0"/>
<set-property property="https" value="10551:0"/>
</action>
4. Make an entry in cmdreg table as follows.
insert into CMDREG (STOREENT_ID, INTERFACENAME, DESCRIPTION, CLASSNAME, TARGET)
values (10551,'com.ibm.commerce.sample.commands.MyNewControllerCmd',
'AJAX test',
'com.ibm.commerce.sample.commands.MyNewControllerCmdImpl','Local');
5. Restart you server instance and hit the new controller URL, following message will be printed in the browser.
The data that get's printed is the JSON representation of all response parameters you set from your controller. in an ideal world some AJAX client code would read this JSON data and paint it on the UI.
/* { "langId": ["-1"], "message1": "Message 2 Data", "storeId": ["10551"] } */
Resources
WCS Best Practices in Web 2.0 stores
0 Response to "WCS AJAX Framework"
Post a Comment