I a new to Java Spring MVC web development. I am kind of confused by the 3 config files below. They are auto created by the STS webmvc project template.
Create a file name "javax.servlet.ServletContainerInitializer" (without quotes) the file content will be fully qualified name of the class implementing this interface, put the file here /META-INF/services
You may implement ServletContainerInitializer and override the method like this
public class CourtServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(Set> c, ServletContext ctx) throws ServletException {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(CourtConfiguration.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
ServletRegistration.Dynamic registration = ctx.addServlet("court", dispatcherServlet);
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}
After this you do not need web.xml
Do remember if you are using maven to build your application mention this in pom.xml
false
Before that you have to write a configuration class using @Configuration and @Bean annotations
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages = "com.practice.learnspringmvc.*")
public class CourtConfiguration {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/views/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}
This configuration class replaces your
initializers from servlet-context.xml