zoukankan      html  css  js  c++  java
  • (2)struts2配置祥解

    struts工作流程

    反射 :

     

    1.构造对象使用构造器 

    //类似为Servlet
    public class AddAction {
        
        public AddAction(){
            System.out.println("框架反射创建AddAction实例");
        }

     2.web.xml

    2.4版本的servlet规范在部属描述符中新增加了一个<dispatcher>元素,这个元素有四个可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,

    可以在一个<filter-mapping>元素中加入任意数目的<dispatcher>,使得filter将会作用于

    直接从客户端过来的request,通过forward过来的request,通过include过来的request,通过<error-page>过来的request。

    如果没有指定任何< dispatcher >元素,默认值是REQUEST。

    <!-- Struts2核心过滤器,专用于过滤所有请求-->
      <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>
          <dispatcher>REQUEST</dispatcher>
      </filter-mapping>
    </web-app>

    由于AddAction中是转发所以不经过StrutsPrepareAndExecuteFilter。要使其经过改成dispatcher为FORWARD

    *四)struts2其本配置详解(上)

    (一)名称空间和访问Action路径规则

    访问URL=名称空间 / <aciton标签的name属性值>

    名称空间:页面名相同时,用namespace区分请求路径
    (二)默认名称空间和测试Action的访问路径规则

    默认命名空间是"/",可不写;
    若名称空间是/,url=http:localhost:8080/day31/add

    http://localhost:8080/day31/add?num1=100&num2=200

    若名称空间是/xx,url=http://localhost:8080/day31/xx/add

     http://localhost:8080/day31/xx/add?num1=100&num2=200

    为什么需要namespace?

    如果名称空间是/,且<action>的name属性值为add
    如果URL是:http://127.0.0.1:8080/day31/xx/yy/zz/add回车

    http://127.0.0.1:8080/day31/xx/yy/zz/add(正确)
    http://127.0.0.1:8080/day31/xx/yy/add(正确)
    http://127.0.0.1:8080/day31/xx/add(正确)
    http://127.0.0.1:8080/day31/add(正确)

    如果名称空间是/xx,且<action>的name属性值为add
    如果URL是:http://127.0.0.1:8080/day31/xx/yy/zz/add回车

    http://127.0.0.1:8080/day31/xx/yy/zz/add(正确)
    http://127.0.0.1:8080/day31/xx/yy/add(正确)
    http://127.0.0.1:8080/day31/xx/add(正确)
    http://127.0.0.1:8080/day31/add(出错)

    结论:不管URL请求是什么,namespace的属性值,一定名称空间的底线,不能少于namespace的值,
    但可以多于namespace的值。
    路径中,大小写敏感

    (3)如果没有指定<action>的method属性
    默认执行Action中的execute()方法

    (4)推荐,文件只存在加载和解析,没有编译,所以尽量在xml中配置

    execute()方法使其return "success";

    //转发到add.jsp中  由struts.xml中的result代替    
    //request.getRequestDispatcher("/add.jsp").forward(request, response); return "success";

    web.xml

    <!-- struts2的核心配置文件,在应用部署时加载并解析 -->
    <struts>
        <package name="base" extends="struts-default" namespace="/"><!-- struts2内部的一个核心包 -->
            <action name="add" class="cn.itcast.web.struts2.add.AddAction" >
                <result name="success" type="dispatcher">
                    /add.jsp
                </result>
            </action>
        </package>
    </struts>  

    如果没有指定<result>的name属性
    name默认为Action中execute()方法的返回值:小写字母"success"

    (5)如果没有指定<result>的type属性
    type默认为"dispatcher"
    struts2专用转发

    (6)设置访问Action的扩展名的二种设置方式【struts.properties和struts.xml】
    struts.action.extension=action,,
    struts2访问Action的扩展名默认为:无或者是.action(小写)
    修改Actin访问的扩展名:
    A)src/struts.xml文件配置扩展名(value中四种)

    <!-- 修改action的扩展名方式1 -->
    <constant name="struts.action.extension" value="xx,qq,aciton,,"></constant>

      http://localhost:8080/day31/add.qq?num1=100&num2=100

    B)src/struts.properties文件配置扩展名

    struts.action.extension=yy,do
    struts.i18n.encoding=UTF-8


    上述二种方式,最终都会替换框架默认的扩展名
    加载的顺序是:先加载框架的,后加载程序员的
    当properties和xml文件同时存在时,properties文件起决定作用。

    *四)struts2其本配置详解(下)

    (7)指定包含多个struts.xml配置文件的规则
    src/struts.xml是总的配置文件,用总的配置文件能过<include>标签,去包含子的配置文件,
    便用多个模块操作,一个模块使用一个xml文件。

    get.jsp

    <a href="/day31/get" style="text-decoration:none">
            这是GET请求    
    </a>  

    GetAction

    package cn.itcast.web.struts2.add;
    
    public class GetAction {
        public String execute(){
            return "success";
        }
    }

    WEB-INF下ok.jsp

    <body>
        ok.jsp    
    </body>

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <package name="get" extends="struts-default" namespace="/">
            <action 
                name="get" 
                class="cn.itcast.web.struts2.add.GetAction" 
                method="execute">
                <result name="success" type="dispatcher">
                    /WEB-INF/ok.jsp
                </result>        
            </action>        
        </package>    
    </struts>   

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <package name="add" extends="struts-default" namespace="/">
            <action name="add" class="cn.itcast.web.struts2.add.AddAction">
                <result name="success" type="redirect">
                    /add.jsp
                </result>
            </action>
        </package>    
    </struts>  

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <!-- struts2的核心配置文件,在应用部署时加载并解析 -->
    <struts>
        <!-- 包含子的xml文件 -->
        <include file="cn/itcast/web/struts2/add/struts_add.xml"/>
        <include file="cn/itcast/web/struts2/get/struts_get.xml"/>
    </struts>  

    (8)Action采用单例还是非单例模式,需要解决线程安全问题吗?
    一次请求创建一个Action实例,每次都不同。无线程安全问题

    在AddAction中private Integer age;在execute中age++;不会有线程安全问题、

    在平凡中坚持前行,总有一天,会遇见优秀的自己
  • 相关阅读:
    php解析.csv文件
    sublime text3 输入中文的解决方法
    git 的使用
    yii2.0 框架邮件的发送
    yii2.0的分页和排序
    php上传图片文件常用的几个方法
    在yii框架中如何连接数据库mongodb
    yii框架中验证器声明一组内置验证器可以使用短名称引用
    yii中的cookie的发送和读取
    ExpressionToSQL
  • 原文地址:https://www.cnblogs.com/mao-19/p/5706231.html
Copyright © 2011-2022 走看看