• JavaWeb学习:Struts2入门


    一、Struts2概述

      Struts2:是一个基于MVC设计模式的Web应用框架,它本质相当于一个Servlet,在MVC设计模式中Strust2作为控制器来建立模型与视图的数据交互。

    二、搭建Struts2开发环境

      ①、下载Struts2

        官网struts.apache.org 

           

       ②、解压Struts2开发包

             

        apps:Struts2提供的应用,war文件:web项目打成war包。直接放入到tomcat可以允许。

        docs:Struts2的开发文档和API

        lib:Strtus2框架的开发的jar包

        src :Struts2的源码

      ③、引入jar包

        打开解压后的struts-2.5.xxapps,有两个(struts2-showcase.war、struts2-rest-showcase.war--建议解压)war包,修改后缀war变更为zip,使用解压软件解压后找到WEB-INFlib找到所需jar文件,将jar包copy到eclipse项目中的WebContent/WEB-INF/lib/文件目录下,拷贝完后别忘了把jar添加到Build Path中

           

         

       ④、将必备的web.xml、struts.xml两个配置文件拷贝出来

        还是以struts2-rest-showcase.war文件为参考, 在WEB-INF下web.xml copy到项目的WebContent/WEB-INF下

          

        修改web.xml

          struts2.5过滤器类全名称 :org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

          struts2.3过滤器类全名称 :org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app id="WebApp_9" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
        <display-name>Struts 2 Application</display-name>
        <!-- 配置Struts2的核心过滤器 -->
        <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.jsp</welcome-file>
            <welcome-file>default.jsp</welcome-file>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    
    </web-app>

        在WEB-INF/classes下struts.xml copy到 Java Resources/src/,拷贝之后Libraries

          

        修改strut.xml

    <?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随意,extends自struts-default -->
        <package name="strutsTest" extends="struts-default">
            <!-- 编写action,name为地址栏输入字符串,class为class文件的位置 -->
            <action name="hello" class="com.struts2.demo.HelloAction">
                <!-- name为刚才编写的HelloAction类中execute的返回值,execute返回值为success跳转到success.jsp页面 -->
                <result name="success">success.jsp</result>
            </action>
        </package>
    </struts>

      ⑤、新建一个Action类

    public class HelloAction {
        
        public String execute() throws Exception {
        System.out.println("进入HelloAction的execute方法... ...");
        return "success";
        }
    }

      ⑥、新建一个index.jsp文件,验证是否跳转

        

        index.jsp代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>Hello Strut2</h1>
        <h3><a href="${ pageContext.request.contextPath}/hello.action">执行execute方法</a></h3>
    </body>
    </html>

        success.jsp代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <h3>跳转到execute方法的返回值在struts.xml中配置指定画面... ...</h3>
    </body>
    </html>

      ⑦、启动服务器

         

          结果:

              

          点击执行execute方法连接,会执行HelloAction的execute方法,并通过返回值跳转画面

            

     三、Struts2执行流程

      当用户访问某个Action的时,先经过核心过滤器(在web.xml中配置),在核心过滤器(struts.xml配置要执行struts-default.xml中的配置的拦截器)中执行一组拦截器(struts-default.xml配置拦截器),通过struts.xml配置确定执行目标Action并根据Action的返回值进行页面跳转。

     四、Strut2配置文件加载顺序

      ①、每次访问Action会经过核心过滤器,核心过滤器是在什么时候被创建的呢?

        在StrutsPrepareAndExecuteFilter的init方法中添加断点,Debug启动Tomcat服务,可以看到程序会执行到添加的断点,所以过滤器是服务器启动时创建的。

         

        

      ②、加载顺序

        定位到dispatcher = init.initDispatcher(config);代码,查看initDispatcher(config)方法中init方法

          

          • init_DefaultProperties()                                     ----加载default.properties
          • init_TraditionalXmlConfigurations();                  ----加载struts-default.xml、struts-plugin.xml、struts.xml
          • init_LegacyStrutsProperties();                           ----加载struts.properties
          • init_CustomConfigurationProviders();        ----加载配置提供类
          • init_FilterInitParameters() ;                                 ----加载web.xml中过滤器初始化参数
          • init_AliasStandardObjects() ;                              ----加载Bean对象

          配置文件的加载顺序

          • default.properties
          • struts-default.xml
          • struts-plugin.xml
          • struts.xml
          • struts.properties
          • web.xml
            • 注意:后配置的常量的值会覆盖先配置的常量的值。
  • 相关阅读:
    Lucene学习总结之一:全文检索的基本原理
    Solr学习和总结(线下1)
    HBase学习系列
    Hadoop家族系列文章
    SQL on Hadoop系统的最新进展(1)
    【转】redis数据库入门教程(全面详细)+面试问题
    Redis(1.3)Redis的基本特性(事务、多数据库)
    (5.15)mysql高可用系列——mysql mha实践
    Redis(1.2)Redis的数据结构与基本操作
    mysql函数使用报错
  • 原文地址:https://www.cnblogs.com/WarBlog/p/14006943.html
Copyright © 2020-2023  润新知