模型驱动是用于从界面向后台或者从后台向界面数据交互的,一般传递的是对象类型。
Action中定义一个类实现模型驱动接口
public class OrderAction extends ActionSupport implements ModelDriven<Order> {
private Order order=new Order();
public Order getModel(){
return order;
}
}
order类
package cn.itcast.shop.order.vo;
import java.util.Date;
import cn.itcast.shop.user.vo.User;
public class Order {
private Integer oid;
private Double total;
private Date ordertime;
private Integer state;
//订单所属用户
private User user;
//订单中所属的多个订单项
private Set<OrderItem> orderItems=new HashSet<OrderItem>();
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public Double getTotal() {
return total;
}
public void setTotal(Double total) {
this.total = total;
}
public Date getOrdertime() {
return ordertime;
}
public void setOrdertime(Date ordertime) {
this.ordertime = ordertime;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setOrderItems(Set<OrderItem> orderItems) {
this.orderItems = orderItems;
}
public Set<OrderItem> getOrderItems() {
return orderItems;
}
}
由Action跳转到的界面中就会获取到model对象的信息,Model对象直接存放在栈顶,然后通过struts标签回显到界面上,iterator用于遍历循环每一项。
<s:iterator var="orderItem" value="model.orderItems">
<tr>
<td width="60">
<input type="hidden" name="id" value="22"/>
<img src="${pageContext.request.contextPath}/<s:property value="#orderItem.product.image"/>"/>
</td>
<td>
<a target="_blank"><s:property value="#orderItem.product.pname"/></a>
</td>
<td>
<s:property value="#orderItem.product.shop_price"/>
</td>
<td class="quantity" width="60">
<s:property value="#orderItem.count"/>
<input type="text" name="count" value="1" maxlength="4" onpaste="return false;"/>
<div>
<span class="increase"> </span>
<span class="decrease"> </span>
</div>
</td>
<td width="140">
<span class="subtotal">¥<s:property value="#orderItem.subtotal"/></span>
</td>
</tr>
</s:iterator>