Mapleyang

导航

 

SpringMVC扩展

implements WebMvcConfigurer,重写一些方法

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //ViewResolver 实现了视图解析器接口的类,我们就可以把它看做视图解析器
    @Bean
    public ViewResolver myViewResolver(){
        return new NyViewResolver();
    }

    public static class NyViewResolver implements ViewResolver{

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

 

 

Thymeleaf

模板引擎的使用:第一步:引入Thymeleaf

  • Thymeleaf官网:https://www.thymeleaf.org/

  • Thymeleaf在Github的主页:https://github.com/thymeleaf/thymeleaf

  • spring官方文档【springboot参考指南】:https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#using-boot-starter

结论:要使用thymeleaf,只需要导入对应的依赖就可以了!我们将HTML放在我们的templates目录下即可!

public static final String DEFAULT_PREFIX="classpath:/templates/";

public static final String DEFAULT_SUFFIX=".html";

 

修改日期格式,下面这个已经弃用,建议用注解

spring.mvc.date-format=

修改日期注解

@DateTimeFormat(pattern = "yyyy-MM-dd")

 

扩展springmvc 官方建议这样做

//如果我们要扩展springmvc,官方建议我们这样做
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //重写视图跳转方法
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //addViewController添加一个视图,浏览器输入/maple 就会跳转到test页面
        registry.addViewController("/maple").setViewName("text");
    }
}

 

posted on 2022-08-10 09:19  折木~  阅读(51)  评论(0)    收藏  举报