Struts2基础(一)

Struts2
      1.MVC模型 :
            Model:模型 JavaBean 传递数据
            view: 视图 html jsp 展示数据
            Controller:控制器 Servlet action 处理请求

2.Struts2
      struts2是流行和成熟的基于MVC设计模式的WEB应用框架
      作用:
      取代servlet,减少web开发的时间

3.入门案例
      1.创建一个WEB项目
      2.添加依赖
      3.创建控制器HelloAction.java
      4.创建struts.xml
      5.创建页面hello.jsp
      6.在web.xml启动struts2

在这个里面的web中的配置文件中主要包括过滤器的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <!--过滤器-->
  <filter>
    <!--取名-->
    <filter-name>struts2</filter-name>
    <!--过滤器的全类限定名-->
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <!--对应的名称-->
    <filter-name>struts2</filter-name>
    <!--要拦截的目标-->
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts2的配置文件

<?xml  version="1.0" encoding="UTF-8"  ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <!--
           package:对action进行分包
            name:给包取名字,区分多个包+
            namespace:action名称的前缀
            extends:继承  struts-default
    -->
    <package name="default" namespace="/" extends="struts-default">
        <!--
            配置action
                name  action的名称,请求的路径
                class:控制器的全类限定名
        -->
        <action name="hello" class="com.whpu.k16035.action.HelloAction">
            <!--
                返回值处理
                    name    匹配控制器的返回值
                    /jsp/hello.jsp  :跳转的路径
            -->
            <result name="success">/jsp/hello.jsp</result>
        </action>
    </package>

</struts>

整个strut2的执行流程
在这里插入图片描述
在web.xml中元素执行的元素的顺序:
     1、Listener(监听器)  -->
     2、filter(过滤器)  -->
     3、struts拦截器(不是 struts2)  -->
     4、servlet服务器

猜你喜欢

转载自blog.csdn.net/qq_43479839/article/details/92990399