Skip to content

Commit a1a8c29

Browse files
christian-raedelslavisa-baeldung
authored andcommitted
BAEL-86: Refactored WebInitializer and added MultipartResolver.
1 parent 88a4f61 commit a1a8c29

3 files changed

Lines changed: 37 additions & 28 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.spring.dispatcher.servlet;
2+
3+
import org.springframework.web.context.ContextLoaderListener;
4+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
5+
import org.springframework.web.servlet.DispatcherServlet;
6+
7+
import javax.servlet.MultipartConfigElement;
8+
import javax.servlet.ServletContext;
9+
import javax.servlet.ServletException;
10+
import javax.servlet.ServletRegistration;
11+
12+
public class WebApplicationInitializer implements org.springframework.web.WebApplicationInitializer {
13+
@Override
14+
public void onStartup(ServletContext servletContext) throws ServletException {
15+
AnnotationConfigWebApplicationContext rootContext =
16+
new AnnotationConfigWebApplicationContext();
17+
rootContext.register(RootConfiguration.class);
18+
servletContext.addListener(new ContextLoaderListener(rootContext));
19+
AnnotationConfigWebApplicationContext webContext =
20+
new AnnotationConfigWebApplicationContext();
21+
webContext.register(WebConfiguration.class);
22+
webContext.scan("com.baeldung.spring.dispatcher.servlet.web");
23+
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",
24+
new DispatcherServlet(webContext));
25+
servlet.addMapping("/*");
26+
MultipartConfigElement multipartConfigElement =
27+
new MultipartConfigElement("/tmp");
28+
servlet.setMultipartConfig(multipartConfigElement);
29+
}
30+
}

‎spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/WebConfiguration.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.springframework.context.annotation.Configuration;
77
import org.springframework.context.support.ResourceBundleMessageSource;
88
import org.springframework.ui.context.support.ResourceBundleThemeSource;
9+
import org.springframework.web.multipart.MultipartResolver;
10+
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
911
import org.springframework.web.servlet.LocaleResolver;
1012
import org.springframework.web.servlet.ThemeResolver;
1113
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@@ -111,4 +113,9 @@ public ThemeChangeInterceptor themeChangeInterceptor() {
111113
themeChangeInterceptor.setParamName("theme");
112114
return themeChangeInterceptor;
113115
}
116+
117+
@Bean
118+
public MultipartResolver multipartResolver() {
119+
return new StandardServletMultipartResolver();
120+
}
114121
}

‎spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/WebInitializer.java‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)