本文整理匯總了Java中org.springframework.web.servlet.DispatcherServlet類的典型用法代碼示例。如果您正苦於以下問題:Java DispatcherServlet類的具體用法?Java DispatcherServlet怎麽用?Java DispatcherServlet使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DispatcherServlet類屬於org.springframework.web.servlet包,在下文中一共展示了DispatcherServlet類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onStartup
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//register config classes
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebMvcConfig.class);
rootContext.register(JPAConfig.class);
rootContext.register(WebSecurityConfig.class);
rootContext.register(ServiceConfig.class);
//set session timeout
servletContext.addListener(new SessionListener(maxInactiveInterval));
//set dispatcher servlet and mapping
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(rootContext));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
//register filters
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("endcodingFilter", new CharacterEncodingFilter());
filterRegistration.setInitParameter("encoding", "UTF-8");
filterRegistration.setInitParameter("forceEncoding", "true");
//make sure encodingFilter is matched first
filterRegistration.addMappingForUrlPatterns(null, false, "/*");
//disable appending jsessionid to the URL
filterRegistration = servletContext.addFilter("disableUrlSessionFilter", new DisableUrlSessionFilter());
filterRegistration.addMappingForUrlPatterns(null, true, "/*");
}
示例2: addDispatcherContext
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
private void addDispatcherContext(ServletContext container) {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(SpringDispatcherConfig.class);
// Declare <servlet> and <servlet-mapping> for the DispatcherServlet
ServletRegistration.Dynamic dispatcher = container.addServlet("ch08-servlet",
new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
dispatcher.setAsyncSupported(true);
//FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
// springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");
// springSecurityFilterChain.setAsyncSupported(true);
}
示例3: addDispatcherContext
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
private void addDispatcherContext(ServletContext container) {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(SpringDispatcherConfig.class);
// Declare <servlet> and <servlet-mapping> for the DispatcherServlet
ServletRegistration.Dynamic dispatcher = container.addServlet("ch03-servlet",
new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("*.html");
dispatcher.setLoadOnStartup(1);
FilterRegistration.Dynamic corsFilter = container.addFilter("corsFilter", new CorsFilter());
corsFilter.setInitParameter("cors.allowed.methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE");
corsFilter.addMappingForUrlPatterns(null, true, "/*");
FilterRegistration.Dynamic filter = container.addFilter("hiddenmethodfilter", new HiddenHttpMethodFilter());
filter.addMappingForServletNames(null, true, "/*");
FilterRegistration.Dynamic multipartFilter = container.addFilter("multipartFilter", new MultipartFilter());
multipartFilter.addMappingForUrlPatterns(null, true, "/*");
}
示例4: onStartup
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter("UTF-8"));
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("Dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
CXFServlet cxf = new CXFServlet();
BusFactory.setDefaultBus(cxf.getBus());
ServletRegistration.Dynamic cxfServlet = servletContext.addServlet("CXFServlet", cxf);
cxfServlet.setLoadOnStartup(1);
cxfServlet.addMapping("/services/*");
servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")).addMappingForUrlPatterns(null, false,
"/*");
servletContext.getSessionCookieConfig().setSecure(cookieSecure);
}
示例5: onStartup
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//On charge le contexte de l'app
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.setDisplayName("scrumtracker");
rootContext.register(ApplicationContext.class);
//Context loader listener
servletContext.addListener(new ContextLoaderListener(rootContext));
//Dispatcher servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
示例6: configServlet
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Bean(name = "configServlet")
public ServletRegistrationBean configServlet() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ConfigServletConfig.class);
dispatcherServlet.setApplicationContext(applicationContext);
ServletRegistrationBean registrationBean = new ServletRegistrationBean(dispatcherServlet, "/config/*");
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
示例7: onStartup
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
/**
* Configure the given {@link ServletContext} with any servlets, filters, listeners
* context-params and attributes necessary for initializing this web application. See examples
* {@linkplain WebApplicationInitializer above}.
*
* @param servletContext the {@code ServletContext} to initialize
* @throws ServletException if any call against the given {@code ServletContext} throws a {@code ServletException}
*/
public void onStartup(ServletContext servletContext) throws ServletException {
// Spring Context Bootstrapping
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
rootAppContext.register(AutoPivotConfig.class);
servletContext.addListener(new ContextLoaderListener(rootAppContext));
// Set the session cookie name. Must be done when there are several servers (AP,
// Content server, ActiveMonitor) with the same URL but running on different ports.
// Cookies ignore the port (See RFC 6265).
CookieUtil.configure(servletContext.getSessionCookieConfig(), CookieUtil.COOKIE_NAME);
// The main servlet/the central dispatcher
final DispatcherServlet servlet = new DispatcherServlet(rootAppContext);
servlet.setDispatchOptionsRequest(true);
Dynamic dispatcher = servletContext.addServlet("springDispatcherServlet", servlet);
dispatcher.addMapping("/*");
dispatcher.setLoadOnStartup(1);
// Spring Security Filter
final FilterRegistration.Dynamic springSecurity = servletContext.addFilter(SPRING_SECURITY_FILTER_CHAIN, new DelegatingFilterProxy());
springSecurity.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
}
示例8: onStartup
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
示例9: onStartup
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(MyMvcConfig.class);
ctx.setServletContext(servletContext); // ②
Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
示例10: onStartup
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
int majorVersion = servletContext.getMajorVersion();
int minorVersion = servletContext.getMinorVersion();
LOG.info("Container servlet version is {}.{}", majorVersion, minorVersion);
int effectiveMajorVersion = servletContext.getEffectiveMajorVersion();
int effectiveMinorVersion = servletContext.getEffectiveMinorVersion();
LOG.info("Application servlet effective version is {}.{}", effectiveMajorVersion, effectiveMinorVersion);
Map<String, ? extends ServletRegistration> map = servletContext.getServletRegistrations();
Collection<? extends ServletRegistration> registrations = map.values();
for (ServletRegistration registration : registrations) {
String className = registration.getClassName();
if (DispatcherServlet.class.getCanonicalName().equals(className)) {
LOG.info("DispatcherServlet has been initialized");
return;
}
}
super.onStartup(servletContext);
}
示例11: addDispatcherServlet
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
private ServletRegistration.Dynamic addDispatcherServlet(ServletContext servletContext, String servletName,
Class<?>[] servletContextConfigClasses, boolean allowBeanDefinitionOverriding, String... mappings) {
Assert.notNull(servletName);
Assert.notEmpty(servletContextConfigClasses);
Assert.notEmpty(mappings);
AnnotationConfigWebApplicationContext servletApplicationContext = new AnnotationConfigWebApplicationContext();
servletApplicationContext.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding);
servletApplicationContext.register(servletContextConfigClasses);
ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet(servletName, new DispatcherServlet(servletApplicationContext));
dispatcherServlet.setLoadOnStartup(1);
dispatcherServlet.addMapping(mappings);
return dispatcherServlet;
}
示例12: dispatcherServletDefaultConfig
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Test
public void dispatcherServletDefaultConfig() {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(ServerPropertiesAutoConfiguration.class,
DispatcherServletAutoConfiguration.class);
this.context.refresh();
DispatcherServlet bean = this.context.getBean(DispatcherServlet.class);
assertThat(bean).extracting("throwExceptionIfNoHandlerFound")
.containsExactly(false);
assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true);
assertThat(bean).extracting("dispatchTraceRequest").containsExactly(false);
assertThat(new DirectFieldAccessor(
this.context.getBean("dispatcherServletRegistration"))
.getPropertyValue("loadOnStartup")).isEqualTo(-1);
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:17,代碼來源:DispatcherServletAutoConfigurationTests.java
示例13: createMainDispatcherServlet
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
private void createMainDispatcherServlet(ApplicationContext rootContext) {
mainWebApplicationContext = new XmlWebApplicationContext();
mainWebApplicationContext
.setConfigLocation(getRequiredInitParameter("communoteWebContextConfigLocation"));
mainWebApplicationContext.setParent(rootContext);
// add ContextLoaderListener with web-ApplicationContext which publishes it under a
// ServletContext attribute and closes it on shutdown. The former is required for
// WebApplicationContextUtils which are used by DelegatingFilterProxy (spring security).
// Closing is also done by dispatcher servlet's implementation of destroy method.
this.servletContext.addListener(new ContextLoaderListener(mainWebApplicationContext));
DispatcherServlet dispatcherServlet = new DispatcherServlet(mainWebApplicationContext);
ServletRegistration.Dynamic addedServlet = this.servletContext.addServlet(
getInitParameter("communoteServletName", "communote"), dispatcherServlet);
addedServlet.setLoadOnStartup(1);
addedServlet.addMapping(getRequiredInitParameter("communoteServletUrlPattern"));
this.mainDispatcherServlet = dispatcherServlet;
}
示例14: setUp
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
// final Template expectedTemplate = new Template();
fc = new FreeMarkerConfigurer();
fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
fc.afterPropertiesSet();
wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
wac.refresh();
request = new MockHttpServletRequest();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
response = new MockHttpServletResponse();
}
示例15: setUp
import org.springframework.web.servlet.DispatcherServlet; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
final Template expectedTemplate = new Template();
VelocityConfig vc = new VelocityConfig() {
@Override
public VelocityEngine getVelocityEngine() {
return new TestVelocityEngine("test.vm", expectedTemplate);
}
};
wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
wac.refresh();
request = new MockHttpServletRequest();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
response = new MockHttpServletResponse();
}