I'm trying to create a embedded Tomcat running with Struts2.
First step was to make a working pure embedded Tomcat. It was easy to do by following this tutorial:
Now, to put the Struts2 to work, the next step was to add the Struts libs to POM file.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.20</version>
</dependency>
Piece of cake! Next step: Add the Struts filter to web.xml. Easy, but .... Embedded Tomcat don't loads the web.xml file! No matter what I do. Solution: create a folder under the target JAR folder, called resources
, put the web.xml file there and add this code to the server starter class:
tomcat.getHost().setAppBase(appBase);
StandardContext ctx = (StandardContext) tomcat.addWebapp(contextPath, appBase);
( (StandardJarScanner) ctx.getJarScanner() ).setScanAllDirectories(true);
ctx.getServletContext().setAttribute( Globals.ALT_DD_ATTR, "resources/web.xml");
tomcat.start();
This will load the web.xml from the external folder. Worked fine. Now I can load struts libs. I can see that by looking the server log. All struts loading stuff is in there.
Next step: To configure an Interceptor. In a normal case, I need to put the struts.xml file into `src\main\resources' folder (Eclipse). So I tried this and not work.
My struts.xml file only have an interceptor tag pointing to a class implementing Interceptor. I like to use annotations to configure Actions so I have no Actions tags in this file. This struts.xml file and the interceptor class came from other system I have and its working very well. Very simple.
The trouble: the struts.xml file is not loading. I can comprove that by sending outputs from interceptor's intercept
method and messing the struts.xml file ( changing the interceptor class name to an inexistent one, messing the tag structure and so on ).
My question: how can I tell the embedded Tomcat to load the struts.xml file or, more precisely, how can I tell the struts engine the struts.xml location?