Details
Description
There is a feature in the Struts (1.x) that allows the user to define multiples struts configuration files (struts-config.xml) without use modules. To do this, the user must define a list of files separated by comma in the "config" parameter inside the web.xml file, as you can see:
<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/struts-config-2.xml,/WEB-INF/struts-config-3.xml,/WEB-INF/struts-config-4.xml</param-value>
</init-param>
...
</servlet>
So here comes the problem. If the user defines for each of them a separated validation configuration file, i.e., the file /WEB-INF/struts-config.xml has:
...
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml" />
</plug-in>
...
And the file /WEB-INF/struts-config-2.xml has:
...
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator-2.xml" />
</plug-in>
...
And so on. Then, after the bootstrap, only the last validation configuration file was loaded. So, all others files was discarted. This occurs because after each initialization of the struts config file, the ValidatorPlugin reset the resources in the servlet context. As you can see in the follow piece of code:
...
this.initResources();
servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(), resources);
...
I guess to solve this problem we first need to verify if the resources stored in the servlet context has any value. If yes, instead of reset the new value, we have to add the constants, formsets and actions (from the new resouce readed) to previous resources stored.
Something like this:
...
this.initResources();
ValidatorResources resources = (ValidatorResources) servlet.getServletContext().getAttribute(VALIDATOR_KEY + config.getPrefix());
if (resources != null) {
// Copy constants from previous resources.
for (Iterator i = this.resources.getConstants().keySet().iterator(); i.hasNext()
// Copy validator actions from previous resources.
for (Iterator i = this.resources.getActions().values().iterator(); i.hasNext()
// Copy form sets from previous resources.
for (Iterator i = this.resources.getFormSets().values().iterator(); i.hasNext()
this.resources = resources;
}
servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(), this.resources);
servlet.getServletContext().setAttribute(STOP_ON_ERROR_KEY + '.' + config.getPrefix(), (this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE));
...
But here comes another problem. The methods [getFormSets(), getActions(), getConstants()] are protected. So to do this we have to:
1) Put those methods as public. But they are in another apache project.
2) Create a subclass of ValidatorResources exposing other three methods that are public and return each specified value. One for form set, other for validation action and other for constant.
3) Think in another approach.
Felipe.