Action类:
public class ok extends ActionSupport{
List<Orderitem> it = new ArrayList<Orderitem>();
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
try {
System.out.println(it.get(0).getGoods());
System.out.println(it.get(0).getAmount());
System.out.println(it.get(0).getGoods().getId());
} catch (Exception e) {
// TODO: handle exception
System.out.println("异常");
}
return super.execute();
}
public List<Orderitem> getIt() {
return it;
}
public void setIt(List<Orderitem> it) {
this.it = it;
}
}
orderitem 实体类
public class Orderitem implements java.io.Serializable {
// Fields
private Integer id;
private Goods goods=new Goods();
private Orders orders;
private Integer amount;
// Constructors
/** default constructor */
public Orderitem() {
}
/** full constructor */
public Orderitem(Goods goods, Orders orders, Integer amount) {
this.goods = goods;
this.orders = orders;
this.amount = amount;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Goods getGoods() {
return this.goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public Orders getOrders() {
return this.orders;
}
public void setOrders(Orders orders) {
this.orders = orders;
}
public Integer getAmount() {
return this.amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
}
goods 实体类
public class Goods implements java.io.Serializable {
// Fields
private Integer id;
private Double price;
private String name;
private String specification;
private String manufacturer;
private Set orderitems = new HashSet(0);
// Constructors
/** default constructor */
public Goods() {
}
/** full constructor */
public Goods(Double price, String name, String specification,
String manufacturer, Set orderitems) {
this.price = price;
this.name = name;
this.specification = specification;
this.manufacturer = manufacturer;
this.orderitems = orderitems;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSpecification() {
return this.specification;
}
public void setSpecification(String specification) {
this.specification = specification;
}
public String getManufacturer() {
return this.manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public Set getOrderitems() {
return this.orderitems;
}
public void setOrderitems(Set orderitems) {
this.orderitems = orderitems;
}
}
使用Struts2框架,在传参到Action中时,如何为对象的对象的属性赋值呢
我想在Action中为集合对象里的每个orderitem的goods属性的ID赋值
在页面中的name属性该怎么写呢
6 个解决方案
#1
1:<input type="text" name="orderItems[0].goods.id"/>
2:<input type="text" name="orderItems[1].goods.id"/>
3:<input type="text" name="orderItems[2].goods.id"/>
4:<input type="text" name="orderItems[3].goods.id"/>
5:<input type="text" name="orderItems[4].goods.id"/>
.........
private List<Orderitem> orderItems;
public List<Orderitem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<Orderitem> orderItems) {
this.orderItems = orderItems;
}
public String execute(){
System.out.println("集合大小:"+orderItems.size());
for (Orderitem orderitem :orderItems) {
System.out.println("---->"+orderitem.getGoods().getId());
}
return null;
}
#2
目前我的写法就是这样子的。。可是goods获取到的一直是null。。会不会是Struts版本的问题?我目前用的是2.3.31的
#3
我的也是2.3,打印出来没问题啊。。。。
#4
1:<input type="text" name="orderItems[0].goods.id"/>
2:<input type="text" name="orderItems[1].goods.id"/>
3:<input type="text" name="orderItems[2].goods.id"/>
4:<input type="text" name="orderItems[3].goods.id"/>
5:<input type="text" name="orderItems[4].goods.id"/>
.........
private List<Orderitem> orderItems;
public List<Orderitem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<Orderitem> orderItems) {
this.orderItems = orderItems;
}
public String execute(){
System.out.println("集合大小:"+orderItems.size());
for (Orderitem orderitem :orderItems) {
System.out.println("---->"+orderitem.getGoods().getId());
}
return null;
}
目前我的写法就是这样子的。。可是goods获取到的一直是null。。会不会是Struts版本的问题?我目前用的是2.3.31的
我的也是2.3,打印出来没问题啊。。。。
我好像解决了 是我页面的的goods 写成了good
#5
为什么不用el表达式呢?
一个一个的取不累吗?
一个一个的取不累吗?
#6
为什么不用el表达式呢?
一个一个的取不累吗?
对啊,为什么要一个一个取。。。
#1
1:<input type="text" name="orderItems[0].goods.id"/>
2:<input type="text" name="orderItems[1].goods.id"/>
3:<input type="text" name="orderItems[2].goods.id"/>
4:<input type="text" name="orderItems[3].goods.id"/>
5:<input type="text" name="orderItems[4].goods.id"/>
.........
private List<Orderitem> orderItems;
public List<Orderitem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<Orderitem> orderItems) {
this.orderItems = orderItems;
}
public String execute(){
System.out.println("集合大小:"+orderItems.size());
for (Orderitem orderitem :orderItems) {
System.out.println("---->"+orderitem.getGoods().getId());
}
return null;
}
#2
1:<input type="text" name="orderItems[0].goods.id"/>
2:<input type="text" name="orderItems[1].goods.id"/>
3:<input type="text" name="orderItems[2].goods.id"/>
4:<input type="text" name="orderItems[3].goods.id"/>
5:<input type="text" name="orderItems[4].goods.id"/>
.........
private List<Orderitem> orderItems;
public List<Orderitem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<Orderitem> orderItems) {
this.orderItems = orderItems;
}
public String execute(){
System.out.println("集合大小:"+orderItems.size());
for (Orderitem orderitem :orderItems) {
System.out.println("---->"+orderitem.getGoods().getId());
}
return null;
}
目前我的写法就是这样子的。。可是goods获取到的一直是null。。会不会是Struts版本的问题?我目前用的是2.3.31的
#3
1:<input type="text" name="orderItems[0].goods.id"/>
2:<input type="text" name="orderItems[1].goods.id"/>
3:<input type="text" name="orderItems[2].goods.id"/>
4:<input type="text" name="orderItems[3].goods.id"/>
5:<input type="text" name="orderItems[4].goods.id"/>
.........
private List<Orderitem> orderItems;
public List<Orderitem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<Orderitem> orderItems) {
this.orderItems = orderItems;
}
public String execute(){
System.out.println("集合大小:"+orderItems.size());
for (Orderitem orderitem :orderItems) {
System.out.println("---->"+orderitem.getGoods().getId());
}
return null;
}
目前我的写法就是这样子的。。可是goods获取到的一直是null。。会不会是Struts版本的问题?我目前用的是2.3.31的
我的也是2.3,打印出来没问题啊。。。。
#4
1:<input type="text" name="orderItems[0].goods.id"/>
2:<input type="text" name="orderItems[1].goods.id"/>
3:<input type="text" name="orderItems[2].goods.id"/>
4:<input type="text" name="orderItems[3].goods.id"/>
5:<input type="text" name="orderItems[4].goods.id"/>
.........
private List<Orderitem> orderItems;
public List<Orderitem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<Orderitem> orderItems) {
this.orderItems = orderItems;
}
public String execute(){
System.out.println("集合大小:"+orderItems.size());
for (Orderitem orderitem :orderItems) {
System.out.println("---->"+orderitem.getGoods().getId());
}
return null;
}
目前我的写法就是这样子的。。可是goods获取到的一直是null。。会不会是Struts版本的问题?我目前用的是2.3.31的
我的也是2.3,打印出来没问题啊。。。。
我好像解决了 是我页面的的goods 写成了good
#5
为什么不用el表达式呢?
一个一个的取不累吗?
一个一个的取不累吗?
#6
为什么不用el表达式呢?
一个一个的取不累吗?
对啊,为什么要一个一个取。。。