Springboot-web篇---part02-引入资源,静态资源映射,国际化

默认访问首页的设置方式

方式一 :
controller中

 @RequestMapping({"/","/index.html"})
    public String index(){
        return "index";
    }

方式二:
MyMvcConfig中

//所有的WebMvcConfigurerAdapter组件都会一起起作用

@Bean//讲组件注册在容器中
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() {
            public  void addViewControllers(ViewControllerRegistry registry){
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");

            }
        };
        return adapter;
    }
}

静态资源映射

静态资源映射参照

国际化

mvc方式概述:
1.编写国际化配置文件
2.使用ResourceBundleMessageSource管理国际化资源文件
3.在页面使用fmt:message去除国际化内容
步骤:
1.编写国际化配置文件,抽取页面需要的国际化消息
在这里插入图片描述
在这里插入图片描述
2.SpringBoot自动配置好了管理国际化资源文件的组件
3.去页面取国际化的值
application.properties去配置
spring.messages.basename=国际化文件位置(例:spring.messages.basename=i18n.login)

取值方式:
在这里插入图片描述
4.点击链接切换国际化

/*
* 在连接上携带区域信息
* */
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l= request.getParameter("1");
        Locale locale=Locale.getDefault();
        if(!StringUtils.isEmpty(1)){
             String[] split=l.split("_");
             locale=new Locale(split[0],split[1]);
        }
        return locale;
    }
    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
    }

    @Bean
    public LocaleResolver localeResolver(){
    return new MyLocaleResolver();
    }
发布了43 篇原创文章 · 获赞 6 · 访问量 1525

猜你喜欢

转载自blog.csdn.net/weixin_43729631/article/details/104681188