丁勇 Email:18442056@QQ.com
学习目标 理解 Struts 框架的工作原理 掌握使用 Struts 框架开发 Web 应用的基本步骤 熟悉 MyEclipse 对 Struts 开发的支持
Web 框架事实标准 : Web 框架的事实标准 http://struts.apache.org Java EE 主流技术趋势图 主流 Web 框架趋势图
使用 Struts 实现加法器 使用开发的 4 个步骤 1 2 3 4 给项目添加 Struts 支持 实现业务类添加 Struts 组件 1 ActionForm 2 Action 3 添加 jsp 页面 4 配置 struts config.xml 调试运行 完成的项目
使用 Struts 实现加法器 1 给项目添加 Struts 支持 新建 Web 项目 在项目节点上单击右键 添加 Struts 支持 点选 MyEclipse Add Struts Capabilities
使用 Struts 实现加法器 1 给项目添加 Struts 支持 增加了 : Struts JAR 包 strutsconfig.xml Struts 标签库 修改了 web.xml Struts 是基于 Servlet 技术实现的框架 可以配置 Struts 配置文件的文件名, 支持多个配置文件 添加 Struts 支持前的项目 拦截所有以.do 结尾的添加 Struts 支持后的项目请求
使用 Struts 实现加法器 2 实现业务类 ( 模型 -Model)
3 使用 Struts 实现加法器 添加 Struts 组件 2 1 ActionForm 填写 Name 属性 1 使用 Struts 1.2 Form,Action&J SP 向导新建组件填写 Form type 属性 3 单击 Add, 给 Form 增加属性
使用 Struts 实现加法器 3 添加 Struts 组件 2 Action 配置 Action 属性 设置 Path 属性即访问该 Action 的路径 设置 Type 属性即 Action 对应的 Java 类 设置 Form 属性将 Form 和 Action 关联 设置 Forwards 属性用于配置转发的 JSP 页面
3 使用 Struts 实现加法器添加 Struts 组件 2 Action -- 编写 Action 代 y2ssh.sg.chp2.addaction.ja 码 public ActionForward execute(actionmapping mapping, va ActionForm form, HttpServletRequest request, HttpServletResponse response ) { Calculator calc = new Calculator(); // 获得 ActionForm 的引用从 ActionForm 中即可 AddForm addform = (AddForm) form; 获得表单提交的参数 double model = calc.add( Double.parseDouble(addForm.getFirstNumber()), Double.parseDouble(addForm.getSecondNumber()) ); // 将计算结果存储到 request 中 request.setattribute("sum", model); 通过逻辑名引用页面 // 将请求转发到 "result" 出口指向的页面逻辑名和页面对应关系在 return mapping.findforward("result"); struts-config.xml 中配置
3 使用 Struts 实现加法器添加 Struts 组件 3. 添加 JSP 页面根据 Action 的 Forwords 属性的设置, 添加 add.jsp 和 add_result.jsp 两个页面采用 Struts 提供的对应前面 Action html:form 标签的 path 属性 <html:form action="/add" method="post"> 第一个数 : <input type="text" name="firstnumber"/> /ch02/add.jsp 第二个数 : <input type="text" name="secondnumber"/> /ch02/add_result.jsp <input type="submit" ActionForm 保存在 name="submitadd" value=" 加 "/> request 或 session 中 </html:form> 第一个数 : ${requestscope.addform.firstnumber} 第二个数 : ${requestscope.addform.secondnumber} 对应前面 Form 的属性名
3 使用 Struts 实现加法器 添加 Struts 组件 4 配置 struts-config.xml add.jsp AddAction.java
使用 Struts 实现加法器 3 调试运行 演示示例 : 基于基于 Struts 实现加法器
小结 使用 Struts 开发加法器过程中使用了哪些 Struts 组件? ActionForm 组件组件 Action 组件组件 使用 Struts 开发步骤是什么? 1 添加 Struts 支持 2 开发业务逻辑类 ( 模型,Model) 3 开发 ActionForm 组件 ( 视图组件,View) 4 开发 Action 组件 ( 控制组件,Controller) 5 开发 JSP 页面 6 修改配置文件 struts config.xml, 配置 ActionForm 和 Action 7 调试运行
回顾 Servlet+JSP+JavaBean 的编程模型 Servlet JaveBean JSP 数据库
自定义框架原理 Controller Servlet 视图 AddAction 控制器 add.jsp Calculator 业务逻辑方法 模型 add_result.jsp
Struts Web 应用运行过程 控制器组件 : ActionServlet struts- config.xml 控制器组件 : 控制器组件 : [Add]Action [Post]Action 模型组件 : [Post]Biz 视图组件 : JSP 视图组件 : JSP 模型组件 : [Post]DAO 数据库
1 6 开发时只需关注 : 两个核心组件 : ActionForm [Add]Action 一个配置文件 Controller ( 控制器控制器 ) ActionServlet 2 [Add]Action 5 JSP View( View( 视图视图 ) AddForm, ActionMapping JavaBean 3 1 页面提交的数据封装到 ActionForm 中 2 通过请求路径查找 Strutsconfig.xml 中 Action 的配置, 找到并调用对应的 Action 4 Model( 模型模型 ) 数据库 查找 ActionMapping, 找到正确的 JSP 页面, 并转发
Struts MVC 小结 控制器组件 ActionServlet 由 Struts 提供 :org.apache.struts.action.actionservlet 是一个 Servlet, 需要在 web.xml 中配置 [Add]Action Action Bean 封装一类客户操作继承自 org.apache.struts.action.action, 实现 execute public ActionForward execute(actionmapping 方法 mapping, 需要在 struts config.xml ActionForm 中配置 form, public class AddAction extends Action { } } HttpServletRequest request, HttpServletResponse response) { // TODO: 在这里添加代码 return mapping.findforward("result");
Struts MVC 小结 视图组件 ActionForm Form Bean 封装页面提交的数据继承自 org.apache.struts.action.actionform 需要在 struts config.xml 中配置与 JSP 的交互 -- 从页面获得输入 addform.getfirstnumber(); -- 发送数据到页面 request.setattribute("data", data); 其他视图组件 :JSP JSTL EL 自定义标签第 4 章介绍 Struts 标签
Struts MVC 小结 模型组件 Struts 对模型组件的实现没有任何限制 一般为 :[User]Biz 接口 [User]BizImpl 类 [User]DAO 接口 [User]DAOJdbcImpl 类 <struts-config> struts config.xml <form-beans> <form-bean name="addform" 配置 Form Bean 和 Action Bean type="y2ssh.sg.ch2.web.form.addform" /> 服务器启动时,Struts 装载 struts config.xml 信息 </form-beans> <action-mappings> <action name="addform" path="/add" scope="request" type="y2ssh.sg.ch2.web.action.addaction"> <forward name="input" path="/add.jsp" /> <forward name="result" path="/add_result.jsp" />
小结 Struts 是如何实现 MVC 的?
Struts 执行顺序 Struts 深度历险 在 AddForm 和 AddAction 中增加调试代码 运行程序查看控制台输出 ActionServlet 调用 AddForm 的 setter 方法然后调用 AddAction 的 execute 方法最后页面将 Form 中的值显示出来
Struts 深度历险 ActionForm 和 ActionMapping 中有什么? Action 中 execute 方法的参数列表 public ActionForward execute( AddAction.java ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 设置断点, 观察变量 通过在 excute 方法中设置断点 观察变量, 可以帮我们解决很多使用 Struts 中出现的问题
小结 结合下图, 说说 Struts 的运行过程
使用 Struts 开发 发布出租信息 使用 Struts 开发 发布房屋信息 功能 模型 (Model) 组件 典型的三层结构 面向接口编程 开发顺序 : DAO Biz Form Bean JSP Action Bean struts config.xml 调试
使用 Struts 开发 发布出租信息 业务流程 post.jsp 1 如果未登录, 转到 login 2 如果 isconfirmed 为 false, 转到 confirm; 否则, 保存数据, 转到 userlist 3 如果出错, 转到 error confirm.jsp PostAction login error userlist
public ActionForward execute(...){ // 如果没有登录, 转发到登录页面 HttpSession session = request.getsession(false); 使用 Struts 开发 发布出租信息 USER user = null; if (session!= null) { user = (USER) session.getattribute("user"); } if(user == null){ PostAction.java return mapping.findforward("login"); 实现代码 } } // 如果还没有确认, 转到确认页面 String confirm = request.getparameter("confirm"); if(null==confirm!"yes".equals(confirm)){ return mapping.findforward("confirm"); } // 调用业务逻辑方法, 插入数据到数据库 try{ FwxxForm fwxxform = (FwxxForm) form; FWXX fwxx = new FWXX(); fwxx.settitle(fwxxform.gettitle());... biz.post(fwxx); request.setattribute("msg"," 发布成功!"); return mapping.findforward("index"); }catch(exception e){ request.setattribute("msg"," 出错啦 :" + e.getmessage()); return mapping.findforward("error"); }
使用 Struts 开发 发布出租信息 配置文件 <form-beans> <form-bean name="fwxxform" type="com.aptech.jb.entity.form.fwxxform" /> </form-beans> <action-mappings> <action name="fwxxform" path="/post" scope="request" type="com.aptech.jb.action.postaction"> <forward name="affirm" path="/affirm.jsp" /> <forward name="userlist" path="/list.do?myrent=y" /> <forward name="login" path="/login.jsp" /> <forward name="error" path="/error.jsp" /> 演示示例 </action> : 使用使用 Struts 开发开发 发布出租信息 功能功能 </action-mappings>
总结 如何使用 Struts 开发登录功能? Action 组件的作用是什么? ActionForm 组件的作用是什么? struts config.xml 中都配置了哪些信息? 回忆 Struts 架构图, 讲述 Struts 是如何实现 MVC 模式的?