1.2 知识点 1.2.1 struts2的servlet的api的访问 1.2.1.1 方式一 : 通过actioncontext实现 页面: <h1>servlet的api的访问方式一:解耦合的方式</h1> <form action="${ pagecontext.request.contextpath }/requestdemo1.action" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"><br/> <input type="submit" value="提交"> </form> 编写action /** * servlet的api的访问方式一:解耦合的方式 * * 只能用于接收参数 和 操作域中的数据 * @author jt */ public class requestdemo1action extends actionsupport{ @override public string execute() throws exception { // 接收数据: /** * 在actioncontext中提供了一些方法:操作域对象的数据 * * map<string,object> getparameters(); * * map<string,object> getsession(); * * map<string,object> getapplication(); */ // 接收参数: actioncontext actioncontext = actioncontext.getcontext(); map<string,object> map = actioncontext.getparameters(); for (string key : map.keyset()) { string[] arrs = (string[]) map.get(key); system.out.println(key+" "+arrays.tostring(arrs)); } // 向request域中存值: actioncontext.put("reqname", "req如花"); // 向session域中存值: actioncontext.getsession().put("sessname", "sess凤姐"); // 向application域中存值: actioncontext.getapplication().put("appname","app石榴"); return success; } } 1.2.1.2 方式二: 实现特定接口的方式实现: 在struts中提供了一些接口 : servletrequestaware,servletresponseaware,servletcontextaware; 页面: <h1>servlet的api的访问方式二:实现特定接口的方式</h1> <form action="${ pagecontext.request.contextpath }/requestdemo2.action" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"><br/> <input type="submit" value="提交"> </form> 编写action: /** * servlet的api的访问方式二: * @author jt * */ public class requestdemo2action extends actionsupport implements servletrequestaware,servletcontextaware{ private httpservletrequest request; private servletcontext application; @override public string execute() throws exception { // 接收数据:使用request对象。 map<string, string[]> parametermap = request.getparametermap(); for (string key : parametermap.keyset()) { string[] arrs = parametermap.get(key); system.out.println(key+" "+arrays.tostring(arrs)); } // 向域中保存数据; // 向request域中保存数据 request.setattribute("reqname", "r郝三"); // 向session域中保存数据 request.getsession().setattribute("sessname", "s李四"); // 向application域中保存数据 application.setattribute("appname", "a王五"); return none; } @override public void setservletrequest(httpservletrequest request) { this.request = request; } @override public void setservletcontext(servletcontext application) { this.application = application; } } 1.2.1.3方式 : 通过servletactioncontext对象的静态方法实现 : 页面: <h1>servlet的api的访问方式三:通过servletactioncontext对象静态方法获取</h1> <form action="${ pagecontext.request.contextpath }/requestdemo3.action" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"><br/> <input type="submit" value="提交"> </form> 编写action: /** * servlet的api访问方式三:通过servletactioncontext的静态方法访问 * @author jt * */ public class requestdemo3action extends actionsupport { public string execute() throws exception { //接收参数: /* servletactioncontext 在struts2的api中的 : httpservletrequest getrequest(); httpservletresponse getresponse(); servletcontext getservletcontext(); */ httpservletrequest request = servletactioncontext.getrequest(); map<string,string[]> parametermap = request.getparametermap(); for(string key : parametermap.get()) { string[] value = parametermap.get(key); system.out.println(key + " " + arrays.tostring(value)); } //向域中存值 : request.setattribute("reqname" , "用request向域中存数据"); request.getsession.setattribute("session","向session域中存数据"); servletactioncontext.getservletcontext().setattribute("servletcontext","向servletcontext域中存储数据"); return super.execute(); } } 1.2.2.1 :结果页面的分类 : 全局结果页面 : 针对一个包下所有的action都生效的一个页面. <!--全局结果页面--> <global-results> <result>/jsp/1.jsp</result> </global-results> 局部结果页面 : 针对某一个action生效的一个页面 : <action name="requestdemo1" class="com.baidu.struts2.demo1.actiondemo1"> <result>/jsp/1.jsp</result> </action> 1.2.2.2 配置结果页面 : <result>标签配置: name : 逻辑视图的名称.默认值是success. type : dispatcher : 默认值,转发.(转发到jsp的页面) redirect : 重定向.(重定向到jsp的页面),被跳转的页面中丢失传递的信息. chain : 转发到另一个action. redirectaction : 重定向到另一个action.跳转的页面中丢失传递的信息 stream : 文件的下载 1.2.2.3 struts2的多例性 : 原来的servlet是单例存在,多次请求,请求都是同一个servlet的实例.struts2中action是多实例的.有异常请求就会有一个action的实例. 现在可以在action中定义成员属性了. 大部分我们会优先使用模型驱动的方式,因为struts2内部有很多结果是围绕模型驱动设计的。但如果页面向多个对象中封装,那么就需要使用属性驱动的方式二了。 这些都是像某个对象中封装数据, 1.2.3 struts2的数据封装 : 1.2.3.1 struts2中的数据封装-属性驱动 : 提供对应属性的set方法的方式 : 页面: <h3>数据封装的方式一:提供属性的set方法的方式</h3> <form action="${ pagecontext.request.contextpath }/employee1action.action" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"/><br/> 性别:<input type="text" name="sex"/><br/> 工资:<input type="text" name="salary"/><br/> <input type="submit" value="提交"/> </form> 编写action: /** * 数据封装的方式一:提供set方法的方式 * @author jt * */ public class employee1action extends actionsupport{ private string name; private integer age; private string sex; private double salary; public void setname(string name) { this.name = name; } public void setage(integer age) { this.age = age; } public void setsex(string sex) { this.sex = sex; } public void setsalary(double salary) { this.salary = salary; } @override public string execute() throws exception { system.out.println("员工姓名:"+name); system.out.println("员工年龄:"+age); system.out.println("员工性别:"+sex); system.out.println("员工工资:"+salary); // 手动封装数据: employee employee = new employee(); employee.setname(name); employee.setage(age); employee.setsex(sex); employee.setsalary(salary); return none; } } 这种方式不是特别常用,因为需要手动封装数据,而且如果属性很多,提供很多的set方法,导致action类易读性变差 页面中提供表达式的方式: 页面: <h3>数据封装的方式二:页面提供ognl表达式的写法</h3> <form action="${ pagecontext.request.contextpath }/employee2action.action" method="post"> 姓名:<input type="text" name="employee.name"/><br/> 年龄:<input type="text" name="employee.age"/><br/> 性别:<input type="text" name="employee.sex"/><br/> 工资:<input type="text" name="employee.salary"/><br/> <input type="submit" value="提交"/> </form> 编写action: /** * 数据封装的方式二:页面提供表达式的方式 * @author jt * */ public class employee2action extends actionsupport{ // 提供成员的属性employee,必须提供属性的get和set方法 private employee employee; public employee getemployee() { return employee; } public void setemployee(employee employee) { this.employee = employee; } @override public string execute() throws exception { system.out.println(employee); return none; } } 1.2.3.2 struts2 中的数据封装-模型驱动 : 采用模型驱动完成数据封装(推荐) 页面: <h3>数据封装的方式三:采用模型驱动的方式</h3> <form action="${ pagecontext.request.contextpath }/employee3action.action" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"/><br/> 性别:<input type="text" name="sex"/><br/> 工资:<input type="text" name="salary"/><br/> <input type="submit" value="提交"/> </form> 编写action: /** * 数据封装的方式三:采用模型驱动的方式 * @author jt * */ public class employee3action extends actionsupport implements modeldriven<employee>{ // 模型驱动使用的对象:必须手动构建对象的实例。 private employee employee = new employee(); @override public employee getmodel() { return employee; } @override public string execute() throws exception { system.out.println(employee); return none; } } ***** 模型驱动只能向一个实体中封装数据,如果需要向多个实体封装数据优先采用第二种方式。 1.2.4 struts2 的复杂数据的封装 : 1.2.4.1 封装到list集合 : 页面: <form action="${ pagecontext.request.contextpath }/product1action.action" method="post"> 商品名称:<input type="text" name="list[0].name"/><br/> 商品价格:<input type="text" name="list[0].price"/><br/> 商品名称:<input type="text" name="list[1].name"/><br/> 商品价格:<input type="text" name="list[1].price"/><br/> 商品名称:<input type="text" name="list[2].name"/><br/> 商品价格:<input type="text" name="list[2].price"/><br/> <input type="submit" value="批量导入"/> </form> action: /** * 封装复杂的数据到list集合中。 * @author jt * */ public class product1action extends actionsupport{ private list<product> list; public list<product> getlist() { return list; } public void setlist(list<product> list) { this.list = list; } @override public string execute() throws exception { for (product product : list) { system.out.println(product); } return none; } } 1.2.4.2 封装到map集合 页面: <h1>批量插入商品:封装到map集合</h1> <form action="${ pagecontext.request.contextpath }/product2action.action" method="post"> 商品名称:<input type="text" name="map['one'].name"/><br/> 商品价格:<input type="text" name="map['one'].price"/><br/> 商品名称:<input type="text" name="map['two'].name"/><br/> 商品价格:<input type="text" name="map['two'].price"/><br/> 商品名称:<input type="text" name="map['three'].name"/><br/> 商品价格:<input type="text" name="map['three'].price"/><br/> <input type="submit" value="批量导入"/> </form> action: /** * 复杂类型数据封装:封装到map集合 * @author jt * */ public class product2action extends actionsupport { private map<string,product> map; public map<string, product> getmap() { return map; } public void setmap(map<string, product> map) { this.map = map; } @override public string execute() throws exception { for (string key : map.keyset()) { product product = map.get(key); system.out.println(key+" "+product); } return none; } } 随堂笔记 : 1 strtus2对servlet api的访问 1 使用servletactioncontext类的静态方法(重要) 2 使用actioncontext对象的方法(理解)往域中存储数据,到页面展示(ps:这个对象获取不到域对象,只能操作域中的数据) 静态方法getcontext()获取actioncontext对象 特点: 只能接受页面的数据,以及对域中数据的进行操作 获取不response对象做不了响应,也获取不到域对象 使用actioncontext对象的方法(理解) // getcontext() --获取actioncontext对象 // map<string,object> ----- getparameters() 获取页面的所有数据 // map<string,object> ------getsession() 获取session域中的数据 // map<string,object> -------getapplication() 获取servletcontext域中的数据 3 实现特定的接口来获取的方法(了解) 证明action是单实例还是多实例? servlet是单实例 成员属性有线程危机 action是多实例 成员属性没有线程危机 2 结果页面的逻辑视图配置 分类: 全局配置 针对一个包下所有的action都生效的一个页面 局部配置 针对某一个action生效的一个页面 注意:如果全局配置和局部配置都配置了,局部配置的优先级大于全局配置 result标签: 对返回的逻辑视图进行匹配 name: 逻辑视图的名称。默认值是success。 type: dispatcher 默认 请求转发(用于转发到jsp页面) redirect 重定向(用于重定向到jsp页面) chain 请求转发(用于转发到action) redirectaction 重定向(用于重定向到action) stream 用于下载的(crm3天案例) 3 struts2对页面数据的封装(模型驱动,属性驱动) 1 属性驱动---set属性方式(例如:分页玩法) (重点) 条件: 1 需要在action的成员位置提供属性,并且要有set()方法 2 属性驱动---页面表达式的方式 (理解) 条件: 1 需要在action的成员位置上申明javabean对象,且提供get/set方法 例如: private user user; get()/set() 2 在页面上的参数name属性需要: name=javabean对象.属性 例如: user.username; uiser.age; 3 模型驱动(单一的对象) (掌握) 条件: 1 让action实现一个接口: modeldriven 2 需要在action的成员位置上申明javabean对象,并且这个对象得实例化 private user user=new user; 3 需要 modeldriven 这个接口中的getmodel()方法 4 需要在getmodel()方法里面将我们的javabean对象以返回值的形式返回回去 主页:页面还是正常的写法 4 批量方式(针对的是多个对象数据的封装) 条件: 需要在action中提供集合,且提供get()/set()方法 list封装: 在action中集合名称: ll 在页面需要: ll[0].username ll[0].age ll[1].usrname ll[1].age map封装: 在action中集合名称: mm 在页面需要: mm[键名].属性 总结: 1 struts2怎么样获取到servlet的api(request,response,session,servletcontext,以及接受页面的数据) 1 通过servletactioncontext类的静态方法 2 通过actioncontext对象的方法(理解) getactioncontext()--对象的获取 注意:只能接受操作页面参数,以及操作域中的数据 不能获取到域对象,也不能做响应(response对象也获取不到) 3 通过特定的接口来实现 servletrequestaware,servletresponseaware,servletcontextaware,sessionaware 2 结果逻辑视图的配置 全局配置 针对的是一个包下的所有action 局部配置 针对的是指定的action 2个都存在的情况下优先级:局部>全局 result标签: name: 逻辑视图的名称。默认值是success。 type: dispatcher :默认值,请求转发。(请求转发到jsp的页面) redirect :重定向。(重定向到jsp的页面) chain :转发到另一个action。 redirectaction:重定向到另一个action。 stream :文件的下载。 3 struts2对页面数据封装的方式: 4种 ps:action多实例的,每访问一次就创建一个action对象,所以它成员位置的属性不存在线程危机 1 属性驱动---set方式 条件: action提供相应的成员属性,必须得有set方法 2 属性驱动---页面表达式的方式 条件: 1 需要在action的成员位置上申明javabean对象,且提供get/set方法 2 在页面上的参数name属性需要: name=javabean对象.属性 例如: user.username; uiser.age; 3 模型驱动 条件: 1 让action实现一个接口: modeldriven 2 需要在action的成员位置上申明javabean对象,并且这个对象得实例化 private user user=new user; 3 需要 modeldriven 这个接口中的getmodel()方法 4 需要在getmodel()方法里面将我们的javabean对象以返回值的形式返回回去 主页:页面还是正常的写法 4 批量封装 条件: 提供集合且提供set/get方法 list: 在action中: ll 在页面: ll[索引].属性 map: 在action中: mm 在页面: mm['键名'].属性
如对本文有疑问, 点击进行留言回复!!
网友评论