zoukankan      html  css  js  c++  java
  • Struts2 配置文件

     

     

    本章将带你通过一个Struts2应用程序所需的基本配置。在这里,我们将看到在一些重要的配置文件,将配置文件:web.xml ,struts.xml,struts-config.xml和struts.properties

    使用web.xml和struts.xml的配置文件,并在前面的章节中,已经看到我们的例子中曾使用这两个文件,让我解释以及其他文件。

    web.xml 文件:

    web.xml配置文件是一个J2EE的配置文件,决定如何处理元素的HTTP请求由servlet容器。严格来说它不是一个Struts2的配置文件,但它是Struts2的工作需要进行配置的文件。

    如前所述,这个文件为任何Web应用程序提供了一个切入点。 Struts2 应用程序的入口点,将是一个部署描述符(web.xml)中定义的过滤器。因此,我们将定义在web.xml中的FilterDispatcher是类的项。需要创建的文件夹的WebContent/ WEB-INF下web.xml文件。

    这是第一个配置文件,将需要配置,如果没有一个模板或工具,可生成(如Eclipse或Maven2的)的帮助下开始。以下是web.xml文件中的内容,我们用我们的最后一个例子。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://java.sun.com/xml/ns/javaee" 
       xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       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">
       
       <display-name>Struts 2</display-name>
       <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
       
       <filter>
          <filter-name>struts2</filter-name>
          <filter-class>
             org.apache.struts2.dispatcher.FilterDispatcher
          </filter-class>
       </filter>
    
       <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
       </filter-mapping>
    
    </web-app>

    请注意,我们Struts 2的过滤器映射为/*, /*.action这意味着所有的URL将被解析struts的过滤器。我们将覆盖时,我们将通过“注释”一章。

    struts.xml 文件:

    struts.xml文件中包含的配置信息,将为动作开发被修改。这个文件可以被用来覆盖默认设置的应用程序,例如struts.devMode=false 和其他设置中定义的属性文件。这个文件可以被文件夹WEB-INF/classes下创建 

    让我们来看看在我们struts.xml文件中创建的Hello World的例子在前面的章节中解释。

    <?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>
       <constant name="struts.devMode" value="true" />
       <package name="helloworld" extends="struts-default">
         
          <action name="hello" 
                class="com.yiibai.struts2.HelloWorldAction" 
                method="execute">
                <result name="success">/HelloWorld.jsp</result>
          </action>
          <-- more actions can be listed here -->
    
       </package>
       <-- more packages can be listed here -->
    
    </struts>

    首先要注意的是DOCTYPE。所有的Struts配置文件需要有正确的doctype所示,我们的小例子。 <struts>根标签的元素,我们声明不同的包使用<package>标签。 <package>允许分离和模块化的配置。这是非常有用的,当有一个大项目,项目被划分成不同的模块。

    也就是说,如果项目有三个域 - business_applicaiton ,customer_application 和 staff_application,可以创建三个包和存储相关的动作,在适当的包。包装标签具有以下属性:

    属性描述
    name (required) The unique identifier for the package
    extends Which package does this package extend from? By default, we use struts-default as the base package.
    abstract If marked true, the package is not available for end user consumption.
    namesapce Unique namespace for the actions

    随着name和value属性恒定的标签将被用于覆盖default.properties中定义以下属性,就像我们刚刚设置struts.devMode属性。 Settingstruts.devMode属性可以让我们看到更多的调试消息,在日志文件中。

    我们定义动作标记对应的每一个URL,我们要访问,我们定义了一个类的execute()方法,将访问时,我们将访问相应的URL。

    结果决定得到执行动作后返回给浏览器。从操作返回的字符串应该是一个结果的名称。以上,或者作为一个“global”的结果,可包中的每一个动作,结果被配置每次动作。结果有可选的名称和类型属性。默认名称的值是“success”。

    随着时间的推移,struts.xml文件可以逐步扩展,打破它包是模块化的方式之一,但Struts提供了另一种模块化struts.xml文件。可以将文件分割为多个XML文件,并以下列方式将它们导入。

    <?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>
         <include file="my-struts1.xml"/>
         <include file="my-struts2.xml"/>
    </struts>

    其他的配置文件,我们还没有涉及到在struts-default.xml中。这个文件包含了Struts的标准配置设置,就不必去触摸项目的这些99.99%设置。出于这个原因,我们不打算对这个文件介绍太多。如果有兴趣,不妨看看到struts2的核心2.2.3.jar文件default.properties文件。

    struts-config.xml 文件:

    在struts-config.xml 配置文件是在Web客户端组件的视图和模型之间的链接,但99.99%不会有触碰这些设置在项目中。基本配置文件包含以下主要内容:

    SN拦截 & 描述
    1 struts-config
    This is the root node of the configuration file.
    2 form-beans
    This is where you map your ActionForm subclass to a name. You use this name as an alias for your ActionForm throughout the rest of the struts-config.xml file, and even on your JSP pages.
    3 global forwards
    This section maps a page on your webapp to a name. You can use this name to refer to the actual page. This avoids hardcoding URLs on your web pages.
    4 action-mappings
    This is where you declare form handlers and they are also known as action mappings.
    5 controller
    This section configures Struts internals and rarely used in practical situations.
    6 plug-in
    This section tells Struts where to find your properties files, which contain prompts and error messages

    下面是示例struts-config.xml文件:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE struts-config PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
    "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
    
    <struts-config>
    
       <!-- ========== Form Bean Definitions ============ -->
       <form-beans>
          <form-bean name="login" type="test.struts.LoginForm" />
       </form-beans>
    
       <!-- ========== Global Forward Definitions ========= -->
       <global-forwards>
       </global-forwards>
    
       <!-- ========== Action Mapping Definitions ======== -->
       <action-mappings>
          <action
             path="/login"
             type="test.struts.LoginAction" >
    
             <forward name="valid" path="/jsp/MainMenu.jsp" />
             <forward name="invalid" path="/jsp/LoginView.jsp" />
          </action>
       </action-mappings>
    
       <!-- ========== Controller Definitions ======== -->
       <controller 
          contentType="text/html;charset=UTF-8"
          debug="3"
          maxFileSize="1.618M"
          locale="true"
          nocache="true"/>
    
    </struts-config>

    struts-config.xml文件的更多详细信息,请查看 Struts 文档。

    struts.properties 文件

    此配置文件提供了一种机制来改变框架的默认行为。 struts.properties配置文件内包含的属性其实也可以被配置在web.xml中使用init-param中,以及在struts.xml的配置文件中使用恒定的标签。但如果喜欢保持独立和特定Struts,那么可以创建这个文件的文件夹下的WEB-INF/classes。

    在这个文件中配置的值将覆盖默认值配置default.properties这是包含在struts2-core-x.y.z.jar 分布。有几个的属性,可能会考虑改变使用struts.properties文件:

    ### When set to true, Struts will act much more friendly for developers
    struts.devMode = true
    
    ### Enables reloading of internationalization files
    struts.i18n.reload = true
    
    ### Enables reloading of XML configuration files
    struts.configuration.xml.reload = true
    
    ### Sets the port that the server is run on
    struts.url.http.port = 8080

    这里井号(#)开头的行会被假定作为注释,它将被Struts 2忽略。

  • 相关阅读:
    Linux命令应用大词典-第11章 Shell编程
    Kubernetes 学习12 kubernetes 存储卷
    linux dd命令
    Kubernetes 学习11 kubernetes ingress及ingress controller
    Kubernetes 学习10 Service资源
    Kubernetes 学习9 Pod控制器
    Kubernetes 学习8 Pod控制器
    Kubernetes 学习7 Pod控制器应用进阶2
    Kubernetes 学习6 Pod控制器应用进阶
    Kubernetes 学习5 kubernetes资源清单定义入门
  • 原文地址:https://www.cnblogs.com/Jeely/p/11437991.html
Copyright © 2011-2022 走看看