I have, what I would consider a pretty simple Spring MVC setup. My applicationContext.xml is this:
Java-based configuration without adding any elements to web.xml. WebApplicationInitializer is a perfect fit for use with Spring's code-based @Configuration classes
WebApplicationInitializer « Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach. Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container. Using Servlet Spec 3.0 of Tomcat 7
From Spring 3.2 some Abstract class were listed which implemented WebApplicationInitializer which will be detected automatically by the SrevletContainer.
AbstractAnnotationConfigDispatcherServletInitializer extends
AbstractDispatcherServletInitializer extends
AbstractContextLoaderInitializer implements WebApplicationInitializer
Using Spring 4.1.6.RELEASE version with the modules core, web, webmvc, beans.
public class WebXML_DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
return new Class[] { MvcServletXMLConfigurer.class };
}
@Override
protected Class>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Java-based configuration to Serve Static Resources with Spring. Spring Boot
@Configuration
@EnableWebMvc //
@ComponentScan(value = {"com.github.yash777.controllers"})
//
public class MvcServletXMLConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {
/**
*
*
* @return InternalResourceViewResolver as a bean configuration.
*/
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
//
registry
.addResourceHandler("/styles/**")
.addResourceLocations("/css/") // webapp/css/
.setCachePeriod(3600)
.resourceChain(true) // Spring 4.1
.addResolver(new GzipResourceResolver()) // Spring 4.1
.addResolver(new PathResourceResolver()); // Spring 4.1
//
registry.addResourceHandler("/static/**")
.addResourceLocations("/static/", "classpath:/static/") // src/main/resources/static/
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
Listed a sample controller:
@Controller
@RequestMapping(value = { "/controller", "/c" })
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
@RequestMapping(value = {"/message", "/m"}, method = RequestMethod.GET )
public void message(HttpServletRequest request, HttpServletResponse response ) throws IOException {
System.out.println("@Controller Get method called.");
}
@RequestMapping(value = "/getView", method = RequestMethod.GET )
public ModelAndView setViewName( Model model ) {
System.out.println("GET... /getView");
ModelAndView mav = new ModelAndView();
mav.setViewName("test");
return mav;
}
}
WEB-INF/web.xml
Archetype Created Web Application