有没有办法在嵌入式码头中以编程方式设置context-params?

haw*_*eye 9 java web-testing embedded-jetty

查看嵌入式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>
...
Run Code Online (Sandbox Code Playgroud)

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

xey*_*eye 10

在你的情况下

wac.setInitParameter("com.sun.faces.expressionFactory",
                     "com.sun.el.ExpressionFactoryImpl")
Run Code Online (Sandbox Code Playgroud)

会做.