1

I used to run Struts2 app on embedded Tomcat using the following code:

package nth347.main;

import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import java.io.File;

public class Main {
    public static void main(String[] args) throws Exception {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8086);

        StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File("src/main/webapp/").getAbsolutePath());
        File additionWebInfClasses = new File("target/classes"); // "target/classes" if Maven, "build/classes" if Grable
        WebResourceRoot resources = new StandardRoot(ctx);
        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));
        ctx.setResources(resources);

        tomcat.start();
        tomcat.getServer().await();
    }
}

I see that we need to point out 1 important thing for the web container - The location of .class files (target/classes in the case of Maven)

I tried the following code (from embedded Jetty docs):

public class OneWebAppUnassembled
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
 
        WebAppContext context = new WebAppContext();
        context.setDescriptor(webapp+"/WEB-INF/web.xml");
        context.setResourceBase("../test-jetty-webapp/src/main/webapp");
        context.setContextPath("/");
        context.setParentLoaderPriority(true);
 
        server.setHandler(context);
 
        server.start();
        server.join();
    }
}

I visited a Struts2 action, I got a 503 Service Unavailable error. So the question is: Can I run Struts2 on embedded Jetty as I did with embedded Tomcat? If yes, example code, please.

NTH
  • 31
  • 4
  • I also tried to use the Maven jetty plugin, when running `mvn jetty:run` I got an error saying that "java.lang.IllegalStateException: class org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter is not a jakarta.servlet.Filter". So Jetty does not support Struts2? – NTH Oct 17 '22 at 07:12
  • wiki.eclipse.org is not for current versions of Jetty, as it says quite prominently in the giant red banner at the top of the page. – Joakim Erdfelt Oct 17 '22 at 14:55

1 Answers1

0

Are your WEB-INF/classes in your ResourceBase?

I doubt it.

This is likely what is happening.

Try referencing your webapp in your maven target directory? Or using the WebAppContext.setExtraClasspath("target/classes") to add the classes it needs to work.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • I added. However, I got another error: ```Unable to load configuration. - bean - jar:file:/C:/Users/nth347/.m2/repository/org/apache/struts/struts2-core/2.5.30/struts2-core-2.5.30.jar!/struts-default.xml:130:150```. Some people said that the jar:file: need to be removed, but not said how to remove it – NTH Oct 17 '22 at 14:52