zoukankan      html  css  js  c++  java
  • 1-struts2框架基配置

    目录:


    MVC模式:

    struts2 是基于MVC模式的web层的开发框架,是对servlet技术的封装。

      M: Model模型。使用javabean技术, 对业务数据的封装。

      V: View视图。 使用jsp技术,对业务数据的显示。

      C: Control控制器。使用servlet技术, 1)接收请求, 获取用户参数; 2)调用业务逻辑处理方法; 3)获取结果,把结果显示到视图中。

    ——》优化MVC的web层代码, 一个项目对应一个servlet类

     

     struts2的文件夹结构

    • apps文件夹:存放官方提供的示例程序。
    • docs文件夹:存放官方文档。
    • lib文件夹: 核心类库,以及第三方插件。
    • src文件夹:对于源码。

    struts2使用步骤

    • 导入struts的支持jar包

    • 配置启动的全局过滤器(filter)-----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"
        id="WebApp_ID" version="3.0">
        <display-name>structs_2018</display-name>
    
        <!-- 配置structs2的全局过滤器 -->
        <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>
    
        <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>
    </web-app>
    web.xml
    • 编写一个action类,实现业务方法。
    package action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloAction extends ActionSupport{
    
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            return SUCCESS;
        }
    }
    View Code
    • 新建一个struts.xml(在src根目录下),配置Action对象
    <?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 name="default" namespace="/" extends="struts-default">
                <action name="hello" class="action.HelloAction">
                    <result name="success" type="redirect">/success.jsp</result>
                </action>
            </package>
            <!-- 感叹号可以使用 -->
            <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    </struts>
    struts.xml
    •  访问Action业务:

      http://localhost:8080/项目名/hello.action

    struts2系统架构及执行流程

    系统架构

    1. 当web容器收到一个请求时,他将请求传递给一个标准过滤器链,其中包括ActionContextCleanUp及其他过滤器。
    2. 接下来需要调用FilterDispatcher,它将调用ActionMapper来确定调用哪个action,ActionMapper返回一个收集了Action详细信息的ActionMapper对象。
    3. 接下来,FilterDispatcher将控制权委派给ActionProxy, ActionProxy调用配置管理器从配置文件中读取配置信息,然后创建ActionInvocation对象。实际上,ActionInvocation的处理过程就是Struts2处理请求的过程。ActionInvocation被创建的同时,填充了所需要的所有对象和信息,他在调用action对象之前会依次调用所有拦截器。
    4. 一旦action返回结果字符串,ActionInvocation负责查找结果字符串对于的Result,然后执行这个Result(一般为jsp来呈现页面)。
    5. 之后拦截器会再次执行(与之前action执行之前的顺序相反),最后响应被返回给在web.xml中配置的那些过滤器。
    • 其中FilterDispatcher:struts2的前端控制器,是struts2的调度中心,只需在web.xml配置依次即可。
          <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>

    运行流程:

    项目启动:

    • 创建核心过滤器, strutsPrepareAndExecuteFilter对象
    • 执行核心过滤器的init方法:

        依次读取以下信息:

            struts-default.xml

            struts-plugin.xml

            struts.xml

    访问资源:

    • 在内存中查询相应的Action配置,得到class内容,创建Action对象
    • 读取Action配置的method内容,执行Action对象的对应方法。

    struts2.xml配置

    • 电脑无网络时,编程过程中显示注释,配置过程如下:

        首先找到struts2压缩文件中的struts-2.5.dtd文件(可复制到其他地方),按下图add添加 。

          key=-//Apache Software Foundation//DTD Struts Configuration 2.5//EN, 

          keyType=public ID

          location = dtd复制的地址,之后重启MyEclipse.

    • 有网络的时候:
      <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
              "http://struts.apache.org/dtds/struts-2.5.dtd">
  • 相关阅读:
    .NET Core 服务调用 RPC
    从Docker 到 Kubernatetes 的跃迁之路
    同步异步-多线程梳理
    Net的微服务选型之路
    Visual Studio 2019安装SSIS
    HL7协议的基本语法
    vue学习笔记
    开发常用的部分sql语句总结
    VSPD虚拟串口来调试通信接口程序
    SSRS报表工具之合并行数据
  • 原文地址:https://www.cnblogs.com/clairexxx/p/9837190.html
Copyright © 2011-2022 走看看