1 什么是Struts2框架

  基于MVC设计模式的web应用框架

  Struts2框架是一个轻量级的MVC流程框架

    轻量级是指程序的代码不是很多,运行时占用的资源不是很多,MVC流程框架就是说它是支持分层开发,控制数据的流程,从哪里来,到那里去,怎么来,怎么去的这样一个框架;  

  Struts1 和 Struts2 没有任何关系
  Struts2的前身是WebWork
  Struts1/Struts2/SpringMVC都是MVC设计模式的表现层框架【SpringMVC现在最流行】

 

2 如何使用Struts2框架

  2.1 导包 Struts2-core

  2.2 配置主控制器,在web.xml中进行配置(注意:这里的主控制器相当于SpringMVC的前端控制器)

  2.3 配置struts.xml配置文件(注意:这里的配置文件名不能进行更改)

 

3 案例:使用Struts2表现框架实现一个helloworld案例

  3.1 案例效果

    在浏览器输入:http://localhost:8080/ssh01/demo/hello

    服务器返回的结果是:

      Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

  3.2 配置主控制器    

    Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

      注意:主控制器类在maven中的位置

        Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

  3.3 配置struts.xml配置文件

    3.3.1 手动创建一个struts.xml文件

      》创建一个xml文件,文件名为struts

      》打开默认的struts.xml文件

        Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

      》将默认struts.xml文件中24——26中内容复制到自己创建的struts.xml文件中

        Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

      》在自己创建的struts.xml文件中添加一个<struts></struts>标签

     3.3.2 配置struts.xml文件

      》配置请求路径

        通过 package 标签实现

          name属性:随便写个名字就行,无什么实际意义

          namespace属性:请求路径

          extends属性:继承默认的struts.xml配置文件

      》配置具体请求名

        通过 action 标签实现

          name属性:具体的请求名(注意:会默认在后面添加 .action ,所以访问时使用 hello 和 hello.action 都可以)

          class属性:请求控制类

      》配置返回的结果

        通过 result 标签实现

          name属性:该属性值必须和控制类中execute方法的返回值保持一致

       Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

  3.4 编写控制类

    该类中必须有一个execute方法,该方法是用来处理请求的

    Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>cn.xiangxu</groupId>
 4   <artifactId>ssh01</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   <packaging>war</packaging>
 7   <dependencies>
 8       <dependency>
 9           <groupId>org.apache.struts</groupId>
10           <artifactId>struts2-core</artifactId>
11           <version>2.3.8</version>
12       </dependency>
13   </dependencies>
14 </project>
pom.xml (maven依赖)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2022-01-18
  • 2021-07-24
  • 2021-09-19
  • 2021-06-23
猜你喜欢
  • 2022-12-23
  • 2021-08-28
  • 2021-05-08
  • 2021-12-23
  • 2022-12-23
相关资源
相似解决方案