Action返回自定义类的List集合通过JSP中的Struct2标签显示
有时候,通过数据库查询出来的数据需要显示在页面上,但是Action中返回的只有List<Object>易于操作,所以需要在Jsp页面调用标签显示
不多说,上代码
package org.action;
import java.util.ArrayList;
import java.util.List;
import org.dao.ShowDao;
import org.vo.Goods;
import com.opensymphony.xwork2.ActionSupport;
public class ShowAction extends ActionSupport{
List<Goods> goods = new ArrayList<Goods>();
public List<Goods> getGoods() {
return goods;
}
public void setGoods(List<Goods> goods) {
this.goods = goods;
}
public String execute(){
Goods good = new Goods();
Goods good1 = new Goods();
Goods good2 = new Goods();
Goods good3 = new Goods();
goods.add(good);
goods.add(good1);
goods.add(good2);
goods.add(good3);
return SUCCESS;
}
}
java代码返回的是List<Object>对象然后需要显示在jsp页面
上代码
<table class="bordered">
<caption><h1>商品列表</h1></caption>
<thead>
<tr>
<th>#</th>
<th>名称</th>
<th>地址</th>
<th>简介</th>
<th>当前价格</th>
<th>原价</th>
</tr>
</thead>
<s:iterator value="goods" status="st" id="good">
<tr>
<td><s:property value="#good.getId()"/></td>
<td><s:property value="#good.getName()"/></td>
<td><s:property value="#good.getPath()"/></td>
<td><s:property value="#good.getIntro()"/></td>
<td><s:property value="#good.getCost_price()"/></td>
<td><s:property value="#good.getPresent_price()"/></td>
</tr>
</s:iterator>
</table>
此上所用的
<s:iterator value="goods" status="st" id="good">
<s:property value="#good.getName()"/>
</s:iterator>
只可以说出到页面上
如果你想这样输出Action中的数据,
<s:textfield name="good.name" label="名称" value="<s:property value="#good.getName()"/>"/>
抱歉不可以的
<s:textfield name="good.name" label="名称" value="%{#good.getName()}"/>
换成OGNL就可以了具体 为什么这样写我就不知道了