Reputation: 23854
This is the code I have:
public class Bootstrap implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
container.getServletRegistration("default").addMapping("/resources/*");
AnnotationConfigWebApplicationContext servletContext =
new AnnotationConfigWebApplicationContext();
servletContext.register(ServletContextConfiguration.class);
ServletRegistration.Dynamic dispatcher =
container.addServlet("springDispatcher", new DispatcherServlet(servletContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
moving on:
@Configuration
@EnableWebMvc
@ComponentScan(
basePackages = "biz.tugay.booksspringone.controller",
useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(Controller.class)
)
public class ServletContextConfiguration {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
}
and web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>/welcome</welcome-file>
</welcome-file-list>
</web-app>
and my controller:
@Controller
public class HelloController {
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView welcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("welcome");
return modelAndView;
}
}
When I deploy my app and navigate to localhost:8080/ I expect HelloController.welcome to be called, but it is not.
The method is called only if I explicitly visit http://localhost:8080/welcome
How can I fix this?
Upvotes: 0
Views: 2554
Reputation: 4807
A welcome file list will find for that file "not a request" you specified in the tag.
so
<welcome-file-list>
<welcome-file>/welcome</welcome-file>
</welcome-file-list>
will look for the file with criteria you specified in view resolver, so it will going to find following file
/WEB-INF/views/welcome.jsp
Create that file and you will be redirected to welcome.jsp for the home url.
Upvotes: 2