Hobione's Weblog

Living & Breathing in Web 2.0 Era

Another day with DWR 2.0 – error to Execute

Since demo application worked, I wanted to leverage my DWR knowledge in my existing servlet application. I have a shopping cart application that I wanted to add ajax callback for a field and I am having some serious issues.

Marshalling issue Since ‘has-a’ relationship exist in ItemOrder.java, so I had to do marshalling on ItemOrder and CDTypeVO in dwr.xml. Here is the code for marshalling both beans.

<allow>
<convert converter=”bean” match=”cdorder.domain.ItemOrder” />
<create creator=”new” javascript=”ItemOrder”>
<param name=”class” value=”cdorder.domain.ItemOrder”/>
</create>

<convert converter=”bean” match=”cdorder.domain.CDTypeVO” />
<create creator=”new” javascript=”CDTypeVO” >
<param name=”class” value=”cdorder.domain.CDTypeVO”/>
</create>
</allow>

This resolved all red error, but IteamOrder bean still not functioning properly. Every time I click those Execute button, I am getting a nasty pop up Error message. Not sure, what am I missing.

DWR Pop up Error Here is the architecture description: I have a form which has ‘Add’ buttons. When an Add button get clicked, it submits a form and request goes to OrderPageServlet.java and here what it does as follows:

  1. Get a shopping cart from session variable
  2. If shopping cart is null, instantiate a empty shopping cart and put it in the session

i. cart = (ShoppingCart)session.getAttribute(“shoppingCart”);

ii. // New visitors get a fresh shopping cart.

iii. // Previous visitors keep using their existing cart.

iv. if (cart == null) {

v. cart = new ShoppingCart();

vi. session.setAttribute(“shoppingCart”, cart);

vii. }

  1. Now, based on CD Id that has been passed, add item to the cart in Servlet

i. cart.addItem(cdID,type);

  1. ShoppingCart has an ArrayList that keep tracks all order
  2. Add item to the list in the ShopptingCart.java

i. private ArrayList itemsOrdered = new ArrayList();

ii. ItemOrder newOrder = new ItemOrder(cdTypeDAO.getSingleCdType(cdID,false));

iii. itemsOrdered.add(newOrder);

  1. Here is the code snippet of ItemOrder.java

public class ItemOrder {

private CDTypeVO item;

private int numItems;

private String processedDate;

private String processedBy;

public ItemOrder(CDTypeVO item) {

setItem(item);

setNumItems(1);

}

public CDTypeVO getItem() {

return (item);

}

//@Override

// public String toString(){

// return item.toString() + ” ” + getNumItems() + ” cdID ” + getCdId() + ” cdType ” + getCDType() + ” description: ” + getDescription() + “\n”;

// }

public void setItem(CDTypeVO item) {

this.item = item;

}

public int getCdId() {

return (getItem().getCdId());

}

public String getCDType() {

return (getItem().getCdType());

}

public String getDescription() {

return (getItem().getDescription());

}

//quantity

public int getNumItems() {

return numItems;

}

public void setNumItems(int n) {

this.numItems = n;

}

public void incrementNumItems() {

setNumItems(getNumItems() + 1);

}

public void cancelOrder() {

setNumItems(0);

}

public String getProcessedDate() {

return processedDate;

}

public void setProcessedDate(String processedDate) {

this.processedDate = processedDate;

}

public String getProcessedBy() {

return processedBy;

}

public void setProcessedBy(String processedBy) {

this.processedBy = processedBy;

}

public String sayHello(String name) {

return “Hello, ” + name;

}

}

  1. Here is the code snippet for CDTypeVO.java Bean\
  2. /*
  3. * CDType.java
  4. *
  5. * Created on December 6, 2004, 10:02 AM
  6. */
  7. package cdorder.domain;
  8. /**
  9. *
  10. * @author hhaque
  11. * VO = Value Object
  12. */
  13. public class CDTypeVO implements java.io.Serializable{
  14. private int cdId;
  15. private String cdType;
  16. private String description;
  17. private String date;
  18. /** Creates a new instance of CDType */
  19. public CDTypeVO() {
  20. }
  21. // public String toString(){
  22. //
  23. // return “cdID = ” + getCdId() +
  24. // ” cdType = ” + getCdType() +
  25. // ” description = ” +getDescription() +” date = “+getDate() + “\n”;
  26. // }
  27. public int getCdId() {
  28. return cdId;
  29. }
  30. public void setCdId(int cdId) {
  31. this.cdId = cdId;
  32. }
  33. public String getCdType(){
  34. return cdType;
  35. }
  36. public void setCdType(String cdType){
  37. this.cdType = cdType;
  38. }
  39. public String getDescription(){
  40. return description;
  41. }
  42. public void setDescription(String description){
  43. this.description = description;
  44. }
  45. public String getDate(){
  46. return date;
  47. }
  48. public void setDate(String date){
  49. this.date = date;
  50. }
  51. }

When I do this

<script>

var numItemValue = dwr.util.getValue(‘numItems’);

ItemOrder.sayHello(numItemsValue, loadDataCallback); It does not do anything.

Here is my call back function:

function loadDataCallback(returnValue) {

dwr.util.setValue(‘demoReply’, returnValue);

} </script>

Advertisements

December 14, 2007 - Posted by | Ajax Libraries, DWR

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: