How to run Struts2 app on embedded Jetty?

203 views Asked by At

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.

1

There are 1 answers

1
Joakim Erdfelt On

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.