【问题标题】:Struts 1.3.10 in embedded Jetty. ActionServlet can't find /WEB-INF/web.xml嵌入式 Jetty 中的 Struts 1.3.10。 ActionServlet 找不到 /WEB-INF/web.xml
【发布时间】:2011-11-19 23:19:47
【问题描述】:

州:

  • 我有一个基于 Struts 1.3 的 web 应用程序,可以很好地部署到 Tomcat(战争或爆炸)。
  • 我将 webapp 与一个类结合起来运行嵌入式 jetty 7.x。这一切都进入一个 jar 文件。
  • 我使用 maven-assembly-plugin 将所有已分解的依赖项打包到单个 jar 中。我查看了 jar 文件,一切都如你所料。除了所有依赖类之外的标准 Web 应用程序布局都在标准包布局中。 WEB-INF/web.xml 就在您期望的位置。
  • Jetty 可以正常启动并运行我的第一个启动 servlet,它会进行一些数据库初始化。我的 JspConfiguration 使用路径“WEB-INF/web.xml”来获取 web.xml(注意缺少前导斜杠)。

问题

  • 当 Struts 动作 servlet 初始化时,它专门进行如下调用:

    InputStream input = getServletContext().getResourceAsStream("/WEB-INF/web.xml");

导致:

javax.servlet.ServletException: The /WEB-INF/web.xml was not found.
    at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1781)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:349)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:432)
    at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:260)

问题:

我想这是由于 struts 在请求资源时使用了前导斜杠。

  • 我应该采用不同的包装方式吗?
  • 我是否应该有代码来捕获该请求并调整 URI 以删除前导斜杠?怎么样?
  • 如果可能的话,我宁愿不调整 struts 代码....
  • 如果您决定提供帮助,谢谢!

信息:

这是我用来启动码头的类以及两个依赖类。

public class JettyRunner {
    public static void main(String[] args) throws Exception {

        if (args == null) {
            args = new String[0];
        }


        // Construct the new arguments for jetty-runner
        boolean transientState = false;


        int port = 8070;

        Server server = new Server(port);

        WebAppContext webapp = new WebAppContext(".", "");
        webapp.setConfigurations(new Configuration[]{new JspConfiguration()});


        ClassPathResourceHandler resourceHandler = new ClassPathResourceHandler();
        resourceHandler.setContextPath("");

        ContextHandlerCollection contexts = new ContextHandlerCollection();

        contexts.addHandler(resourceHandler);
        contexts.addHandler(webapp);

        server.setHandler(contexts);
        //server.setHandler(webapp);
        URL jettyXmlURL = new JettyRunner().getClass().getClassLoader().getResource("jetty.xml");
        XmlConfiguration configuration = new XmlConfiguration(jettyXmlURL); //or use new XmlConfiguration(new FileInputStream("myJetty.xml"));
        //XmlConfiguration configuration = new XmlConfiguration(new FileInputStream("jetty.xml"));
        configuration.configure(server);

        server.start();
        server.join();

    }


}

public class JspConfiguration extends WebXmlConfiguration {

    @Override
    public Resource findWebXml(WebAppContext webAppContext) throws IOException, MalformedURLException {
        URL path = getClass().getClassLoader().getResource("WEB-INF/web.xml");
        return  Resource.newResource(path);
    }
}

public class ClassPathResourceHandler extends ContextHandler {
    Logger log = Logger.getLogger(startupServlet.class.getName());

    private ResourceHandler realResourceHandler = null;

    public ClassPathResourceHandler() {
        realResourceHandler = new ResourceHandlerImplementation();
    }

    @Override
    public void doHandle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
        realResourceHandler.handle(s, request, httpServletRequest, httpServletResponse);
    }

    private class ResourceHandlerImplementation extends ResourceHandler {

        @Override
        protected Resource getResource(HttpServletRequest httpServletRequest) throws MalformedURLException {

            String requestedFile = httpServletRequest.getRequestURI();
            log.debug("getResource(): " + requestedFile);

            URL path = getClass().getResource(requestedFile);

            try {
                Resource resource = Resource.newResource(path);
                if (resource != null && resource.exists() && !resource.isDirectory()) {
                    return resource;
                }
                else {
                    return null;
                }
            }
            catch (IOException e) {
                return null;
            }
        }
    }


}

【问题讨论】:

    标签: java struts jetty embed


    【解决方案1】:

    Struts 被设计为作为 Web 应用程序而不是作为独立应用程序运行。在您的 JettyRunner 类中,您有一个 main 方法来启动 Jetty。这不适用于 Struts。如果要使用 Struts 1.x,则必须创建一个 Web 应用程序。

    【讨论】:

    • 感谢您的回复。它一个在 tomcat/resin/weblogic 和独立码头中运行良好的网络应用程序。问题在于 Struts 的 ActionServlet 在 embedded 码头服务器中运行时发现 WEB-INF/web.xml。 Web 应用程序启动,只是 servlet init 未能找到此资源。我问问题的方式可能不清楚。还是谢谢。
    【解决方案2】:

    我能够使用 WebAppContext 的 setWar() 方法为我的 struts 1.3 应用程序解决这个问题。我的基本码头跑步者版本如下所示:

    public class JettyRunner {
        private static final Logger logger = Logger.getLogger(JettyRunner.class);
    
        public static void main(String[] args) throws Exception {
    
            WebAppContext context = new WebAppContext();
            context.setWar(System.getProperty("user.dir") + "/src/main/webapp/");
            context.setContextPath("/");
            logger.debug(context.dump());
    
            Server server = new Server(8080);
            server.setHandler(context);
    
            server.start();
            server.join();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      相关资源
      最近更新 更多