




已阅读5页,还剩32页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
v c 表示层 jsp 控制层 servlet struts2 userWorld helloWorld struts xml文件 success jsp error jsp public class Struts2Filter implements Filter private Map map new HashMap public void init FilterConfig filterConfig throws ServletException map put primer userAction action cn itcast web UserAction map put helloworld helloworldAction action cn itcast web HelloworldAction public void doFilter ServletRequest req ServletResponse res FilterChain chain throws IOException ServletException String path request getServletPath System out println path path Action action Action Class forName map get path newInstance action execute request getRequestDispatcher success jsp forward request response 加载 路径需要pg自己写 userWorld helloWorld struts xml文件 xml文件名称必须是struts xml 放置在src下 类路径下 该文件的配置自己写 要遵循dtd规范 struts2提供的 success jsp error jsp struts2流程 struts xml文件 success jsp error jsp 过滤器struts2提供的的 加载 struts xml文件 xml文件名称必须是struts xml 放置在src下 类路径下 该文件的配置自己写 要遵循dtd规范 struts2提供的 success jsp error jsp M 业务层 bo Dao层 po 持久 spring hibernate public interface Action public String execute public class UserAction implements Action public String execute System out println UserAction return success public class HelloworldAction implements Action public String execute System out println HelloworldAction return error struts xml文件 success jsp error jsp 该接口是struts2提供的 作为pg实现该接口 public interface Action public String execute pg自己写的 public class UserAction implements Action public String execute System out println UserAction return success pg自己写的 public class HelloworldAction implements Action public String execute System out println HelloworldAction return error struts xml文件 success jsp error jsp public class Struts2Filter implements Filter private Map map new HashMap public void init FilterConfig filterConfig throws ServletException map put primer userAction action cn itcast web UserAction map put helloworld helloworldAction action cn itcast web HelloworldAction public void doFilter ServletRequest req ServletResponse res FilterChain chain throws IOException ServletException String path request getServletPath System out println path path Action action Action Class forName map get path newInstance action execute request getRequestDispatcher success jsp forward request response struts xml文件 xml文件名称必须是struts xml 放置在src下 类路径下 该文件的配置自己写 要遵循dtd规范 struts2提供的 success jsp error jsp public class HelloworldAction implements Action public String execute System out println HelloworldAction return error struts xml文件 xml文件名称必须是struts xml 放置在src下 类路径下 该文件的配置自己写 要遵循dtd规范 struts2提供的 success jsp error jsp 过滤器struts2提供的的 pg自己写的 public class HelloworldAction implements Action public String execute System out println HelloworldAction return error 客户端发送请求 primer helloWorldAction action a 9 struts2的过滤器 action result jsp html 分离关注 class A aa 1 2 3 4 class A1 1 class A2 2 class A3 3 class A4 4 struts2中使用分离关注方式 把完成具体任务的工作委托给多个类 完成 这样的类叫做拦截器 处理请求参数拦截器 处理Cookie拦截器 文件上传拦截器 表单重复提交拦截器 自定义拦截器 过滤器和拦截器区别 过滤器 javaee中的规范 任何javaweb中都可以使用 具有通用性 拦截器 是struts2中提出的概念 只拦截器action 离开了struts2框架拦截器不复存在 在struts中是过滤器来调用拦截器 struts2的过滤器 action result jsp html resulttype success jsp 转发到jsp public class ServletDispatcherResult extends StrutsResultSupport private String location public void setLocation String location resulttype success jsp this location location public String getLocation return location public void doExecute String finalLocation ActionInvocation invocation throws Exception HttpServletRequest request ServletActionContext getRequest HttpServletResponse response ServletActionContext getResponse RequestDispatcher dispatcher request getRequestDispatcher location dispatcher forward request response helloWorldAction primer 重定向到action public class ServletActionRedirectResult extends ServletRedirectResult protected String actionName protected String namespace public void setActionName String actionName this actionName actionName public void setNamespace String namespace this namespace namespace protected void doExecute String finalLocation ActionInvocation invocation HttpServletResponse response HttpServletResponse ctx get ServletActionContext HTTP RESPONSE sendRedirect response finalLocation protected void sendRedirect HttpServletResponse response String finalLocation response sendRedirect finalLocation helloWorldAction primer public class ServletDispatcherResult extends StrutsResultSupport private String location public void setLocation String location resulttype success jsp this location location public String getLocation return location public void doExecute String finalLocation ActionInvocation invocation throws Exception HttpServletRequest request ServletActionContext getRequest HttpServletResponse response ServletActionContext getResponse RequestDispatcher dispatcher request getRequestDispatcher location dispatcher forward request response public class ServletActionRedirectResult extends ServletRedirectResult protected String actionName protected String namespace public void setActionName String actionName this actionName actionName public void setNamespace String namespace this namespace namespace protected void doExecute String finalLocation ActionInvocation invocation HttpServletResponse response HttpServletResponse ctx get ServletActionContext HTTP RESPONSE sendRedirect response finalLocation protected void sendRedirect HttpServletResponse response String finalLocation response sendRedirect finalLocation aop userAction save action 模拟用户登陆 session setAttribute user user 如果用户没有登陆 则不能访问UserAction中的save方法 转到error jsp 如果用户登陆了 则能访问UserAction中的save方法 转到success jsp struts2的过滤器 处理参数的拦截器 处理Cookie的拦截器 自定义的拦截器 自定义的拦截器 Object obj session getAttribute user 用户没有登陆 if obj null return error error jsp 用于已经登陆 调用action中的save方法 error jsp public class UserAction extends ActionSupport public String save System out println save return success 模拟用户登陆 session setAttribute user user 如果用户没有登陆 则不能访问UserAction中的save方法 转到error jsp 如果用户登陆了 则能访问UserAction中的save方法 转到success jsp public class ValidateAction extends ActionSupport private String age private String username 第一个执行 先获取表单页面的值 public void setUsername String username this username username 第二个执行 public void validate 验证userName 第三个执行业务方法 public String login 进行业务逻辑判断 return success 验证 前台验证 客户端验证 javaScript验证 后台验证 服务器端验证 struts2 的验证机制 fff age xml配置文件 true public class RequiredStringValidator extends FieldValidatorSupport private boolean doTrim true public void setTrim boolean trim doTrim trim public boolean getTrim return doTrim public void validate Object object throws ValidationException String fieldName getFieldName username Object value this getFieldValue fieldName object String s String value if doTrim s s trim if s length 0 addFieldError fieldName object public class RegexFieldValidator extends FieldValidatorSupport private String expression a zA Z0 9 6 12 public void setExpression String expression this expression expression public void validate Object object throws ValidationException String fieldName getFieldName psw Object value this getFieldValue fieldName object aa Pattern pattern pattern Ppile expression String compare String value if trim compare compare trim Matcher matcher pattern matcher compare if matcher matches addFieldError fieldName object public class ValidateAction extends ActionSupport private String age private String username 第一个执行 先获取表单页面的值 public void setUsername String username this username username 第二个执行 public void validate 验证userName 第三个执行业务方法 public String login 进行业务逻辑判断 return success public class RequiredStringValidator extends FieldValidatorSupport private boolean doTrim true public void setTrim boolean trim doTrim trim public boolean getTrim return doTrim public void validate Object object throws ValidationException String fieldName getFieldName username Object value this getFieldValue fieldName object String s String value if doTrim s s trim if s length 0 addFieldError fieldName object public class RegexFieldValidator extends FieldValidatorSupport private String expression a zA Z0 9 6 12 public void setExpression String expression this expression expression public void validate Object object throws ValidationException String fieldName getFieldName psw Object value this getFieldValue fieldName object aa Pattern pattern pattern Ppile expression String compare String value if trim compare compare trim Matcher matcher pattern matcher compare if matcher matches addFieldError fieldName object public class OgnlValueStack implements ValueStack OgnlContext context OgnlContext Map集合 CompoundRoot root List集合 public class OgnlContext implements Map private Object root private Map values new HashMap 23 实际放置 public class CompoundRoot extends ArrayList request作用域 ValueStack valueStack ValueStack request getAttribute struts valueStack OgnlContext context Map集合 Map栈 CompoundRoot root List集合 对象栈 对象栈 List集合 Map栈 OgnlContext对象 Object root Map values Map valueStackAction 0 1 key value request key username psw CompoundRoot root List集合 对象栈 存放的数据 当前请求的action对象 调用valueStack set msg msgxxxx 方法 先放置数据到Map集合中 在把Map集合放置到List集合 对象栈 中 valueStack getRoot add 0 new Employee 自己直接放置对象到list 集合 对象栈 中 OgnlContext context Map集合 Map栈 request session application page 四个作用域中信息在Map栈放置 ognlAction test action cid 9 请求的参数值也在Map栈放置 default Person对象 2 3 HttpServletRequest request ServletActionContext getRequest request setAttribute username username request request setAttribute psw username request ServletContext sc ServletActionContext getServletContext sc setAttribute username username application sc setAttribute psw psw application application username psw Map栈 OgnlContext对象 Object root 复制 person person对象 id name age Map sessionMap ServletActionContext getContext getSession sessionMap put username username session sessionMap put psw psw session HttpServletRequest request ServletActionContext getRequest request setAttribute username username request request setAttribute psw username request ServletContext sc ServletActionContext getServletContext sc setAttribute username username application sc setAttribute psw psw application value username request psw request usernaem application public class OgnlContext implements Map private Object root private Map values new HashMap 23 实际放置 public class CompoundRoot extends ArrayList value key username psw CompoundRoot root List集合 对象栈 存放的数据 当前请求的action对象 调用valueStack set msg msgxxxx 方法 先放置数据到Map集合中 在把Map集合放置到List集合 对象栈 中 valueStack getRoot add 0 new Employee 自己直接放置对象到list 集合 对象栈 中 OgnlContext context Map集合 Map栈 request session application page 四个作用域中信息在Map栈放置 ognlAction test action cid 9 请求的参数值也在Map栈放置 psw application ServletContext sc ServletActionContext getServletContext sc setAttribute username username application sc setAttribute psw psw application username psw Map栈 OgnlContext对象 Object root person对象 id name age usernaem application ServletContext sc ServletActionContext getServletContext sc setAttribute username username application sc setAttribute psw psw application 用户名 电话 描述 用户名 电话 描述 cn itcast model Person 编辑 model userAction edit action id 1 public String edit 获取id 通过id查询数据库信息 模拟 User newUser new User newUser setUsername zhang newUser setTel 110 newUser setDes xxx 复制从数据库中查出的数据到栈顶对象 user setUsername newUser getUsername user setTel newUser getTel user setDes newUser getDes model edit jsp return edit edit jsp 用户名 电话 描述 edit jsp 用户名 电话 描述 数据回显的原理 当struts2解析该标签时 获取name属性的值username 以username为条件 到对象栈中查找PropertyName username 的属性 如果找到了 获取该属性的值 该属性的值就是的值 如果找到了 获取该属性的值 该属性的值 是 就是的值 如果没有找到了 的值 user对象 user newUser private User user new User User newUser new User newUser setUsername zhang newUser setTel 110 newUser setDes xxx user newUser edit jsp 用户名 电话 描述 username des tel cn itcast model Person edit jsp 用户名 电话 描述 栈顶 User对象new user对象 public class UserAction extends ActionSupport private Integer id private String userName private Date createTime private Edu edu public String save 获取request HttpServletRequest request ServletActionContext getRequest String id request getParameter id String userName request getParameter userName System out println id userName return success 编号 姓名 出生日期 学历id 学历名称 public interface TypeConverter public Object convertValue Map context Object target Member member String propertyName Object value Class toType public class DefaultTypeConverter implements TypeConverter 重写该方法 public Object convertValue Map context Object value Class toType return convertValue value toType public Object convertValue Map context Object target Member member String propertyName Object value Class toType return convertValue context value toType TypeConverter typeConverter new DefaultTypeConverter typeConverter convertValue Map context Object target Member member String propertyName Object value Class toType 入口方法 public class DefaultTypeConverter implements TypeConverter 重写该方法 public Object convertValue Map context Object value Class toType return convertValue value toType public Ob
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2030中国荷花凉茶行业市场深度研究及发展前景投资可行性分析报告
- 2025至2030中国糖加工筛行业市场深度研究及发展前景投资可行性分析报告
- 风电项目环境监测与优化方案
- 钢结构高空作业技术创新与新工艺应用
- 2025版航空服务业员工全日制劳动合同书
- 2025版矿山地质勘查与资源评价合同范本
- 二零二五年度搅拌站工程结算审计合同
- 2025版矿山矿产资源开发利用合同范本
- 2025版高科技园区墙面喷漆智能化合同范本
- 2025版新能源汽车购置贷款融资服务合同
- 部编版《县委书记的榜样-焦裕禄》课件1
- 汽车保养基础知识优秀课件
- 青少年运动员 运动损伤的预防 课件
- 物资供应投标书范本
- 2022年十部经典的三级片电影
- 眼震视图结果分析和临床意义
- 2011-2017国民经济行业分类标准转换对照表
- 《现代汉语》PPT课件(223页PPT)
- 顶推法钢箱梁安装施工方案
- 桥架支吊架安装实用标准图
- 中国诗词协会入会申请表
评论
0/150
提交评论