Jakarta Struts Framework 是一个开放源代码的 Apache 项目,由 Craig R. McClanahan 于 2000 年建立,并捐赠给了 Apache 基金会,已经成为基于 Java Servlet 和 Java Server Page (JSP)技术构建 Web 应用程序的最流行的框架包。 Craig 编写了 Tomcat4 实现的大部份内容,同时他仍是 JCP 的资深成员,负责 Servlet 和 JSP 规范的制定。html
Struts 是一个基于模型 (Model) -视图 (View) -控制器 (Controller)(MVC) 模式的应用架构的开源框架。若是您开发过大型 Web 应用程序,您就理解变化这个词的含义。“模型 - 视图 - 控制器” (MVC) 就是用来帮助您控制变化的一种设计模式。 MVC 减弱了业务逻辑接口和数据接口之间的耦合。java
JSP 页面中能够很是容易地结合业务逻辑 (jsp:useBean) 、服务端处理过程( jsp:scriplet )和 HTML(<html>) ,在 JSP 页面中同时实现显示,业务逻辑和流程控制,从而能够快速地完成应用开发。如今不少的 Web 应用就是由一组 JSP 页面构成的。这种以 JSP 为中心的开发模型咱们能够称之为 Model 1 。固然这种开发模式在进行快速和小规模的应用开发时,是有很是大的优点,可是从工程化的角度考虑,它也有一些不足之处,主要在于不方便应用扩展,以及不利 于应用系统业务的重用和改动。考虑到这些问题在开发大型的 Web 应用时必须采用不一样的设计模式——这就是 Model2 。web
Model 2 表示的是基于 MVC 模式的框架。 "Model" 表明的是应用的业务逻辑(经过 JavaBean , EJB 组件实现), "View" 是应用的表示面(由 JSP 页面产生), "Controller" 是提供应用的处理过程控制(通常是一个 Servlet ),经过这种设计模型把应用逻辑,处理过程和显示逻辑分红不一样的组件实现,这些组件能够进行交互和重用。 Model 2 具备组件化的优势从而更易于实现对大规模系统的开发和管理。apache
Struts 是一组相互协做的类、 Servlet 和 JSP 标记,它们组成一个可重用的 MVC 2 设计。这个定义表示 Struts 是一个框架,而不是一个库,但 Struts 也包含了丰富的标记库和独立于该框架工做的实用程序类库。 Struts 利用 taglib 得到可重用代码和抽象 Java 代码,利用 ActionServlet 配合 Struts-config.xml 实现对整个系统导航。加强了开发人员对系统的总体把握,提升了系统的可维护性和可扩充性。后端
Struts 是一个 Web Framework ,常见的 B/S Web Framework 以下:设计模式
Struts :主要关注于 Controller 流程,基于 JSP与 Taglib 实现的 View 。架构
Cocoon :用于处理 XML 的框架,将 XML 做为 View 并能够良好结合 XSLT 。app
Spring :这是一个服务于全部层面的 Application Framework ,提供了 bean 的配置基础, AOP 的支持, JDBC 的提框架
取框架,抽象事务支持等。它有一个很是显著的特色:在某个层面上若是你不须要 Spring 的支持,你就能够jsp
不使用 String 的 class ,只使用它的某一部分的功能。
JSF :JavaServerFaces ,严格意义上讲,它并不算是 Framework ,但 JSF 一样提供了一种不一样的 View 生成方式。
它能够和多种 Framework 整合。在 J2EE1.4 中, JSP2.0 规范就包含了 JSF 标准。
Framework 有不少种, Struts 并非咱们惟一的选择。咱们能够根据具体的项目来选择不一样的 Framework 。 Struts 之因此如此流行,其主要缘由是它推出时间比较早,版本较稳定,而且是 MVC 及 core J2EE patterns 的标准实现。
Struts 师出名门,拥有诸多优势,其中最关键的一点就是简单易用,这也是正他迅速普及,愈来愈流行的根本缘由。与其同时代的 Tubine 、 Cocoon 等,均可以做为 Web 框架,功能也很强大,但使用起来过于复杂,一直难以流行。做为一名普通的 Java 开发者,只要稍具 JSP 开发水平,均可以在一周左右的时间内,基本学会 Struts 。
Struts 还提供了丰富的 Taglib ,熟练用之,将节省大量的开发时间。
Struts 具备良好的页面导航功能,开发者能够经过一个名为 struts-config.xml 的文件,把握整个系统各部分之间的联系,这对于后期维护有着莫大的帮助,尤为是当另外一批开发者接手该项目时,这种优点体现得更加明显。
固然, Struts 也有一些不足之处。对于很大的项目,当 struts-config.xml 愈来愈庞大时,维护也就变得愈来愈复杂,甚至须要专人负责(对于这种维护状况,建议采用专门的维护工具,如 Struts Console )。另外, Struts 框架主要是针对表示层设计的,对于后端的逻辑层支持不是很强。
未来的发展趋势,颇有多是 JSF+Struts+Spring+Hibernate/JDO 的开发模式,建议你从 Struts 入手。
话很少说开始入门
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>StrutsDemo</display-name> <!-- struts2框架的核心控制器,用户对本程序的全部请求都先交给 StrutsPrepareAndExecuteFilter核心控制器进行处理 核心控制器就是根据struts.xml的文件来决定具体使用什么业务逻辑控制器处理本次请求 --> <filter> <filter-name>struts</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <!-- 表示接收全部请求 --> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
这个是struts的模块配置,他被struts.xml include放进模块 hr-user.xml,不少公司都是模块式开发,因此我也用模块是开发 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="hr" extends="struts-default" namespace="/"> <action name="user" class="org.heinrich.action.UserAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
这个是struts的全局配置,在这个xml中的全部配置,在其余的模块中均可以用至关于定义了全局变量,在其余模块中的就是局部变量 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 在struts.xml文件中一般是配置一下模块的引入配置和公共配置 公共的常量配置能够参考struts2-core-2.3.15.1.jar中的default.properties文件中配置 --> <!-- 配置本项目容许的请求后缀 --> <constant name="struts.action.extension" value="action,do,,"/> <!-- 配置本工程是开发模式(会热部署) --> <constant name="struts.devMode" value="true"/> <!-- 开启动态方法调用,默认是开启状态 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 为了方便团队分模块开发,一般建议将不一样的模块的包(package)对应的单独的文件中 而后使用include 标签导入的struts.xml文件中 --> <include file="hr-user.xml"/> </struts>
UserAction 的配置
package org.heinrich.action; import org.heinrich.dao.UserDao; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 6161139006224338587L; private String key; private String value; public String add() { System.out.println("I am reading Book about Linux"); UserDao userDao = new UserDao(); userDao.add(key, value); return "success"; } public String delete() { System.out.println("I am reading Book about Linux"); UserDao userDao = new UserDao(); userDao.delete(key); return "success"; } public String update() { System.out.println("I am reading Book about Linux"); UserDao userDao = new UserDao(); userDao.update(key, value); return "success"; } public String get() { System.out.println("I am reading Book about Linux"); UserDao userDao = new UserDao(); String name = userDao.get(key); System.out.println(name); return "success"; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
定义了CURD四个方法,全部方法都采用
http://localhost:8080/Struts/user!add?key="color"&value="bule"
http://localhost:8080/Struts/user!delete?key="null"
http://localhost:8080/Struts/user!update?key="color"&value="red"
http://localhost:8080/Struts/user!update?key="color"
这种模式进行访问,这样能够省去不少代码,固然你也能够有其余的方式,好比动态方法调用,那样须要修改局部的hr-user.xml的代码
如今我配置给你们看 http://localhost:8080/Struts/user_add?key="color"&value="bule" http://localhost:8080/Struts/user_delete?key="null" http://localhost:8080/Struts/user_update?key="color"&value="red" http://localhost:8080/Struts/user_update?key="color"
hr-user.xml的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="hr" extends="struts-default" namespace="/"> <action name="user_*" class="org.heinrich.action.UserAction" method="{1}"> <result name="success">/success.jsp</result> </action> </package> </struts>
其实还有不少中配置方式,最简单的方式就是
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="hr" extends="struts-default" namespace="/"> <action name="add" class="org.heinrich.action.UserAction" method="add"> <result name="success">/success.jsp</result> </action> <action name="delete" class="org.heinrich.action.UserAction" method="delete"> <result name="success">/success.jsp</result> </action> <action name="update" class="org.heinrich.action.UserAction" method="update"> <result name="success">/success.jsp</result> </action> <action name="get" class="org.heinrich.action.UserAction" method="get"> <result name="success">/success.jsp</result> </action> </package> </struts>
请求URL是
http://localhost:8080/Struts/add?key="color"&value="bule" http://localhost:8080/Struts/delete?key="null" http://localhost:8080/Struts/update?key="color"&value="red" http://localhost:8080/Struts/update?key="color"
这个看起来是否是不少,可是他是最简单的,每一个方法对应一个action,因此说公司不少都是用第一种或者第二种
下次我会写关于Struts2怎么接收请求参数,和获取Request的几个方式,和Struts2底层运行的状况,带你们Review一次源代码,
求关注,求转发