zoukankan      html  css  js  c++  java
  • struts2之配置文件struts.xml详解

    struts配置文件 struts.xml配置参数详解

    struts.xml中很大一部分配置默认配置就好了 但是有些还是需要做了解  以便于理解 和修改

    <?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配置文件 但由于只加载一个 所以通过此方式-->
    <include file="struts2.xml"/>
    <!--struts与spring整合中将对象工厂交给spring管理 默认无--> <constant name="struts.objectFactory" value="spring" /> <!-- struts 拦截请求后缀为.action 修改此值可以改变访问路径--> <constant name="struts.action.extension" value="action" /> <!-- 开启开发模式 在页面会有一个小按钮 点击可以查看struts的存值栈中各个值 包括自己后台添加的 和内置的--> <constant name="struts.devMode" value="true" /> <!-- 修改struts配置否自动加载 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 文件上传最大值 --> <constant name="struts.multipart.maxSize" value="104857600" /> <package name="dabai" namespace="/" extends="struts-default">    </package> </struts>

    package:通常一个人配置的时候没什么用当多人配置 时 各个xml不在同一文件然后action还可能同名的情况下package就显得格外重要了。必须配置 且多个xml时不能重复

    namespace:/ 代表根目录  /test 代表test目录下  通常配置 /   前台访问action的路径 可各种添加路径随自己先换  但访问时也需要修改不然就404 了  如 /a   访问时就访问  /a/*.action。  必许配置

    extends:通常我们都会配置成struts-default  这包含了struts提供的很多功能 如默认的二十多个拦截器和上传文件功能等,也可以继承自己配置的 如果不闲麻烦的话  必须配置

      

    如上图所示 按照struts的约束 进行依次说明

    result-type  :通常在action中配置  见<action>

    <inteceptors> 在此标签中按约束配置

      <interceptor>自定义拦截器 :属性name:自定义在栈中引用  class:完整的类名

        <interceptor-stack>拦截器栈 :属性: name:自定义 在 设置需要拦截器栈的地方应用

          <interceptor-ref>拦截器引用  :属性 name: 引用<interceptor>中配置的name   但切记最后加上对struts默认拦截器的引用 (把自定义的配置在前  至于配置在后貌似没有什么关系  可能有但我没发现)

             <param>参数:一个拦截器引用可配置多个用于设置不同值  如excludeMethod

          <interceptor-ref>

                  <interceptor-stack>

          <interceptor>

    <inteceptors>

    <default-action-ref>  属性:name :引用拦截器栈 即struts默认的二十几个拦截器加上自定义的

    <global-result>

      <result> 属性: name: 对应 action返回的字符串  此处时所有action返回 对应的name 优先匹配 class:类名:完整类名  若交由spring管理objectFactory除外  type; dispatcher(默认)。redirect,redirection(转到Action中   但是会丢失传递的参数   ,chain 转到Action携带参数  stream 不常用  用于文件下载

    <global-exeption-mappings>由名字可见  可以配置多个exception-mapping

      <exception-mapping> 属性:result:响应结果  只可以是全局result中的某个  exception:如java.lang.exception  

    <global-exeption-mappings>

    <action> 属性: name:访问的类名映射  class  action对应的完整类名  由spring管理时可以写成bean的name  method:可选项 为便于配置 通常将action的name配置成 xxAction_*  method配置成{1}代表匹配xxAction_后面内容与方法第一个匹配上的方法

      <result> 属性:name :acton返回的字符串处理  

    <action

     

    struts.xml 提供参照

    <?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>
    
        <constant name="struts.objectFactory" value="spring" />
        <package name="dabi1" namespace="/" extends="struts-default">
            <!-- 注册拦截器 拦截器栈 设置默认拦截器 -->
            <interceptors>
                <interceptor name="loginInterceptor"
                    class="com.dabai.mall.filter.LoginInterceptor"></interceptor>
                <interceptor-stack name="loginStack">
                    <interceptor-ref name="loginInterceptor">
                        <param name="excludeMethods">login,register,read</param>
                        
                    </interceptor-ref>
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
            </interceptors>
              <default-action-ref name="loginStack"></default-action-ref> 
            <!-- 设置全局结果集 -->
            <global-results>
                <result name="toLogin" type="redirect">/login.jsp</result>
            </global-results>
            <global-exception-mappings>
                <exception-mapping result="asd" exception="asd">/error.jsp
                </exception-mapping>
            </global-exception-mappings>
            <!-- 由于配置了struts.object value=spring -->
            <action name="userAction_*" class="userAction" method="{1}">
                <result name="login">/login.jsp</result>
            </action>
            <action name="productAction_*" class="productAction" method="{1}">
                <result name="addProduct">/addProduct.jsp</result>
                <result name="productList">/productList.jsp</result>
            </action>
            <action name="orderAction_*" class="orderAction" method="{1}">
                <result name="addOrder">/orderList.jsp</result>
            </action>
            <action name="categoryAction_*" class="categoryAction" method="{1}">
            
            </action>
            <action name="orderItemAction_*" class="orderItemAction" method="{1}">
            
            </action>
            
        </package>
    </struts>
  • 相关阅读:
    javascript package
    javascript 的forEach函数
    JavaScript 缓存函数
    javascript String
    javascript的动态this与动态绑定
    CSS Position Fixed for IE6
    raphaeljs图形库学习笔记
    javascript wrapper函数
    javascript框架之继承机制3
    javascript 超级字符串对象
  • 原文地址:https://www.cnblogs.com/notably/p/10503169.html
Copyright © 2011-2022 走看看