I have configured view resolvers in DispatcherServlet xml files. But how to configure view resolver using annotations in spring boot?
1 Answers
From Spring Boot documentation :
If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.
You can do it :
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
}
Of course, adapt the prefix and the suffix according to your actual configuration.
Edit to handle the redirection to the page when /
is request :
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
// add a mapping for redirection to index when / requested
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index");
}
}

- 125,838
- 23
- 214
- 215
-
my index.html in in root folder. i want index.html to be opened when i run localhost:8080. how can i do this – Roshan Bade Dec 25 '16 at 14:50
-
You can add a mapping to do the redirection.I just added this in my answer to illustrate how to do it. But normally you should not need it if index.html is at the root. – davidxxx Dec 25 '16 at 15:12
-
but it shows me this error: This application has no explicit mapping for /error, so you are seeing this as a fallback. – Roshan Bade Dec 25 '16 at 15:13
-
Indeed it is a fallback. You should edit your question and show the layout of the resource files, it could help to understand :) – davidxxx Dec 25 '16 at 15:14
-
okay, i set prefix to "/WEB-INF/" on above code and mapping for " /index " is import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class GreetingController { @RequestMapping("/index") public String greeting() { return "index"; } } – Roshan Bade Dec 25 '16 at 15:19
-
if you use Spring Boot, you should not place the web resources here. Move your `index.html` in `src/main/resources/static` folder of the projet and remove this mapping because it should not be required and try again. – davidxxx Dec 25 '16 at 15:24
-
ooo thats works fine, is that some default thing spring does? ;) – Roshan Bade Dec 25 '16 at 15:36
-
Read the **27.1.5 Static Content part** from http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html. You should understand the default properties and behaviors of Spring. – davidxxx Dec 25 '16 at 16:03