Struts2--入门

作者: 常威爆打来福 | 来源:发表于2017-09-19 08:27 被阅读35次

    一 概述
    1 Struts2 框架是用在javaEE三层中web层框架
    2 Struts2框架是在Structs1和webwork基础之上发展全新的框架
    3 Sturuts2解决问题:

    基础操作 Sturts2基本原理

    4 Sturuts2版本
    Sturuts-2.3.24
    5 web层常见框架
    (1)Sturuts2
    (2)springMVC
    二 Sturuts2入门案例
    1 导入jar包

    jar包

    maven配置

    <!-- https://mvnrepository.com/artifact/asm/asm -->
        <dependency>
          <groupId>asm</groupId>
          <artifactId>asm</artifactId>
          <version>3.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/asm/asm-commons -->
        <dependency>
          <groupId>asm</groupId>
          <artifactId>asm-commons</artifactId>
          <version>3.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
          <groupId>org.freemarker</groupId>
          <artifactId>freemarker</artifactId>
          <version>2.3.23</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javassist/javassist -->
        <dependency>
          <groupId>javassist</groupId>
          <artifactId>javassist</artifactId>
          <version>3.11.0.GA</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
          <version>2.8.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-api</artifactId>
          <version>2.8.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ognl/ognl -->
        <dependency>
          <groupId>ognl</groupId>
          <artifactId>ognl</artifactId>
          <version>3.0.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
        <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-core</artifactId>
          <version>2.3.24</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core -->
        <dependency>
          <groupId>org.apache.struts.xwork</groupId>
          <artifactId>xwork-core</artifactId>
          <version>2.3.24</version>
        </dependency>
    

    2 创建action

    package Action;
    
    /**
     * Created by pc on 2017/9/18.
     */
    public class HelloAction {
        /*
        * (1)每次访问servlet时候,都会service方法
        * - 写继承HttpServlet,重写类里面的方法
        * - 在web.xml里面配置servlet访问路径
        * (2)访问action,每次访问action时候,默认执行名称execute
        * - 配置action访问路径
        * */
        public String execute(){
            return "ok";
        }
    }
    

    3 配置action访问路径

    • 创建Struts2核心配置文件

      • 核心配置文件名称和位置是固定的
      • 位置必须在src下面,名称 struts.xml
    • 引入DTD约束

    <?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 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="hellodemo" extends="struts-default" namespace="/">
            <!--name:访问名称-->
            <action name="hello" class="Action.HelloAction">
                <!--配置方法的返回值到页面-->
                <result name="ok">/hello.jsp</result>
            </action>
    
        </package>
    </struts>
    
    • 访问路径

    http://localhost:8080/Struts2/hello.action

    4 配置Struts2过滤器

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>
    

    注释:Class FilterDispatcher Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilterand StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one..即,从Struts 2.1.3起已被标注为过时的,改用StrutsPrepareAndExecuteFilter

    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    

    会出现

    ************************************************************************
    *                                     WARNING!!!                                         *
    *                                                                                                  *   
    *>>> FilterDispatcher <<< is deprecated! Please use the new filters! *
    *          This can be a source of unpredictable problems!                  *         
    *             Please refer to the docs for more details!                         *
    *           http://struts.apache.org/2.x/docs/webxml.html              *      
    *                                                                                         *
    *************************************************************************
    

    运行结果

    运行结果

    注释:在idea编辑器中,路径中不需要项目名称
    三 运行过程图解

    图解

    相关文章

      网友评论

        本文标题:Struts2--入门

        本文链接:https://www.haomeiwen.com/subject/wzfisxtx.html