对于静态资源位置,使用@EnableWebMvc和WebMvcConfigurerAdapter进行Spring MVC配置会导致“ forward:”问题。和“重定向:” [英] Spring MVC configuration with @EnableWebMvc and WebMvcConfigurerAdapter for static resources location causes problems with "forward:" and "redirect:"

查看:118
本文介绍了对于静态资源位置,使用@EnableWebMvc和WebMvcConfigurerAdapter进行Spring MVC配置会导致“ forward:”问题。和“重定向:”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适用于Web的Spring Boot问候世界,并对配置有些困惑:

I have a Spring Boot hello world for web and some confusion with configuration:

Spring版本:1.2.6.RELEASE

Spring version: 1.2.6.RELEASE

我的项目结构:

我需要提供一些静态内容,因此我决定在一些自定义的 WebConfig 类中(出于研究目的)重新定义此类内容的源目录:

I need to supply some static content so I decided to redefine the source directory for this type of content in some custom WebConfig class (for study purposes):

Javadoc for @EnableWebMvc 说:

Javadoc for @EnableWebMvc say:


将此注释添加到@Configuration类可从WebMvcConfigurationSupport导入Spring
MVC配置

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport

要自定义导入的配置,请实现接口
WebMvcConfigurer或更可能扩展空方法基类
WebMvcConfigurerAdapter并覆盖单个方法,例如:

To customize the imported configuration, implement the interface WebMvcConfigurer or more likely extend the empty method base class WebMvcConfigurerAdapter and override individual methods, e.g.:

因此诞生了下一个配置类:

So the next configuration-class was 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);
    }
}

如果运行该应用并尝试访问 http:// localhost:8080 /
我遇到下一个异常:

And if run the app and try to visit http://localhost:8080/ I get the next exception:

javax.servlet.ServletException: Could not resolve view with name 'forward:/r/diploma/index.html' in servlet with name 'dispatcherServlet'

但是如果我删除了 @EnableWebMvc 从WebConfig中,我在浏览器中找到了index.html。
那么,这种行为的原因是什么呢?

But if i remove the @EnableWebMvc from my WebConfig, I got my index.html in browser. So what is the reason of such behavior?

实际上,我有一个生产项目,以我为例进行研究,它同时具有 @EnableWebMvc & WebConfig 上的 WebMvcConfigurerAdapter

Actually I have a production project which I using as example to study and it has both @EnableWebMvc & WebMvcConfigurerAdapter on WebConfig.

推荐答案

您应该添加 ViewResolver 来解析 WebConfig 中的视图,如下所示:

You should add a ViewResolver to resolves your views in WebConfig, something like this:

@Bean
public InternalResourceViewResolver defaultViewResolver() {
    return new InternalResourceViewResolver();
}

添加 @EnableWebMvc ,您将关闭所有Spring Boot的网络自动配置,这将自动为您配置此类解析程序。通过删除注释,自动配置将再次打开,并且自动配置的 ViewResolver 解决了该问题。

When you're adding @EnableWebMvc, you're turning off all of the spring boot's web auto configurations, which automatically configures such a resolver for you. By removing the annotation, auto configuration will turn on again and the auto configurated ViewResolver solves the problem.

这篇关于对于静态资源位置,使用@EnableWebMvc和WebMvcConfigurerAdapter进行Spring MVC配置会导致“ forward:”问题。和“重定向:”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆