H2ManagerMenu

TODO - v2 - not yet reviewed/updated.

The H2ManagerMenu provides a single menu item to redirect to the H2 web console. This is only enabled for prototyping, and only if H2 is detected in the underlying JDBC URL. The menu appears under the "Prototyping" menu.

API & Implementation

The API of the service is:

public class H2ManagerMenu {
    public void openH2Console() { /* ... */ }
}

Note that this launches the manager on the same host that the webapp runs, and so is only appropriate to use when running on localhost.

Additional Configuration

The following additional configuration is also required (all of this is present in the helloworld and simpleapp starter apps):

  • the h2 jdbc driver is required on the classpath of the webapp:

    pom.xml
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
  • the jdbc:h2 URL configuration properties must be defined, eg in application.properties:

    application.properties
    spring.datasource.platform=h2
    spring.datasource.url=jdbc:h2:mem
  • finally, and most importantly, the h2 console servlet must be configured in web.xml, and must be mapped to /db:

    web.xml
    <servlet>
        <servlet-name>H2Console</servlet-name>
        <servlet-class>org.h2.server.web.WebServlet</servlet-class>
        <init-param>
            <param-name>webAllowOthers</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>H2Console</servlet-name>
        <url-pattern>/db/*</url-pattern>
    </servlet-mapping>

Disabling/hiding the menu

The menu can be hidden or disabled by subscribing to its domain event, eg:

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

@Service
public class HideH2ManagerMenu {

    @EventListener(H2ManagerMenu.ActionDomainEvent.class)
    public void on(H2ManagerMenu.ActionDomainEvent ev) {
        ev.hide();
    }
}