通过属性封装list,传递给JSP文件
package Action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import dao.Impl.CustomerDAOImpl;
import Service.CustomerServiceImpl;
import VO.Customer;
public class ViewAllAction {
private List<Customer> list;
public List<Customer> getList(){
return list;
}
public String execute(){
CustomerServiceImpl cs = new CustomerServiceImpl();
cs.setDao(new CustomerDAOImpl());
list = cs.viewAll();
//List<Customer> list = cs.viewAll();
//ActionContext ctxt = ActionContext.getContext();
//ctxt.put("allcustomers", list);
return "success";
}
}
JSP页面使用JSTL对list进行迭代
<body>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
All Customers:<br>
<table width="200" border="1">
<tbody>
<tr>
<td> Custname</td>
<td> age</td>
<td>address </td>
</tr>
<c:forEach items="${requestScope.list}" var="c">
<tr>
<td>${c.custname}</td>
<td>${c.age}</td>
<td>${c.address}</td>
</tr>
</c:forEach>
</tbody>
</table><br>
</body>
Action属性封装Action配置参数
在strtus.xml中配置参数
<action name="Test" class="Action.TestAction">
<result name="sucess">/testaction.jsp</result>
<param name="rate">1.0</param>
</action>
要想在Action中使用该参数,就需要再Action中为参数提供getXXX和setXXX方法,TestAction.java
public class TestAction
{
private Integer count;
private Double rate;
public Double getRate(){
return rate;
}
public void setRate(Double rate){
this.rate=rate;
}
public Integer getCount(){
return count;
}
public void setRate(Integer count){
this.count=count;
}
public String execute(){
count++;
System.out.println("rate is :"+rate);
return "success";
}
}
Action类是多实例的,每次访问Action,都将实例化一个Action对象,所以Action类的属性是线程安全的。