How to configure programmatically withou

2020-02-29 02:31发布

问题:

Is there any other way to configure a context parameter excluding the one which uses web.xml as in the below example?

<context-param>
    <param-name>com.sun.faces.numberOfLogicalViews</param-name>
    <param-value>10</param-value>
</context-param>

回答1:

Since Servlet 3.0, you can programmatically set them via ServletContext#setInitParameter().

Just put below ServletContextListener anywhere in your web project. The @WebListener annotation will make the container to automatically pickup and run it during webapp startup.

@WebListener
public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext(); 
        servletContext.setInitParameter("com.sun.faces.numberOfLogicalViews", "10");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // NOOP.
    }

}


回答2:

If you're using embedded Tomcat, it is like this:

Context.getServletContext()
    .getServletRegistrations()
    .get("webdavservlet")
    .setInitParameter("listings", "true");