struts1和struts2都是由Apache组织发布的,但是比较有趣的是struts2和struts1并没有“血缘关系”。在Apache发布struts1之后,当时是还是非常流行的,但是随着时间推荐,这时候struts1暴露的问题就越来越多了,在这个时候opensymphony组织发布了WebWork框架,WebWork框架在当时适应的很好。opensymphony组织由于向外推广的力度不够,使得WebWork不能很快的面向大众。这个时候Apache组织联合opensymphony组织发布了struts2,因此struts2和struts1联系并不大。在struts2刚发布出来的时候,许多的类库都是直接使用struts1的。

2,Struts2和SpringMVC对比

struts2和springMVC结构非常相似,都是基于MVC结构的。就连配置都很相似,关于SpringMVC和Spring的知识可以参见http://www.cnblogs.com/HDK2016/category/918254.html

下面给两种图片,分别是springMVC和struts2的结构流程图。

SpringMVC流程图:

【Struts2】Struts2框架的搭建

struts2流程图:

【Struts2】Struts2框架的搭建

通过这两张图片的对比可以看出。

struts2中的 StrutsPrepareAndExecuteFilter 相当于 SpringMVC中的 DispatcherServlet。

struts2中的 Action 相当于 SpringMVC 中的 Controller

struts2中的 result 相当于 SpringMVC 中的 ViewResovler

 

3,Struts2框架的搭建

首先应该下载jar包,将jar包导入到项目的lib目录下面。

【Struts2】Struts2框架的搭建

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>struts</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>
web.xml

相关文章: