java - Spring MVC configuration with @EnableWebMvc and WebMvcConfigurerAdapter for static resources location causes problems with "forward:" and "redirect:" -


i have spring boot hello world web , confusion configuration:

spring version: 1.2.6.release

my project structure:

enter image description here

i need supply static content decided redefine source directory type of content in custom webconfig class (for study purposes):

javadoc @enablewebmvc say:

adding annotation @configuration class imports spring mvc configuration webmvcconfigurationsupport

to customize imported configuration, implement interface webmvcconfigurer or more extend empty method base class webmvcconfigureradapter , override individual methods, e.g.:

so next configuration-class born:

@configuration @enablewebmvc public class webconfig extends webmvcconfigureradapter {      @override     public void addresourcehandlers(resourcehandlerregistry registry) {         registry.addresourcehandler("/r/**").addresourcelocations("classpath:/static/r/");         registry.addresourcehandler("/favicon.ico").addresourcelocations("classpath:/static/r/favicon.ico");     }     @override     public void addviewcontrollers(viewcontrollerregistry registry) {         registry.addviewcontroller("/").setviewname("forward:/r/diploma/index.html");         registry.setorder(ordered.highest_precedence);         super.addviewcontrollers(registry);     } } 

and if run app , try visit http://localhost:8080/ next exception:

javax.servlet.servletexception: not resolve view name 'forward:/r/diploma/index.html' in servlet name 'dispatcherservlet' 

but if remove @enablewebmvc webconfig, got index.html in browser. reason of such behavior?

actually have production project using example study , has both @enablewebmvc & webmvcconfigureradapter on webconfig.

you should add viewresolver resolves views in webconfig, this:

@bean public internalresourceviewresolver defaultviewresolver() {     return new internalresourceviewresolver(); } 

when you're adding @enablewebmvc, you're turning off of spring boot's web auto configurations, automatically configures such resolver you. removing annotation, auto configuration turn on again , auto configurated viewresolver solves problem.


Comments