Search code examples
javastripes

Stripes MVC Model Data


I am experienced with Spring MVC and am trying out Stripes to decide whether to try it out for a new project.

In Spring MVC I would prepare model data and pass it to the view by adding it to a map in the ModelAndView instance created by my controller. I am having trouble finding the equivalent of this for Stripes.

It seems like the closest parallel is to have an ActionBean prepare my model data and add it to the HttpSession. A ForwardRedirect is used to load the view and the data is accessed from the session.

Is there better support for a front controller provided by Stripes, or is this a totally different design principle than Spring MVC? (ie I have to invoke methods from the view using EL to retrieve data, as some of the examples do)

Thanks!


Solution

  • A typical MVC design in Stripes would look like something like the code below.

    The JPA entity is automaticaly loaded by a Stripes interceptor provided by Stripersist (but this can also easily implemented on your own if you wish). Thus in this example, requesting http://your.app/show-order-12.html will load a order with id 12 from the database and will show it on the page.

    Controller (OrderAction.java):

    @UrlBinding("/show-order-{order=}.html")
    public class OrderAction implements ActionBean {
        private ActionBeanContext context;
        private Order order;
    
        public ActionBeanContext getContext() {
            return context;
        }
    
        public void setContext(ActionBeanContext context) {
            this.context = context;
        }
    
        public void setOrder(Order order) {
            this.order = order;
        }
    
        public String getOrder() {
            return order;
        }
    
        @DefaultHandler
        public Resolution view() {
            return new ForwardResolution(“/WEB-INF/jsp/order.jsp”);
        }
    }
    

    View (order.jsp):

    <html><body>
        Order id: ${actionBean.order.id}<br/>
        Order name: ${actionBean.order.name)<br/>
        Order total: ${actionBean.order.total)<br/>
    </body></html>
    

    Model (Order.java):

    @Entity
    public class Order implements Serializable {
        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
    
        private String name;
        private Integer total;
    
        public String getName() {
            return name;
        }
    
        public Integer getTotal() {
            return total;
        }   
    }
    

    BTW there is an really excellent short(!) book on Stripes that covers all these things:

    Stripes: ...and Java Web Development Is Fun Again