zoukankan      html  css  js  c++  java
  • SpringBoot学习构建Web笔记,持续完善...

    总的配置类

    @Configuration
    public class MyMvcConfig implements WebMvcConfigurer {
        @Bean
        //...将类丢入到容器中,注册成组件
    }

     

    首页配置

    所有的静态资源都是用thymeleaf接管,就是导入命名空间,然后对于地址使用@{} 接管,如th:src="@{/img/bootstrap-solid.svg}"

    编写自定义映射类,需要实现WebMvcConfigurer

    public class WelComeConfig implements WebMvcConfigurer {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("index");
            registry.addViewController("/index.html").setViewName("index");
        }
    }

    在我们总的配置类(理解为Spring的配置,它是一个容器,我们配置的需要转换为组件丢到里面去)

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WelComeConfig();
    }

    templates下的静态页面,因为需要thymeleaf模板引擎,因此我们去需要引入命名空间

    <html lang="en" xmlns:th="http://www.thymeleaf.org">

    参照thymeleaf文档可知,链接使用@{}标注,如:

    th:href="@{/css/bootstrap.min.css}"
    th:action="@{/templates/dashboard.html}"

     

    国际化

    i18n(internationalization->i18个字母n,so起名为i18n)

    在resources下建立i18n文件夹,建立login.properties,login_zh_CN.properties,login_en_US.properties,我们建立好了之后,点击login_zh_CN.properties /login_en_US.properties后IDEA会出现一个编辑界面,我们写默认值,中英文的值,IDEA,帮我们分别在这些配置文件下全部建好了相应的字段。我们需要在application.yaml配置文件中开启国际化spring.messages.basename=i18n.login,这样我们静态页面就可以通过#{}读取我们设定的值了,然后我们写我们自顶一个区域解释器的类,放到容器中,供我们使用。

    1.首先建立i18n文件夹,建立国际化需要的配置文件(主要存的就是字段-》中文/英文)

     2.login_zh_CN.properties /login_en_US.properties后IDEA会出现一个编辑界面

     3.我们需要在application.yaml配置文件中开启国际化spring.messages.basename=i18n.login

    spring:
      messages:
        basename: i18n.login  #配置文件的真实位置

     4.这样我们静态页面就可以通过#{}读取我们设定的值了。如:

     th:href="@{/index.html(l='zh_CN')}意思:跳转到根目录下的index.html?l=zh_CN

    <!--读取使用的值是默认的-->
    <p  th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
    <label  th:text="#{username}"></label>
    
    <!--下方为2个按钮,用于切换区域语言的,也就是我们想要的-->
    <a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>

     5.自定义区域解析器(实现LocaleResolver接口)(主要是由它来做切换的)

    public class MyLocaleResolver implements LocaleResolver {
        @Override
        public Locale resolveLocale(HttpServletRequest httpServletRequest) {
            String l = httpServletRequest.getParameter("l");
            Locale defaultLocale = Locale.getDefault();
            if(!StringUtils.isEmpty(l)){
                String[] strs=l.split("_");//zh,CN
                defaultLocale=new Locale(strs[0],strs[1]);
            }
            return defaultLocale;
        }
    
        @Override
        public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
    
        }
    }

    6.丢到主配置类中,作为一个组件Bean

    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

    到了这里我们启动程序,显示两个按钮,我们点击哪个就会切换到哪个啦

  • 相关阅读:
    Object类中常见的方法,为什么wait notify会放在Object里边
    什么是Java序列化和反序列化,如何实现Java序列化
    hashCode和equals方法的区别与联系
    MVC设计思想
    dstat命令--检查linux系统性能
    linux mint19.1解决网易云音乐安装后打不开的问题
    linux 修改用户密码
    MD5加密
    xmpp
    cocoapods安装
  • 原文地址:https://www.cnblogs.com/ningxinjie/p/13378867.html
Copyright © 2011-2022 走看看