【问题标题】:Is there a way to set context-params programmatically in embedded jetty?有没有办法在嵌入式码头中以编程方式设置上下文参数?
【发布时间】:2012-03-17 04:48:55
【问题描述】:

查看以下嵌入式 Jetty 示例: http://musingsofaprogrammingaddict.blogspot.com.au/2009/12/running-jsf-2-on-embedded-jetty.html

给出以下代码示例(如下。

然后作者继续给出了一个在 web.xml 文件中引用上下文参数的示例。例如

...
<context-param>
  <param-name>com.sun.faces.expressionFactory</param-name>
  <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
...

我的问题是 - 如果我想在 Java 类中做所有事情 - 有没有办法以编程方式设置上下文参数?

public class JettyRunner {

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

        Server server = new Server();

        Connector connector = new SelectChannelConnector();
        connector.setPort(8080);
        connector.setHost("127.0.0.1");
        server.addConnector(connector);

        WebAppContext wac = new AliasEnhancedWebAppContext();
        wac.setContextPath("/myapp");
        wac.setBaseResource(
            new ResourceCollection(
                new String[] {"./src/main/webapp", "./target"}));
        wac.setResourceAlias("/WEB-INF/classes/", "/classes/");

        server.setHandler(wac);
        server.setStopAtShutdown(true);
        server.start();
        server.join();
    }
}

【问题讨论】:

    标签: java embedded-jetty web-testing


    【解决方案1】:

    你的情况

    wac.setInitParameter("com.sun.faces.expressionFactory",
                         "com.sun.el.ExpressionFactoryImpl")
    

    会的。

    【讨论】:

    • 如何以编程方式为 servlet 2.5 设置上下文参数?
    【解决方案2】:
        ServletContextHandler context = new ServletContextHandler(
                ServletContextHandler.SESSIONS);
        context.setContextPath("/");
    

    上面的代码应该适合你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2020-04-23
      • 2013-04-02
      • 1970-01-01
      相关资源
      最近更新 更多