As you can see, I decided to dispense with the web.xml
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class SpringWebInicializador implements WebApplicationInitializer {
public void onStartup(ServletContext contenedor) throws ServletException {
AnnotationConfigWebApplicationContext contexto = new AnnotationConfigWebApplicationContext();
contexto.register(ConfiguracionSpring.class);
contexto.setServletContext(contenedor);
ServletRegistration.Dynamic servlet = contenedor.addServlet("dispatcher", new DispatcherServlet(contexto));
servlet.setLoadOnStartup(0);
servlet.addMapping("/");
}
}
Here I put the code for the spring configurator
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@ComponentScan("lacasadelasabuelas.*")
@Configuration
public class ConfiguracionSpring implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
@Bean
// Only used when running in embedded servlet
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
System.out.println("llega");
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
But at the time of doing a requestMappging to the address: localhost: 8080 / hello I get a 404
@Controller
public class MainController {
@RequestMapping("/hola")
public String hola() {
return "index";
}