• Struts2(一)


    1.简介

      Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。

    2.执行过程

    A.创建过滤器(StrutsPrepareAndExecuteFilter) - 每次请求都执行过滤
    1.执行init方法,加载xml文件struts-default.xml,struts-plugin.xml,struts.xml
    B.用户访问struts.xml - 执行核心过滤器doFilter()方法
    1.创建ActionContext封装数据
    把表示request/session/application等的map都放到值栈中.
    把值栈数据,拷贝一份给ActionContext
    2.struts2把请求封装为一个ActionMapping对象
    如果访问的不是action资源,ActionMapping为空,放行!
    否则执行Action对象创建以及调用默认拦截器
    通过配置管理器对象ConfigurationManager, 创建Action的代理对象此时action对象创建!
    创建完Action之后,通过DefaultActionInvocation对象,旧版本依次调用18个拦截器,Struts2的2.5版本是19个
    3.然后执行action的execute()方法
    4.执行完execute()方法,又回到拦截器,执行invoke()放行之后的代码!

    3.简单示例

    A.新建web项目

    B.导包Struts2-2.5的包(若遇到类似index方法没有定义的问题,可删除Struts-rest的jar包)

      可到Apache官网下载jar包

    C.在web.xml文件中添加Struts2的filter

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3   <display-name>struts0</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   <!-- 配置Struts2的过滤器 -->
    13   <filter>
    14       <filter-name>struts</filter-name>
    15       <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    16   </filter>
    17   <filter-mapping>
    18       <filter-name>struts</filter-name>
    19       <url-pattern>/*</url-pattern>
    20   </filter-mapping>
    21 </web-app>

    1)通知web服务器(tomcat),只要是/* 的请求路径,都交由该Filter来处理.

    2)通过解析和反射struts2的核心Filter,实例化后,加载struts2的相关配置文件,如struts.xml

    将struts.xml文件解析后,在内存中形成一个JavaBean对象,每次访问不需要重新加载,直接访问内存。

    当重新部署时会重新加载struts.xml形成新的JavaBean对象.

     D.写Action类及其方法

    1 package com.rong.action;
    2 
    3 public class OneAction {
    4     public String index() {
    5         System.out.println("toOne Action!");
    6         return "success";
    7     }
    8 }
     1 package com.rong.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class TwoAction extends ActionSupport {
     6     private static final long serialVersionUID = -741546782668382103L;
     7 
     8     public String index() throws Exception {
     9         System.out.println("TwoeAction index");
    10         return "two";
    11     }
    12 }
     1 package com.rong.action;
     2 
     3 import com.opensymphony.xwork2.Action;
     4 
     5 public class ThreeAction implements Action {
     6 
     7     @Override
     8     public String execute() throws Exception {
     9         System.out.println("ThreeAction execute!");
    10         return "SUCCESS";
    11     }
    12 }

    E.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 name="default" namespace="/" extends="struts-default">
            <action name="one" method="index" class="com.rong.action.OneAction">
                <result name="success">/one.jsp</result>
            </action>
            
            <action name="two" class="com.rong.action.TwoAction" method="index">
                <result name="two">/two.jsp</result>
            </action>
            
            <action name="three" class="com.rong.action.ThreeAction">
                <result name="SUCCESS">/three.jsp</result>
            </action>
        </package>
    </struts>

    访问路径为:

    http://localhost:8080/struts0/one

    http://localhost:8080/struts0/two

    http://localhost:8080/struts0/three

    访问Action的规则:

    规则:<package>中namespace属性值拼接上<action>中name标签,拼接符为"/"

    namespace默认为"/",不对此属性赋值也可以。

    两种方式访问Action

      方法1,使用.action扩展名   例如:http://localhost:8080/struts0/one.action

     方法2,不使用扩展名           例如:http://localhost:8080/struts0/one

    两种方式访问效果一样。还可以更改默认扩展名。略。

    <package>标签的属性含义:

    name="default"------包名

    namespace="/" ------访问的空间,该命名空间影响到url的地址

    extends="struts-default"------扩展strust2的内部功能,必须是struts-defaul,默认情况下都要经过18拦截器

    <action>标签的属性含义:

    name="two"------执行路径

    class="com.rong.action.TwoAction"------使用全路径便于反射

    method="execute"------需要执行的业务方法,也是通过反射

    旧版本不写method就执行默认的execute方法,新版本是index方法!!!

    注意:struts2建议将相关的action类放入同一个package下,如CRUD操作,类似于java中的包和类的关系

    <action>中不指定class属性会是什么结果?

    如果不指定class值,那么会返回404状态值。同时可以看到class属性的默认值为"com.opensymphony.xwork2.ActionSupport"

    不写class属性值,要想返回正确的结果,必须在<action>加上result属性,其值必须为"success"。

    另外,需要将excute方法的返回值更改为"success"

    继承 ActionSupport类后,method默认为excute(),返回值为"success"

    <result>中不指定type属性会是什么结果?

    重新发布项目,依然是成功,由此可知,type默认为"dispatcher",即默认为转发

     如果要使用重定向,必须将type赋值为redirect,即type="redirect"

    action使用非单例模式,不需要解决线程安全问题

    一次请求对应一次Action实例,因此不会产生线程安全问题,即在Action中不会产生synchronized同步代码块。

  • 相关阅读:
    数位dp
    可持久化Trie
    网络流
    欧拉定理
    点、边双,圆方树
    [USACO5.3]窗体面积Window Area
    6.2三道模拟
    BZOJ2054 疯狂的馒头
    [USACO5.1]夜空繁星Starry Night
    [USACO5.1]乐曲主题Musical Themes
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/8152678.html
Copyright © 2020-2023  润新知