Struts2 Action属性

通过属性封装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类的属性是线程安全的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值