S2: basic question on rest plugin
Hello All, I'm experimenting with the rest plugin (2.1.8) using the example struts2-rest-showcase as a starting point. I wanted to implement a simple search such that a URL like http://localhost:8080/struts2-rest-showcase-2.1.8/orders?amount=66 returned a list of order with the amount of 66. I approached this by adding an amount property to the controller and corresponding setter method and then modifying the controller's index method to use that value to constrain the results. Problem is that the amount never gets set either on the controller or on the model. Can someone please help me understand what's going on or suggest a better way to approach this problem? Thanks! --john - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: RE: S2: basic question on rest plugin
Thanks for your reply Martin, I think I now have it working using the attribute on the model. --john - Original Message - From: Martin Gainty mgai...@hotmail.com Date: Saturday, November 14, 2009 2:03 pm Subject: RE: S2: basic question on rest plugin //First we need to define the attributes and methods for each Entity to be added/removed //in this case it will be called Order package org.apache.struts2.rest.example; public class Order { String id; String clientName; int amount; //here is the attribute you were seeking BTW public Order() {} public Order(String id, String clientName, int amount) { super(); this.id = id; this.clientName = clientName; this.amount = amount; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public String getClientName() { return clientName; } public void setClientName(String clientName) { this.clientName = clientName; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + amount; result = prime * result + ((clientName == null) ? 0 : clientName.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Order other = (Order) obj; if (amount != other.amount) return false; if (clientName == null) { if (other.clientName != null) return false; } else if (!clientName.equals(other.clientName)) return false; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } //take a look at the service class which feeds the Controller here is on called OrdersService //whose purpose is to add to Orders or remove Orders via the Service method package org.apache.struts2.rest.example; import java.util.*; public class OrdersService { //assuming we are populating our orders thru a HashMap similar to this mechanism private static MapString,Order orders = new HashMapString,Order();private static int nextId = 6; static { orders.put(3, new Order(3, Bob, 33)); orders.put(4, new Order(4, Sarah, 44)); orders.put(5, new Order(5, Jim, 66)); } //get a single Order public Order get(String id) { return orders.get(id); } //return a List all Orders public ListOrder getAll() { return new ArrayListOrder(orders.values()); } //save a single Order public void save(Order order) { if (order.getId() == null) { order.setId(String.valueOf(nextId++)); } //put into HashMap orders.put(order.getId(), order); } //removes an Order public void remove(String id) { orders.remove(id); } } //we can configure our OrderService to interject necessary Orders into our Controller package org.apache.struts2.rest.example; import java.util.Collection; import org.apache.struts2.dispatcher.ServletActionRedirectResult; import org.apache.struts2.rest.DefaultHttpHeaders; import org.apache.struts2.rest.HttpHeaders; import org.apache.struts2.convention.annotation.Results; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Namespaces; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.InterceptorRef; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Validateable; import com.opensymphony.xwork2.ValidationAwareSupport; @Results({ @Result(name=success, type=redirectAction, params = {actionName , orders}) }) public class OrdersController extends ValidationAwareSupport implements ModelDrivenObject, Validateable{ private Order model = new Order(); //initial Order private String id; private CollectionOrder list; //getAll returns this List private OrdersService ordersService = new OrdersService(); //Service will acquire order // GET /orders/1 public HttpHeaders show() { return new DefaultHttpHeaders(show); } // GET /orders public HttpHeaders index() { //get the entire List of orders from ordersService list = ordersService.getAll(); return new DefaultHttpHeaders(index
RE: S2: basic question on rest plugin
..good news.. congratulrations! Martin __ Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen. Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni. Date: Sat, 14 Nov 2009 15:39:39 -0700 From: john.c.cartwri...@noaa.gov Subject: Re: RE: S2: basic question on rest plugin To: mgai...@hotmail.com CC: user@struts.apache.org Thanks for your reply Martin, I think I now have it working using the attribute on the model. --john - Original Message - From: Martin Gainty mgai...@hotmail.com Date: Saturday, November 14, 2009 2:03 pm Subject: RE: S2: basic question on rest plugin //First we need to define the attributes and methods for each Entity to be added/removed //in this case it will be called Order package org.apache.struts2.rest.example; public class Order { String id; String clientName; int amount; //here is the attribute you were seeking BTW public Order() {} public Order(String id, String clientName, int amount) { super(); this.id = id; this.clientName = clientName; this.amount = amount; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public String getClientName() { return clientName; } public void setClientName(String clientName) { this.clientName = clientName; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + amount; result = prime * result + ((clientName == null) ? 0 : clientName.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Order other = (Order) obj; if (amount != other.amount) return false; if (clientName == null) { if (other.clientName != null) return false; } else if (!clientName.equals(other.clientName)) return false; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } //take a look at the service class which feeds the Controller here is on called OrdersService //whose purpose is to add to Orders or remove Orders via the Service method package org.apache.struts2.rest.example; import java.util.*; public class OrdersService { //assuming we are populating our orders thru a HashMap similar to this mechanism private static MapString,Order orders = new HashMapString,Order();private static int nextId = 6; static { orders.put(3, new Order(3, Bob, 33)); orders.put(4, new Order(4, Sarah, 44)); orders.put(5, new Order(5, Jim, 66)); } //get a single Order public Order get(String id) { return orders.get(id); } //return a List all Orders public ListOrder getAll() { return new ArrayListOrder(orders.values()); } //save a single Order public void save(Order order) { if (order.getId() == null) { order.setId(String.valueOf(nextId++)); } //put into HashMap orders.put(order.getId(), order); } //removes an Order public void remove(String id) { orders.remove(id); } } //we can configure our OrderService to interject necessary Orders into our Controller package org.apache.struts2.rest.example; import java.util.Collection; import org.apache.struts2