Spring JavaConfig:为自定义Servlet添加映射 [英] Spring JavaConfig: Add mapping for custom Servlet

查看:503
本文介绍了Spring JavaConfig:为自定义Servlet添加映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于javaconfig的Spring 4.0项目中,如何将某个URL的映射添加到除Spring DispatcherServlet之外的Servlet。

In a javaconfig-based Spring 4.0 project, how can I add a mapping for a certain URL to a Servlet other than the Spring DispatcherServlet.

我的情况我是想要使用来自H2数据库的h2console,它通过servlet提供 org.h2.server.web.WebServlet

Im my case I want to use h2console from H2 database which is provided through the servlet org.h2.server.web.WebServlet

编辑:在即将推出的Spring Boot 1.3中,可以使用配置参数启用h2console: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features- sql-h2-console

In the upcoming Spring Boot 1.3 the h2console can be enabled with a configuration parameter: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-sql-h2-console

启用它就像将这两行添加到 application.properties

Enabling it is as simple as adding these two lines to your application.properties:

spring.h2.console.enabled=true
spring.h2.console.path=/console


推荐答案

最简单的方法是使用初始化程序实现可怕的ctly WebApplicationInitializer和以下代码添加到 onStartup(ServletContext servletContext)方法;

The easiest way is to use initializer implementing directly WebApplicationInitializer and the add into onStartup(ServletContext servletContext) method following code;

ServletRegistration.Dynamic h2Servlet = servletContext.addServlet("h2Servlet", new org.h2.server.web.WebServlet());
h2Servlet.setLoadOnStartup(1);
h2Servlet.addMapping("/h2/*");

这篇关于Spring JavaConfig:为自定义Servlet添加映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆