• struts2框架搭建(一)


    struts2是一个基于mvc的web应用框架。struts2本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器层(Controller)来建立模型与视图的数据交互。

    struts2使用优势:自动封装参数、参数校验、结果的处理(转发|重定向)、国际化、显示等待页面、表单防止重复提交等。

    struts2具有更加先进的架构以及思想。

    struts2的历史

      struts2与struts1的区别:struts1是基于servlet开发的,servlet本身就是线程不安全的,所以导致struts1存在很多问题。struts2是基于WebWork架构的完全是另一个架构了。

    搭建struts2框架

      https://mirrors.tuna.tsinghua.edu.cn/apache/struts/2.5.16/struts-2.5.16-min-lib.zip

      1.新建项目 左侧选择java Enterprise 勾选Web Application中的Struts2 默认选择download 自己会下载相关jar包(最好自己下载,idea给下载的会少文件) ->finish。

      2.command+; artifacts -> fix修复问题。

      3.新版本没有web-inf文件夹 解决方法 进入project structure ->facets->点击小加号,添加web.xml 注意路径 项目名/web/WEB-INF/web.xml

      4.在src中新建简单action类 HelloAction

    package com.struts2.web;
    
    public class HelloAction {
        public String hello(){
            System.out.println("hello david");
            return "success";
        }
    }

      5.在核心配置文件src/struts.xml中 配置hello方法

    <?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根元素 -->
    <struts>
        <!--package用于封装多个action
         name:起个名字,不能重复
         namespace:定义命名空间 一级路径 /hello
         extends:继承一个指定包
         abstract:标示属性 是否是抽象类 给人看的专门给包继承的 不能独立运行
         -->
        <package name="hello" namespace="/hello" extends="struts-default">
            <!--action配置action类
             name:访问资源名 二级路径/hello/HelloAction
             class:哪个action类的
             method:action中的哪个方法
             -->
            <action name="HelloAction" class="com.struts2.web.HelloAction" method="hello">
                <!--result结果配置
                 name:结果处理的名称,与action返回值对应
                 type: (默认使用转发)指定哪个result类来处理
                 标签体:页面的相对路径
                 -->
                <result name="success">/index.jsp</result>
            </action>
        </package>
    </struts>

      6.将struts2核心过滤器配置到web.xml

    <?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_3_1.xsd"
             version="3.1">
    
        <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-list>
    
    </web-app>

      7.测试 启动项目

      输入网址 http://localhost:9999/hello/HelloAction 见控制台输出 hello david

    struts2访问流程

      1.浏览器 访问http://localhost:9999/hello/HelloAction 地址

      2.首先会访问web.xml中的过滤器

      3.过滤器会去找 我们的核心配置文件struts.xml

      4.首先拿到/hello去找每个package标签的namespace 看有没有/hello

      5.如果找到了 就继续寻找package下的action 有没有name为HelloAction 的

      6.找到action后 查看class属性 有完整包名 就可以通过反射 创建这个action对象 再去找后面的 method标签中的hello方法 并且调用该方法

      7.调用方法后返回success,用返回值去找result标签 有没有success的结果 如果有 转发到/index.jsp页面

    struts2架构

      1.StrutsPrepareAndExecuteFilter 配置为/* 所有请求都要经过struts这个过滤器

      2.过滤器会去找ActionMapper 处理请求信息 返回ActionMapping对象

      3.获得ActionMapping对象后他回去找ActionProxy代理对象

      4.ActionProxy去找ConfigurationManager配置文件管理器 去读取配置src/struts.xml

      5.ActionProxy有了ActionMapping 又有了配置文件信息 交给了ActionInvocation 来处理后续问题

      6.ActionInvocation处理interceptor集合 Action 后交给Result 去找jsp进行转发 回到浏览器 完成响应

    struts2常量配置

      默认常量配置存储在

        struts-2.5.16/lib/struts2-core-2.5.16.jar!/org/apache/struts2/default.properties

      修改默认常量配置

        1.在src中新建struts.properties 把需要改的常量粘贴过来

        2.(推荐)在struts.xml中 添加<constant name="" value="" /> 来添加需要配置的常量

        3.在web.xml中 添加<context-param><param-name /><param-value /></context-param> 来添加需要配置的常量

      常量配置有哪些

    <?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>
    
        <!-- 指定Web应用的默认编码集,相当于request.setCharacterEncoding() 解决post乱码 -->
        <constant name="struts.i18n.encoding" value="UTF-8" />
    
        <!--设置访问路径 http://localhost:9999/hello/HelloAction.do--> 
       <constant name="struts.action.extension" value="do" />

       <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
       <constant name="struts.serve.static.browserCache" value="false" />

    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
       <constant name="struts.configuration.xml.reload" value="true" />
        <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> 
    <constant name="struts.devMode" value="true" />
      
    </struts>

    动态方法调用

    当一个userAction中有4 5 个方法时,如果静态配置,就需要写4 5 份配置节点。使用动态方法调用只配置一份即可。

      方式1.在struts.xml中开启动态方法,然后action时 不设置method值,其中global-allowed-methods 是解决struts2.55以后版本的 方法授权问题。

    <struts>
        ...
        <!--开启动态方法 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    
        <package name="User" namespace="/User" extends="struts-default">
            <global-allowed-methods>Add,Delete,Update,Get</global-allowed-methods>
            <action name="User" class="com.struts2.web.UserAction">
                <result name="success">/index.jsp</result>
            </action>
        </package>
    </struts>

      然后编写UserAction类的方法

    package com.struts2.web;
    
    
    public class UserAction {
        public String Add(){
            return "success";
        }
    
        public String Get(){
            return "success";
        }
    
        public String Update(){
            return "success";
        }
    
        public String Delete(){
            return "success";
        }
    }

      测试:在浏览器中输入 http://localhost:9999/User/User!Delete  访问规则是/namespace/name!方法名

        此方式不利于seo优化。

      方式2.不需要配置动态方法常量,使用通配符*

    <struts>
        ...
        <package name="User" namespace="/User" extends="struts-default">
            <global-allowed-methods>Add,Delete,Update,Get</global-allowed-methods>
            <action name="User_*" class="com.struts2.web.UserAction" method="{1}">
                <result name="success">/index.jsp</result>
            </action>
        </package>
    </struts>

      测试:在浏览器中输入 http://localhost:9999/User/User_Delete 访问规则就是/namespace/name_方法名

        此方法常用!

    引入多配置文件,include时文件都是相对于 classpath的  要写全路径

      <include file="xml路径"></include>

    默认值

        <package name="hello" namespace="/hello" extends="struts-default">
            <!--method:默认值execute
             result的name:默认值success
             result的type:默认值dispatcher
             class:默认值com.opensymphony.xwork2.ActionSupport
             -->
            <action name="HelloAction" class="com.struts2.web.HelloAction" method="hello">
                <result name="success">/index.jsp</result>
            </action>
        </package>

    action类创建的方式

      方式1.

    //创建一个类,可以是POJO
    //POJO:不用继承任何父类,也不需要实现任何借口
    //使struts2框架的代码侵入性更低,servlet代码侵入性就很高又要实现接口或者继承httpServlet。
    public class UserAction {
        
    }

      方式2.

    import com.opensymphony.xwork2.Action;
    
    //方式2 实现xwork2下的Action接口 不常用
    public class HelloAction  implements Action{
        //接口中有个方法 属于提示方法告诉你怎么写
        @Override
        public String execute() throws Exception {
            return null;
        }
    }

      方式3.

    import com.opensymphony.xwork2.ActionSupport;
    
    //方式3 继承一个类ActionSupport 
    public class HelloAction extends ActionSupport{
        public String hello(){
            System.out.println("hello david");
            return "success";
        }
    }

    继承该类的用处:https://blog.csdn.net/foreverdwyw/article/details/53462866

  • 相关阅读:
    关于BehaviorEditorPart 不显示的问题(出自msdn)
    webpart msdn 的位置
    Visual Studio 2008 具有一些新的报表功能和改进之处
    .NET Framework 类库
    Reporting Service 安装 及相关问题如:授予的权限不足解决办法
    单服务器部署&&双服务器部署
    Windows Server 2003安装完毕后汉字都为乱码“方框”,配置域控制器
    Dreamweaver MX显示汉字为乱码的解决方法
    名称以无效字符开头。处理资源 'http://localhost/发布了的/Default.aspx' 时出错。第 1 行,位置: 2
    【随感】my feeling about Long Ying
  • 原文地址:https://www.cnblogs.com/baidawei/p/9066600.html
Copyright © 2020-2023  润新知