4

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:

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html#section4

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?

Magno C
  • 1,922
  • 4
  • 28
  • 53
  • Interesting experiment! – Andrea Ligios Dec 10 '15 at 23:15
  • Could you try (just to see what happens) to 1) Change the name of the package both in struts.xml and in action annotations to something that isn't `default` and 2) if n.1 doesn't work, specify the interceptor stack with annotations directly on the action file ? And eventually 3) removing the convention plugin and defining the action in struts.xml, to ensure the struts.xml is really not getting loaded, and not ignored or overwritten by the convention plugin. – Andrea Ligios Dec 10 '15 at 23:22
  • Hi Andrea. The struts.xml file isn't being readed... I can totally mess with this file ( fill with some lorem ipsum ) ... and nothing hapens. – Magno C Dec 15 '15 at 17:32
  • Did you succeed in this one ??.I'm trying to do exactly the same. i'm facing exactly same issues.Can't able to resolve it.Can you share sample code on how to do it. Can you tell me about any tutorials or something it will be very much helpful.@AndreaLigios can you help me. – udaybhaskar Jun 08 '16 at 06:56
  • No... I'm not working with this anymore. I loose my patience. I think certain things are just for academic purposes. Embedded Tomcat is one of them. – Magno C Jun 08 '16 at 11:14

1 Answers1

0

Even though it won't answer the OP directly.But the desired goal of creating executable Fat jar can be achieved.Future google searches can benefit from this.This works with struts2 which i did.AFIK it will work for any web app.

By using tomcat7-maven-plugin it's became just peace of cake. As mentioned here .

 <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
     <executions>
       <execution>
        <id>tomcat-run</id>
        <goals>
            <goal>exec-war-only</goal>
        </goals>
        <phase>package</phase>
        <configuration>
            <path>/appName</path>
            <enableNaming>false</enableNaming>
            <finalName>appName.jar</finalName>
            <charset>utf-8</charset>
         </configuration>
     </execution>
  </executions>
</plugin> 

As mentioned in here either you have to use 2.1 or 2.3 because 2.2 has bug. According to Tomcat Docs you can choose minor version of tomcat by mentioning all tomcat dependencies explicitly in the plugin(I haven't tried).

If you run package goal in maven it will produce .jar file in target folder. You can run it using java -jar appName.jar

Community
  • 1
  • 1
udaybhaskar
  • 159
  • 5
  • 16
  • I think you missunderstood the question. I can't find any reference to make Struts2 work in your answer. – Magno C Jun 13 '16 at 11:34
  • nope. Add this try with maven goal package it will create fat jar. you can run it java -jar. I'm using struts2 and hibernate for my web application. – udaybhaskar Jun 13 '16 at 13:02
  • So I just need to create a normal Java Web application (no Embedded Tomcat) and use a POM like this and it will become a standalone Web Application? Can you post your POM with all relevant items because I can already create a standalone tomcat application. I just can't put Struts2 to work. – Magno C Jun 13 '16 at 14:55
  • you don't need to add one more thing extra.Just add that plugin to your pom.xml and execute maven goal `package`.It will give you appName.jar in target folder. run it using `java -jar appName.jar`. – udaybhaskar Jun 14 '16 at 05:27