2010-10-14 66 views
6

我經歷了Spring MVC和我嘗試條紋決定是否要嘗試一下新的項目。條紋MVC模型數據

在Spring MVC我會做好準備的模型數據,並通過將其添加到我的控制器創建的ModelAndView實例的地圖把它傳遞給視圖。我無法爲Stripes找到相同的結果。

這似乎是最接近的平行是有一個ActionBean中準備我的模型數據,並將其添加到HttpSession中。 ForwardRedirect用於加載視圖並從會話訪問數據。

有由條紋提供了一個前端控制器更好的支持,或者這是一個完全不同的設計原則比Spring MVC的? (即我必須調用使用EL檢索數據從視圖的方法,因爲一些例子做)

謝謝!

+0

我不喜歡加入模型數據到HttpSession的方法,因爲它應該被存放INT請求範圍對於大多數頁面請求,而不是會話範圍。這是我已經能夠找出條紋最接近的等效支持。 – idle 2010-10-14 07:12:24

+0

ActionBean既是控制器(Action)又是父模型數據(Bean)。 ;) – yihtserns 2010-10-16 16:24:09

回答

5

的條狀典型的MVC設計看起來像類似下面的代碼。

JPA實體自動加載由Stripersist提供的Stripes攔截器(但如果您願意,也可以輕鬆實現on your own)。因此在這個例子中,請求http://your.app/show-order-12.html將從數據庫中加載一個ID爲12的訂單,並將其顯示在頁面上。

控制器(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」); 
    } 
} 

視圖(order.jsp):

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

模型(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有一個真正優秀的小書上的條紋,覆蓋了所有這些事情(!):

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

+0

太棒了!感謝您的解釋和完美的例子。 :) – idle 2010-10-15 16:05:16

1

好吧我已經想通了。添加到HttpServletRequest(從上下文獲取)的屬性在接收ForwardRedirect的頁面中可用。在context.getRequest()。setAttribute(「attr1」,「request屬性1」)中提供的屬性可用於接收ForwardRedirect的頁面。 返回新的ForwardResolution(「/ WEB-INF/pages/hello.jsp」);

在hello.jsp $ {attr1} 可用...耶!

+1

這不是你典型的在Stripes中做MVC的方式...... – Kdeveloper 2010-10-14 23:30:17