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

本文介绍SpringBoot中默认访问首页的两种配置方法,包括在Controller和MyMvcConfig中的实现,以及如何通过WebMvcConfigurerAdapter组件进行设置。此外,还详细讲解了SpringBoot的国际化配置流程,从编写国际化配置文件到使用ResourceBundleMessageSource管理资源,再到页面上使用fmt:message获取国际化内容的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

默认访问首页的设置方式

方式一 :
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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值