SpringMvc(国际化)

1、根据浏览器设置语言来实现国际化

通过AcceptHeaderLocaleResolver类在视图解析器时实现的国际化

步骤:

1、新建jsp对应的国际化属性资源文件
login.properties、login_zh_CN.properties、login_en_US.properties
2、配置spring的配置文件,将国际化支持 和 第一步的国际化属性资源文件都注入到spring的配置文件中
3、在jsp页面调用对应的属性资源内容:

<spring:message code="txt.welcome"></spring:message>

在这里插入图片描述

<!-- 展示页面 -->
<form>
    <h1 class="h3 mb-3 fw-normal"><spring:message code="txt.welcome"></spring:message></h1>

    <div class="form-floating">
        <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
        <label for="floatingInput"><spring:message code="txt.emailOne"></spring:message></label>
    </div>
    <div class="form-floating">
        <input type="password" class="form-control" id="floatingPassword" placeholder="<spring:message code="txt.password"></spring:message>">
        <label for="floatingPassword"><spring:message code="txt.password"></spring:message></label>
    </div>

    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> <spring:message code="txt.remember"></spring:message>
        </label>
    </div>
    <button class="w-100 btn btn-lg btn-primary" type="submit"><spring:message code="txt.login_in"></spring:message></button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017–2021</p>
</form>
login.properties、login_zh_CN.properties
txt.emailOne=邮箱地址
txt.login_in=注册
txt.password=密码
txt.remember=记住我
txt.welcome=小乐乐网站欢迎你

login_en_US.properties
txt.emailOne=email address
txt.login_in=Login in
txt.password=Password
txt.remember=Remember me
txt.welcome=Welcome to xiaolele website
<!-- spring配置文件 -->
<!-- 设置国际化文本资源路径 -->
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
    <property name="basename">
        <array>
            <value>i18n/login</value>
        </array>
    </property>
</bean>

2、通过超链接实现国际化

SpringMvc为我们提供了一个SessionLocaleResolver类来解决国际化一直保持状态的类,是从session里面取出国际化当前状态。
1、更改默认本地化语言解析器 LocaleResolver 改成 SessionLocaleResolver,SpringMvc 提供了 SessionLocaleResolver 类,可以覆盖掉 LocaleResolver (用来根据浏览器语言去设置国际化文本),引入 LocaleResolver 类如下:
2、有两种方式。

<!-- 使用 SessionLocaleResolver 保持 Locale 的状态  会从 session 中获取 locale 对象 -->
<bean class="org.springframework.web.servlet.i18n.SessionLocaleResolver" id="localeResolver"></bean>

2.1、自定义控制器实现国际化

所谓自定义控制器实现国际化,其实就是自己定义一个控制器给session赋值,代码示例如下:

<!-- 展示页面 -->
<form>
    <h1 class="h3 mb-3 fw-normal"><spring:message code="txt.welcome"></spring:message></h1>
    <a href="${pageContext.request.contextPath}/i18n/zh_CN">中文</a>
    <a href="${pageContext.request.contextPath}/i18n/en_US">英文</a>
    <div class="form-floating">
        <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
        <label for="floatingInput"><spring:message code="txt.emailOne"></spring:message></label>
    </div>
    <div class="form-floating">
        <input type="password" class="form-control" id="floatingPassword" placeholder="<spring:message code="txt.password"></spring:message>">
        <label for="floatingPassword"><spring:message code="txt.password"></spring:message></label>
    </div>

    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> <spring:message code="txt.remember"></spring:message>
        </label>
    </div>
    <button class="w-100 btn btn-lg btn-primary" type="submit"><spring:message code="txt.login_in"></spring:message></button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017–2021</p>
</form>
// 控制器代码

@RequestMapping("/i18n/{language}_{country}")
public String changeLocale(@PathVariable("language") String language,
                           @PathVariable("country") String country,
                           HttpServletRequest request,
                           HttpServletResponse response,
                           @Autowired SessionLocaleResolver localeResolver){
    Locale locale = new Locale(language,country);
    localeResolver.setLocale(request,response,locale);
    return "login";
}

2.2、使用SpringMvc提供的类做配置

因为这个类会拦截所有的mvc请求,当发现传入国际化参数时,就会去改变session中的值来改变国际化文本。

<form>
    <h1 class="h3 mb-3 fw-normal"><spring:message code="txt.welcome"></spring:message></h1>

    <a href="?locale=zh_CN">中文_拦截器</a>
    <a href="?locale=en_US">英文_拦截器</a>

    <div class="form-floating">
        <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
        <label for="floatingInput"><spring:message code="txt.emailOne"></spring:message></label>
    </div>
    <div class="form-floating">
        <input type="password" class="form-control" id="floatingPassword" placeholder="<spring:message code="txt.password"></spring:message>">
        <label for="floatingPassword"><spring:message code="txt.password"></spring:message></label>
    </div>

    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> <spring:message code="txt.remember"></spring:message>
        </label>
    </div>
    <button class="w-100 btn btn-lg btn-primary" type="submit"><spring:message code="txt.login_in"></spring:message></button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017–2021</p>
</form>
<!-- 使用SpringMvc提供的拦截器,接收local参数(en_US、zh_CN)  设置 session 中去 -->
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>

3、解决java中硬编码的国际化

前提是必须要设置了国际化文本。

1、在属性资源文件中加入需要国际化的硬编码内容

在这里插入图片描述

2、将 MessageSource 自动注入进来
3、根据 MessageSource.getMessage 获取国际化内容

第一个参数:代表国际化文本的key值
第二个参数:可以给占位符赋值
第三个参数:获取到当前的国际化文本标志

@Autowired
MessageSource messageSource;

@PostMapping("/user")
public String login(User user, Model model, Locale locale){
    // 验证用户名密码是否正确
    if(!user.getEmail().equals("123@qq.com") ||
    !user.getPassword().equals("123456")){
        Object[] params = {user.getEmail(),user.getPassword()};
        System.out.println(messageSource.getMessage("login_error",params,locale));
        return "login";
    }
    return "admin";
}

4、验证信息的国际化

springMvc的数据处理
详情可看数据处理

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.1.5.Final</version>
</dependency>

就好比对下面注解的国际化

@NotEmpty(message = "数据不能为空")
private String id;

可以在国际化文本中加入以下格式的国际化文本样式即可:
key :注解.对象.属性
NotEmpty.user.id = “id is not empty.”

5、发生类型不匹配时的国际化文本

typeMismatch:在数据类型绑定的时候,发生数据类型不匹配时,可自定义抛错内容。在国际化文本中如下方式添加:
typeMismatch.user.birthday = Date format is error.

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值