Question

With Struts2 we have to have struts.xml in the class path, so it no longer works to have it under WEB-INF. So the way I got my project to deploy was to stick it under WEB-INF/classes and have it include ../struts2.xml

2 Problems:

  1. Eclipse cleans out the classes folder when I do a rebuild, so it deletes struts.xml
  2. Eclipse doesn't show the classes folder in my project browser, so its a poor place to stick config files in the first place.

How are you Struts2 Eclipse developers doing this?

Was it helpful?

Solution

You can either just put the struts.xml at the root of your source directory or set up an additional resources source directory and put it there. Eclipse quite happily copies it to WEB-INF/classes for you when it does a compilation.

OTHER TIPS

I am late to the party, we can configure the struts.xml in any directory in the classpath of the web application, but provide the location using the "config" init parameter of the filter configuration in web.xml as below, if my struts.xml file is in "/com/resources/" directory.

   <filter>
        <filter-name>action</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         <init-param>
            <param-name>config</param-name>
            <param-value>struts-default.xml,struts-plugin.xml,/com/resources/struts.xml</param-value>
         </init-param>
    </filter>

If we don't provide a config init parameter struts2 by default takes 3 values "struts-default.xml,struts-plugin.xml,struts.xml", you can see the struts2 Dispatcher class code below which will configure these 3 files to the configuration manager.

String configPaths = (String)this.initParams.get("config");
if (configPaths == null) {
  configPaths = "struts-default.xml,struts-plugin.xml,struts.xml";
}
String[] files = configPaths.split("\\s*[,]\\s*");
for (String file : files)
  if (file.endsWith(".xml")) {
    if ("xwork.xml".equals(file))
      this.configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
    else
      this.configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, this.servletContext));
  }

I am not using Eclipse so this answer is not specific to your requirements but, I use Maven so we have all the "resources" that are needed by the application in a seperate folder called "resources" and When the application is built these files are copied into the appropriate places automatically. In Netbeans the files in the folder are available and I know that there are persons using eclipse with a similar setup.

I should point out that our project started from appfuse so most of these configurations were pre made. You can look at how it was done there.

In struts 2 projects, struts.xml file is added in src(Java Resources) folder along with the packages and libraries.

Please refer the image given below.

Struts 2 project directory view in eclipse

If u want to know more about struts 2 project structure please visit this link

Note: In eclipse, you are not allowed to paste a file directly in src folder. So you need to first paste it in any other place in the project( for example, in 'WebContent' folder), then use move functionality to put it in right place( That is 'src' folder).

You can place struts.xml file in src(Java Resources) packages. When the compilation process struts.xml file will generate inside the ROOT/WEB-INF/classes directory.

if you get the same error again and again better check the struts actions. check the deployed path of the application and you can find out what you want. (struts.xml file)

With Struts 1.2, it was required to put the struts-config.xml in the classpath (under WEB-INF folder) but with Struts 2.0, it is required to be in src/main/resources folder.

See the documentation Struts 2 Documentation here

I pasted struts.xml in this directory and the project executed fine.

My project structure looks like this

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top