SpringMVC-国际化

1.通过浏览器语言设置国际化

1.1 新建jsp对应的国际化属性资源文件
在这里插入图片描述
在这里插入图片描述
1.2 配置springmvc.xml文件,将国际化支持和资源文件都注入springmvc中

 <!--设置国际化支持 配置国际化属性资源文件-->
    <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
        <property name="basenames">
            <array>
            <!--如果还有别的jsp就在这配置-->
                <value>i18n/login</value>
            </array>
        </property>
        <!-- 支持UTF-8的中文 -->
        <property name="cacheSeconds" value="0"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

1.3 在jsp页面调用对应的属性资源内容
<spring:message code=“绑定属性资源文件中对应key”></spring:message>

<%--
  Created by IntelliJ IDEA.
  User: MSI-NB
  Date: 2021/12/20
  Time: 0:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% request.setAttribute("basePath",request.getContextPath()); %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
    <title>登录界面</title>
</head>
<body>
<spring:message code="txt_Login"></spring:message>

<p></p>

<spring:message code="txt_Register"></spring:message>

<p></p>



</body>
</html>

1.4结果显示

在这里插入图片描述在这里插入图片描述

在这里插入图片描述
2.通过超链接来切换国际化

2.1 更改默认本地化语言解析器LocaleResolver改成SessionLocaleResolver

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

2.2 方式一:创建一个请求接受local参数(en_US),接受到就设置session中

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

        return "login";
    }

结果显示:在这里插入图片描述
在这里插入图片描述

2.3 方式二:使用springmvc提供的拦截器接受local参数(en_US),接受到就设置session中

<!--方法二:使用springmvc提供的拦截器,接收local参数 设置session中去-->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>

结果显示:
在这里插入图片描述
在这里插入图片描述
注意拦截器的方式与更改默认本地化语言解析器参数的请求方式不同,两个同时配置时请求需要注意

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值