今日任务
   使用Struts2完成对客户的新增操作案例一: 使用Struts2完成对客户的新增的优化操作
1.1 案例需求
1.1.1   需求概述
CRM系统中客户信息管理模块功能包括:
新增客户信息
客户信息查询
修改客户信息
删除客户信息本功能要实现新增客户,页面如下:1.2 相关知识点
1.2.1   Struts2访问Servlet的API:
1.2.1.1 可以使用完全解耦合的方式.
public class RequestActionDemo1 extends ActionSupport{@Overridepublic String execute() throws Exception {// 接收表单的参数:// 使用的是Struts2中的一个对象ActionContext对象.ActionContext actionContext = ActionContext.getContext();// 接收参数:Map<String,Object> paramsMap = actionContext.getParameters();for (String key : paramsMap.keySet()) {String[] value = (String[]) paramsMap.get(key);System.out.println(key+"    "+value[0]);}// 向request中存入数据  request.setAttribute(String name,Object value);actionContext.put("requestName", "张三");// 向session中存入数据 request.getSession().setAttribute(String name,Object value);actionContext.getSession().put("sessionName", "李四");// 向application中存入数据 this.getServletContext().setAttribute(String name,Object value);actionContext.getApplication().put("applicationName", "王五");return SUCCESS;}
}
1.2.1.2 使用原生的Servlet的API
public class RequestActionDemo2 extends ActionSupport{@Overridepublic String execute() throws Exception {// 接收参数:HttpServletRequest req = ServletActionContext.getRequest();Map<String,String[]> map = req.getParameterMap();for (String key : map.keySet()) {String[] value = map.get(key);System.out.println(key+"   "+value[0]);}// 向域中存入数据:req.setAttribute("requestName", "张三");// 向session中存入数据:req.getSession().setAttribute("sessionName", "李四");// 向application中保存:ServletActionContext.getServletContext().setAttribute("applicationName", "王五");return SUCCESS;}
}
1.2.2   结果页面的配置:
1.2.2.1 全局结果页面
<global-results><result name="success">/demo3/demo2.jsp</result>
</global-results>
1.2.2.2 局部结果页面<action name="requestActionDemo2" class="cn.itcast.struts2.demo4.RequestActionDemo2"><result name="success">/demo3/demo3.jsp</result></action><result>标签:* name      :返回一个逻辑视图名称.* type      : 跳转的采用的方式.* dispatcher            :默认值,转发. 转发到一个JSP页面 * redirect          :重定向. 重定向到一个JSP页面* chain             :转发,转发到一个Action.* redirectAction        :重定向到另一个Action1.2.3   Struts的数据封装
实际开发的场景:页面提交参数,在Action中接收参数,需要进行数据的封装,封装到JavaBean中,将JavaBean传递给业务层.
1.2.3.1 属性驱动
【一、 编写属性的set方法的方式进行参数的封装】使用比较少.
页面:
<h1>Struts2的属性驱动中:set方法的方式</h1>
<form action="${ pageContext.request.contextPath }/strutsDemo1.action" method="post">名称:<input type="text" name="name"><br/>年龄:<input type="text" name="age"><br/>生日:<input type="text" name="birthday"><br/><input type="submit" value="提交">
</form>Action:
public class StrutsDemo1 extends ActionSupport{// 接收参数:private String name;private Integer age;private Date birthday;public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}public void setBirthday(Date birthday) {this.birthday = birthday;}
…
}***** 这种方式还需要手动封装对象。
【二、 页面中使用表达式的方式进行参数的封装】
页面:
<h1>Struts2的属性驱动中:OGNL表达式的方式</h1>
<form action="${ pageContext.request.contextPath }/strutsDemo2.action" method="post">名称:<input type="text" name="user.name"><br/>年龄:<input type="text" name="user.age"><br/>生日:<input type="text" name="user.birthday"><br/><input type="submit" value="提交">
</form>Action:
public class StrutsDemo2 extends ActionSupport{private User user;// 必须提供get方法.public User getUser() { return user;}public void setUser(User user) {this.user = user;}
…
}
1.2.3.2 模型驱动
【三、 使用模型驱动的方式进行参数的封装】(优先)
页面:
<h1>Struts2的模型驱动驱动中:模型驱动的方式</h1>
<form action="${ pageContext.request.contextPath }/strutsDemo3.action" method="post">名称:<input type="text" name="name"><br/>年龄:<input type="text" name="age"><br/>生日:<input type="text" name="birthday"><br/><input type="submit" value="提交">
</form>Action:
public class StrutsDemo3 extends ActionSupport implements ModelDriven<User>{// 模型驱动使用的对象.private User user = new User();// 必须手动new@Overridepublic User getModel() {return user;}
…
}
1.2.4   Struts2中封装复杂类型的数据:
1.2.4.1 封装到List集合中:
页面:
<form action="${ pageContext.request.contextPath }/strutsDemo4.action" method="post">名称:<input type="text" name="list[0].name"><br/>年龄:<input type="text" name="list[0].age"><br/>生日:<input type="text" name="list[0].birthday"><br/>名称:<input type="text" name="list[1].name"><br/>年龄:<input type="text" name="list[1].age"><br/>生日:<input type="text" name="list[1].birthday"><br/><input type="submit" value="提交">
</form>Action:
public class StrutsDemo4 extends ActionSupport{private List<User> list;public List<User> getList() {return list;}public void setList(List<User> list) {this.list = list;}@Overridepublic String execute() throws Exception {for (User user : list) {System.out.println(user);}return NONE;}
}
1.2.4.2 封装数据到Map集合:
页面:
<h1>批量插入用户:封装到Map集合</h1>
<form action="${ pageContext.request.contextPath }/strutsDemo5.action" method="post">名称:<input type="text" name="map['one'].name"><br/>年龄:<input type="text" name="map['one'].age"><br/>生日:<input type="text" name="map['one'].birthday"><br/>名称:<input type="text" name="map['two'].name"><br/>年龄:<input type="text" name="map['two'].age"><br/>生日:<input type="text" name="map['two'].birthday"><br/><input type="submit" value="提交">
</form>Action:
public class StrutsDemo5 extends ActionSupport {private Map<String,User> map;public Map<String, User> getMap() {return map;}public void setMap(Map<String, User> map) {this.map = map;}@Overridepublic String execute() throws Exception {for (String key : map.keySet()) {User user = map.get(key);System.out.println(key+"    "+user);}return NONE;}
}
1.3 案例代码:
1.3.1   环境准备
1.3.2   代码实现
1.4 总结:
1.4.1   Struts2的拦截器:
1.4.1.1 拦截器的概述
拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
谈到拦截器,还有一个词大家应该知道——拦截器链 (Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。
1.4.1.2 拦截器的实现原理:
大部分时候,拦截器方法都是通过代理的方式来调用的。Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器。
Struts2拦截器是可插拔的,拦截器是AOP的一种实现。Struts2拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用。
1.4.1.3 Struts2的执行流程:1.4.1.4 自定义拦截器及配置:
第一步: 自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。
第二步:在strutx.xml中注册上一步中定义的拦截器。
第三步:在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。

Struts2(2)相关推荐

  1. struts2 与 sping 整合 控制器中 service注入的问题

    以个人见解认为struts1 与spring整合的时候按照习惯,我们会把 action 控制器直接配置到sping中去: eg : 这里以使用元注解方式实现Service注入进行讲解: 控制器关键代码 ...

  2. Spring整合Struts2

    ①导入Struts2 jar包 ②在web.xml文件中创建过滤器 <?xml version="1.0" encoding="UTF-8"?> & ...

  3. Struts2标签库

    这是个音乐播放列表 1.queryMusic.jsp <%@ page language="java" import="java.util.*" page ...

  4. Struts2中Action接收参数

    Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数:     a.定义:在Action类中定义属 ...

  5. struts2笔记01-环境搭建

    1.官网下载struts2 struts-2.3.28-all.zip,这个包可谓应有尽有,以后全靠它了! 2.jar包怎么选?       (1)struts-2.3.28-all\struts-2 ...

  6. 使用Struts2标签遍历集合

    遍历Map<String,Object> 遍历Map<Stirng,List<Student>> 遍历List<Map<String,Student&g ...

  7. 关于SpringMVC和Struts2的区别

    1.    与struts2不同 1.  springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过虑器. 2. springmvc是基于方法开发,传递参数是通 ...

  8. java struts2值栈ognl_Struts2 (三) — OGNL与值栈

    一.OGNL表达式 1.概述 1.1什么是OGNL ​ OGNL是Object-Graph Navigation Language的缩写,俗称对象图导航语言. 它是一种功能强大的表达式语言,通过它简单 ...

  9. 基于Struts2框架的名片管理系统

    目录 1.系统设计 2.数据库设计 3.系统管理 4.用户管理 5.名片管理 本篇博文将分享一款基于Struts2框架的名片管理系统,JSP引擎为Tomcat9.0,数据库采用的是MySQL5.5,集 ...

  10. Struts2 2.5版本新配置filter-class

    在web.xml 默认代码: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&q ...

最新文章

  1. 行业观察 | 全球IoT云平台第一股诞生,IoT离爆发还有多远?
  2. android MIPI屏 导航栏丢失
  3. [ATF]-MTK:一篇文章了解ATF原理
  4. vivado环境下实现比较器
  5. php正则原子,PHP正则表达式---原子
  6. Qt 读写XML文件
  7. 对 SAP UI5 一无所知的新手,从哪些材料开始学习比较好?
  8. mysql安装innodb插件
  9. java字符串去掉一头一尾_快学Scala第13章----集合
  10. Programming in the Mid-Future(转)
  11. C语言读取文件大量数据到数组
  12. apache安装、配置虚拟主机、配置日志
  13. 【项目管理一点通】(48) 项目结项
  14. 【comsol快速入门】
  15. 18年12月计算机英语统考成绩查询,没查的抓紧,18年12月四六级成绩查询入口将关闭...
  16. 修复计算机命令行,如何用命令提示符修复系统还原_用cmd命令提示符恢复系统的方法...
  17. 可插拨的观感体系结构(一)
  18. 使用Websphere的TPTP工具进行性能分析和监控
  19. 采用FFmpeg从视频中提取音频(声音)保存为mp3文件
  20. Axure RP 9 基础教程

热门文章

  1. 亚马逊广告打造篇(第二期)
  2. html中文本不自动换行,CSS禁止文本自动换行代码
  3. 浙江金华“刷脸看病”,挂号付款20秒搞定!
  4. 香帅的北大金融学课笔记 -- 宏观金融实事分析
  5. windows xp 驱动开发(七)WDK源码 UsbSamp例子的编译及使用
  6. 乒乓球训练机_一个人练习乒乓球的12种方法
  7. Atitit 号码规范 靓号指南 attilax总结 v4 r926.docx 1. Keyword关键词 2 2. 为什么我们需要靓号指南,因为很多人手机号都是瞎选乱选,没有规范不成方圆
  8. jquery换一批的写法
  9. powershell 批量转换文本文件编码(GBK转UTF-8)
  10. 微信小程序设置动态页面title(navigationBarTitleText)