Struts1.0原理 1、页面提交的数据封装到 Controller Action form中 (控制器) 2、通过请求路径查找 Struts config. xml中 Action的配置,找 1到并调用对应的 Action Action Servlet 浏览器 2Action Form, Action Mapping JavaBean 数据库 Action 6 3 Mode(模型) 开发时只需关注: JsP查找 <ActionMapping,找到正确的 两个核心组件 JSP页面,并转发 Action Form Action view(视图) 一个配置文件
Struts 1.0原理 ActionForm, ActionMapping 1 2 3 4 5 6 开发时只需关注: ·两个核心组件: ActionForm Action ·一个配置文件 1、页面提交的数据封装到 ActionForm中 2、通过请求路径查找Struts- config.xml中Action的配置,找 到并调用对应的Action Controller (控制器) Model(模型) View(视图) 查找ActionMapping,找到正确的 JSP页面,并转发
配置文件 e struts-config xml ●配置 Form bean和 Action bean 服务器启动时, Struts装载 struts-config Xn信息 <struts-config> <form-beans> <form-bean name=add Form" type="web form. Add Form"1 </form-beans> <action-mappings> <action name="add Form"path="/add"scope="request type=web action. AddAction"> <forward name="input"path="/add. jsp"/> <forward name=result"path=/add result. jsp"p <action> </action-mappings> </struts-config>
配置文件 struts-config.xml 配置Form Bean和Action Bean 服务器启动时,Struts装载struts-config.xml信息 <struts-config> <form-beans> <form-bean name="addForm“ type=“web.form.AddForm" /> </form-beans> <action-mappings> <action name="addForm" path="/add" scope="request" type=“web.action.AddAction"> <forward name="input" path="/add.jsp" /> <forward name="result" path="/add_result.jsp" /> </action> </action-mappings> </struts-config>
Struts1.0的缺点 ●所支持的表现层技术比较单 只支持JSP,不支持目前流行的 FreeMarker、 Velocity等 与 Servlet aple耦合严重,难于测试 它完全是基于 Servlet APll的,所以在 Struts 1的业务逻辑控 制器内,充满了大量的 Servlet api Servlet严重依赖于Web服务器,尤其是 Http servletRequest和 HttpservletreSponse两个参数,通常由 Web容器负责实例化。一日脱离了Web服务器, Action将很难 测试。 属于侵入式设计 Action中包含了大量的 Struts1o的API,影响了代码的重 构。一旦系统需要重构时,这些 Action类将完全没有利用价值, 成为一堆废品
Struts 1.0的缺点 所支持的表现层技术比较单一 只支持JSP,不支持目前流行的FreeMarker、Velocity等 与Servlet API耦合严重,难于测试 它完全是基于Servlet API的,所以在Struts 1的业务逻辑控 制器内,充满了大量的Servlet API。 Servlet严重依赖于Web服务器,尤其是 HttpServletRequest和HttpServletResponse两个参数,通常由 Web容器负责实例化。一旦脱离了Web服务器,Action将很难 测试。 属于侵入式设计 Action中包含了大量的Struts1.0的API ,影响了代码的重 构。一旦系统需要重构时,这些Action类将完全没有利用价值, 成为一堆废品
Struts2 Struts2是在 Web Work2基础发展而来的。和 struts一样, Struts2也属于MVC框架。不过有一点大家需要注意的是: 尽管 Struts2和 struts在名字上的差别不是很大,但 Struts2 和 struts在代码编写风格上几乎是不一样的。那么既然有 struts,为何还要推出 struts2。主要是因为 Struts有以 下优点: 1>在软件设计上 Struts2没有像 Struts1那样跟 Servlet api和 struts Api有着紧密的耦合, Struts.的应用可以不依赖于 Servlet api和 struts api。 Struts2的这种设计属于无侵入式 设计,而 Struts1却属于侵入式设计。 public class OrderListAction extends Action I public Action Forward execute(Action Mapping mapping, Action Form form, Http Servlet request request Http Servlet response response throws Exception 2> Struts提供了拦截器,实现如权限拦截等功能。 3> Strut提供了类型转换器,我们可以把特殊的请求参数转换成需要的类型。在 Struts中,如果我们要实现同样的功能,就必须向 Struts的底层实现 BeanUtil注册类 型转换器才行。 4> Struts2提供支持多种表现层技术,如:JSP、 freeMarker、 Velocity等 5> Struts2的输入校验可以对指定方法进行校验,解决了 Struts长久之痛。 6>提供了全局范围、包范围和 Action范围的国际化资源文件管理实现
Struts2 Struts2是在WebWork2基础发展而来的。和struts1一样, Struts2也属于MVC框架。不过有一点大家需要注意的是: 尽管Struts2和struts1在名字上的差别不是很大,但Struts2 和struts1在代码编写风格上几乎是不一样的。那么既然有 了struts1,为何还要推出struts2。主要是因为struts2有以 下优点: 1 > 在软件设计上Struts2没有像struts1那样跟Servlet API和struts API有着紧密的耦合, Struts2的应用可以不依赖于Servlet API和struts API。 Struts2的这种设计属于无侵入式 设计,而Struts1却属于侵入式设计。 public class OrderListAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { }} 2> Struts2提供了拦截器,实现如权限拦截等功能。 3> Strut2提供了类型转换器,我们可以把特殊的请求参数转换成需要的类型。在 Struts1中,如果我们要实现同样的功能,就必须向Struts1的底层实现BeanUtil注册类 型转换器才行。 4> Struts2提供支持多种表现层技术,如:JSP、freeMarker、Velocity等 5> Struts2的输入校验可以对指定方法进行校验,解决了Struts1长久之痛。 6> 提供了全局范围、包范围和Action范围的国际化资源文件管理实现
public class LoginAction extends Action 业务逻辑控制器必须继承 Struts1 提供的 Action类 public Action Forward execute(Action Mapping mapping, Action Form form HttpservletreqUest request HttpservletresPonse response)throws Auction Exception Login Form login Form=( Login form)form;/将其强制类型转换为登录用 ∥¥用户名为scot密码为iger时返回成功的 Action Form if (scottequals(login Form get Username &&“ tiger”. equals( login Form. getPasswordo) i return mapping find Forward(success?") Is 返回一个 Action forward对象 return mapping find Forward (error)
S pt ubr licu clat sss Log1 inA . c0 tion的exten缺ds Act点ion { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws AuctionException { LoginForm loginForm = (LoginForm)form; //当用户名为scott,密码为tiger时返回成功 if (“scott”.equals(loginForm.getUsername() && “tiger”.equals(loginForm.getPassword()) { return mapping.findForward(“success”); } else { return mapping.findForward(“error"); } } } 业务逻辑控制器必须继承Struts 1 提供的Action类 //将其强制类型转换为登录用 的ActionForm 返回一个ActionForward对象