三 扩展mvc (重要有点复杂)

64 阅读1分钟

一个视图跳转的例子.

package com.xuxing.study2.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/xuxing").setViewName("xu");
    }
}

添加对应的视图 templates/xu.html

运行效果如图
image.png

官方的建议,扩展方式。

如果加 @EnableWebMvc 后, 则扩展了 webMvcConfigurationSupport 而 mvc 有conditionNo on webMvcConfigurationSupport ,

  1. 视图解析器。
  2. 格式转换器。
  3. 消息转换器

未完待续,后续补充。