Deployment


Deployment of more than one Struts project requires special consideration.  The problem concerns ambiguity with respect to several types of files. These issues are discussed below, and a solution in outlined.

Assume one has two struts projects called StrutsAB and StrutsABApplet.

By convention, all servlet files are deployed under the WEB-INF/classes directory.  If one uses package names, such as jbs.abstruts.action and jbs.abapplet.action, there will be no ambiguity if the classes under . . . action have the same names, such as ActionLogin, in both projects so long as the fully qualified names are used.

Similarly, if the JSPs are deployed to project subdirectories, such as abstruts and abapplet, under the general context, there will be no ambiguity since they will be referenced through context-relative paths, such as /context/abstruts/login.jsp and /context/abapplet/login.jsp. 

However, problems do arise with respect to the projects' struts-config.xml files.  Each project has a struts-config.xml file that contains the mappings between symbolic names by which the Struts framework references objects and the actual objects, themselves, e.g., class files.  By default, the project's struts-config.xml file is placed directly under the context's WEB-INF directory.  No problem for a single Struts project within a given context, but what to do with the second project's struts-config.xml file when that project is deployed to the same context?

There are two solutions. The preferred way is through a Struts mechanism called modules; however, this approach is more complex for simple situations such as the one being considered here.  The other approach is based on concatenating struts-config.xml files, is simpler but less elegant than modules, and is the approach that will be discussed here.

To concatenate struts-config.xml files and thereby address the problem of deploying multiple Struts projects to the same context, several steps are involved.

First, the preferred approach is to create under the WEB-INF directory a subdirectory, conf, that can hold multiple configuration files. 

Second, place the second project's struts-config.xml file in the conf directory; you may wish to rename it, e.g., struts-config-abapplet.xml, for easy identification.  You could also place the first projects configuration file here, as well.

Third, edit the general web.xml file that applies to all projects deployed in the context.  There, comma-delimit references to the second (and subsequent) configuration file(s).  This is done within a standard parameter that maps the Struts ActionServlet class to its configuration file(s). 

The results will be that the two (or more) configuration files will be concatenated.  If there are ambiguities with regard to names, the last one appearing will take precedence.  Thus, you may need to change the mapping names in one (or more) of the configuration files.  But -- here the beauty of late binding comes into play -- you will not need to change anything in the compiled classes themselves, since they use symbolic names.  The only internal change required is in the action references in <html:form> tags that point to symbolic names for actions.

These steps are illustrated in the fragments, below.


web.xml File

<web-app>
    <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.xml,/WEB-INF/conf/struts-config- abapplet.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>3</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>

 

Struts-config-abapplet.xml File

<!-- ========== Form Bean Definitions ================================== -->

<form-beans>
    <form-bean name="formBeanLoginABApplet" type="jbs.abapplet.formbean.FormBeanLogin">
    <form-property name="loginPassword" type="java.lang.String" />
    <form-property name="loginName" type="java.lang.String" />
    <form-property name="message" type="java.lang.String" />
    <form-property name="loginPasswordConfirm" type="java.lang.String" />
</form-bean>

<!-- ========== Action Mapping Definitions =============================== -->

<action-mappings>
    <action
        attribute="formBeanLoginABApplet"
        input="/abapplet/login.jsp"
        name="formBeanLoginABApplet"
        path="/actionLoginABApplet"
        scope="request"
        type="jbs.abapplet.action.ActionLogin">
        <forward name="success" path="/abapplet/abapplet.jsp" />
        <forward name="failure" path="/abapplet/login.jsp" />
    </action>
</action-mappings>

 

JSP Reference

<html:form action="/actionLoginABApplet">