7.7OGNL表达式的综合应用.ppt
文本预览下载声明
7.7 OGNL表达式的综合应用 环境准备: 用户新建web项目ch07,导入struts 2类库,在web.xml文件中配置FilterDispatcher。 Step1:编写领域对象 Package org.sunxin.struts 2.ch07.mode1 ; Public class Address { Private sting country ; Private sting city ; Private sting street ; Public Address(sting country, sting city,sting street) { This. Country= country ; This. city = city ; This. street = street ; } Employee.java Package org.sunxin.struts 2.ch07.mode1 Import java.util.map Public class employee { Private sting name ; Private int age ; Private float salary ; Private Address address ; Private sting[] emails ; Public map sting, stingaccounts: Public employee(sting name, int age, float salary, Address address, sting[] emails, map sting, stingaccounts) { Super() ; T=name This.age=age This salary= salary Step2:编写OgnlAction 说明:OgnlAction类负责准备数据,他实现parameterAware、RequestAware 、SessionAware和ApplicationAware4个接口。框架通过parameterAware接口向action注入保存了请求参数的map对象。 OgnlAction类完整代码: Package org.sunxin.struts 2.ch07.action; import java.util.ArrayList ; import java.util.HashMap ; import java.util. List ; import java.util. Map; import erceptor. ApplicationAware; import erceptor. parameterAware ; import erceptor. RequestAware ; import erceptor. SessionAware ; import org.sunxin. Struts2.ch07.model.Address; import org.sunxin. Struts2.ch07.model.Employee; import com.opensymphony.xwork2.Action; public class OgnlAction implements Action, ParameterAware, RequestAware, SessionAware, ApplicationAware { private Map parameters; private Map request; private Map session; private Map application; private ListEmployee emps = new ArrayListEmployee(); /** * 初始化雇员信息,将Employee对象保存到List对象emps中。 */ public void initEmps() { Address address1 = new Address(中国, 北京, 海淀南路18号); Address address2 = new Address(中国, 上海, 南京西路20号); Address address3 = new Address(中国, 成都, 春熙路8号); String[] emails1 = new String[] { zhangsan@, zhangsan@163.com }; String[] emails2 = new String[] { lisi@, lisi@163.com }; String[] emails3 = new String[] { wangwu@,
显示全部