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 Map<String,Order> orders = new 
> HashMap<String,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 List<Order> getAll() {
>        return new ArrayList<Order>(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 ModelDriven<Object>, Validateable{  
>    private Order model = new Order(); //initial Order
>    private String id;
>    private Collection<Order> 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")
>            .disableCaching();
>    }
>    
>    // GET /orders/1/edit
>    public String edit() {
>        return "edit";
>    }
> 
>    // GET /orders/new
>    public String editNew() {
>        model = new Order();
>        return "editNew";
>    }
> 
>    // GET /orders/1/deleteConfirm
>    public String deleteConfirm() {
>        return "deleteConfirm";
>    }
> 
>    // DELETE /orders/1
>    public String destroy() {
> //we use the ordersService for add/remove/save operations such as 
> what is seen here
>        ordersService.remove(id);
>        addActionMessage("Order removed successfully");
>        return "success";
>    }
> 
>    // POST /orders
>    public HttpHeaders create() {
> //we use the ordersService for add/remove/save operations such as 
> what is seen here
>        ordersService.save(model);
>        addActionMessage("New order created successfully");
>        return new DefaultHttpHeaders("success")
>            .setLocationId(model.getId());
>    }
> 
>    // PUT /orders/1
>    public String update() {
> //we use the ordersService for add/remove/save operations such as 
> what is seen here
>        ordersService.save(model);
>        addActionMessage("Order updated successfully");
>        return "success";
>    }
> 
>    public void validate() {
>        if (model.getClientName() == null || 
> model.getClientName().length() ==0) {
>            addFieldError("clientName", "The client name is empty");
>        }
>    }
> 
>    public void setId(String id) {
>        if (id != null) {
> //find the supplied id from HashMap we have saved previous orders
>            this.model = ordersService.get(id);
>        }
>        this.id = id;
>    }
>    
>    public Object getModel() {
>        return (list != null ? list : model);
>    }
> }
> 
>                                                           Controller
>                                                               |
>                                                               ?
>         -----------------------------------------------------------
> ----------------------
>         |                                          |               
>            |                                        |
>         ?                                          ?               
>           ?                                        ?
>        index                                   destroy             
>       Create                               Update
>         |                                          |               
>            |                                        |
>         ?                                          ?               
>           ?                                        ?
> ordersService.getAll() ordersService.remove(id) 
> ordersService.save(model); ordersService.save(model);
>         |                                          |               
>            |                                        |
>         ?                                          ?               
>           ?                                        ?
> ArrayList<Order>(orders.values()) orders.remove(id); 
> ordersService.save(model); order.setId(String.valueOf(nextId++));
> 
> 
> Apologies for the Bush-league diagram..will update with Pogotoshop 
> whenever the obama money makes its way up here
> 
> HTH
> Martin Gainty 
> ______________________________________________ 
> Local Agents and Eavesdroppers..do NOT alter/modify or disrupt this 
> Transmission.
> 
> 
> 
> > Date: Sat, 14 Nov 2009 12:23:00 -0700
> > From: john.c.cartwri...@noaa.gov
> > Subject: S2: basic question on rest plugin
> > To: user@struts.apache.org
> > 
> > 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
> > 
>                                               
> _________________________________________________________________
> Bing brings you maps, menus, and reviews organized in one place.
> http://www.bing.com/search?
q=restaurants&form=MFESRP&publ=WLHMTAG&crea=TEXT_MFESRP_Local_MapsMenu_Restur
ants_1x1

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to