zoukankan      html  css  js  c++  java
  • Struts – Multiple configuration files example

    Many developers like to put all Struts related stuff (action, form) into a single Struts configuration file. It’s fast for the initial development but bad for the future maintenance, and may be those developers are not aware of the Struts is allow multiple configuration files feature.
    6 years ago, I had joined a large Struts development project which involve 20+ modules. Unfortunately, the prior developers put all the Struts related stuff (action, form and etc) into a single Struts configuration file (struts-config.xml). The struts-config.xml just keep growing extremely fast and finally hit 20++mb, every update to this configuration file will take few minutes, and even wait half an hour for a single debugging deployment in Eclipse IDE. This is a serious performance issue and causing the project keep delay the production date. What a good Struts development experience.

    Please split the Struts configuration details into different modules, Struts can do it easily.

    Struts multiple configuration files example

    This is the sample project structure for the demonstration.
    Struts-mutiple-configuration-file

    1. Single module

    A single module support multiple Struts configuration files.

    page1.jsp

    This is Page 1
    

    page2.jsp

    This is Page 2
    

    struts-config-1.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
    "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
    
    <struts-config>
    
    	<action-mappings>
    		
    		<action
    			path="/Page1"
    			type="org.apache.struts.actions.ForwardAction"
    			parameter="/pages/page1.jsp"/>
    
    	</action-mappings>
    
    </struts-config>
    

    struts-config-2.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
    "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
    
    <struts-config>
    
    	<action-mappings>
    		
    		<action
    			path="/Page2"
    			type="org.apache.struts.actions.ForwardAction"
    			parameter="/pages/page2.jsp"/>
    
    	</action-mappings>
    
    </struts-config>
    

    In the web.xml, you can separate multiple Struts configure file by a comma “,“.

    web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Maven Struts Examples</display-name>
      
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
            org.apache.struts.action.ActionServlet
        </servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>
             /WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
        
      <servlet-mapping>
           <servlet-name>action</servlet-name>
           <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    
    </web-app>
    

    Test it

    http://localhost:8080/StrutsExample/Page1.do
    

    It will display the page1.jsp

    http://localhost:8080/StrutsExample/common/Welcome.do
    

    It will display the page2.jsp

    Both Struts configuration are loaded property.

    2. Multiple modules

    Multiple modules, each has own Struts configuration files.

    admin/welcome.jsp

    Welcome to admin page
    

    common/welcome.jsp

    Welcome to common page
    

    Both “struts-config-admin.xml” and “struts-config-admin.xml” files contains the same settings, Struts is able to differential it via the “config” parameter value in web.xml.
    In Struts 2, the “Namespace” is a more efficient way to replace this “config parameter” setting.

    struts-config-admin.xml, struts-config-admin.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
    "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
    
    <struts-config>
    
    	<action-mappings>
    		
    		<action
    			path="/Welcome"
    			type="org.apache.struts.actions.ForwardAction"
    			parameter="/welcome.jsp"/>
    
    	</action-mappings>
    
    </struts-config>
    

    web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Maven Struts Examples</display-name>
      
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
            org.apache.struts.action.ActionServlet
        </servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>
             /WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml
            </param-value>
        </init-param>
        <init-param>
            <param-name>config/admin</param-name>
            <param-value>
             /WEB-INF/struts-config-admin.xml
            </param-value>
        </init-param>
        <init-param>
            <param-name>config/common</param-name>
            <param-value>
             /WEB-INF/struts-config-common.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
        
      <servlet-mapping>
           <servlet-name>action</servlet-name>
           <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    
    </web-app>
    

    Test it

    The “config/admin” will match to this URL pattern – http://localhost:8080/StrutsExample/admin/
    The “config/common” will match to this URL pattern – http://localhost:8080/StrutsExample/common/

        http://localhost:8080/StrutsExample/admin/Welcome.do
        It will display the admin/welcome.jsp
        http://localhost:8080/StrutsExample/common/Welcome.do
        It will display the common/welcome.jsp
    

    Each modules has own Struts configuration file.

  • 相关阅读:
    pymongo中的连接操作:Connection()与MongoClient()
    Dynamics CRM2016 新功能之从CRM APP通过电子邮件发送页面链接
    [开发工具]_[Sublime Text 2]_[配置C++编译执行环境]
    struts2 全局拦截器,显示请求方法和參数
    A. Polo the Penguin and Strings
    linux驱动之LED驱动_1
    dbgrid控件如何能在左边显示行号?
    软件提示“没有活动事务”原因以及解决办法
    刷新dbgrid 而不失去当前行位置
    用ClientDataSet更新数据表,怎样自动生成行号? [问题点数:40分]
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4766269.html
Copyright © 2011-2022 走看看