首页 > 编程开发 > Java    日期:2020-05-26 / 来自互联网 / 浏览

SpringMVC提供的ViewResolver可以分为两大类:面向单一视图和面向多视图类型。所谓面向单一视图指可通过视图模板的位置来定位视图,面向多视图需要额外的配置文件来确定视图。

项目结构如下(Idea)

Springboot视图解析器ViewResolver使用实例

代码

package com.syu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

//若你想diy一些定制化的功能,只需要写这个组件,然后将它交给SpringBoot
//扩展SpringMVC dispatchservlet
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

  //ViewResolver 实现了试图解析器的接口的类,我们就可以把它看作一个视图解析器
  @Bean
  public ViewResolver myViewResolver(){
    return new MyViewResolver();
  }

  public static class MyViewResolver implements ViewResolver{

    @Override
    public View resolveViewName(String s, Locale locale) throws Exception {
      return null;
    }
  }
}

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章