SpringMvc国际化

国际化资源命名规范 基名_语言_地区.properties
地区可省 基名_语言.properties

springmvc需要使用国际化需要导入二个jar包jstl 和standard

 <!--    国际化需要的二个jar包    -->

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

创建二个区域文件
i18n_en_US.properties

welcomelog=Welcome to the login page
usernamelog=username
passwordlog=password
loginlog=login

i18n_zh_CN.properties

welcomelog=欢迎来到登录页面
usernamelog=用户名
passwordlog=密码
loginlog=登录

项目架构
在这里插入图片描述
applicationContext-springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">


    <context:component-scan base-package="com.stone.mvc"/>


    <!--    配置视图解析器 InternalResourceViewResolver-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--    prefix配置前置  -->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <!--    suffix配置后置  -->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--  springmvc 高级注解驱动  -->
    <!-- 使用mvc注解声明 -->
    <mvc:default-servlet-handler/>

    <!-- 处理静态资源(js,css,html) -->
    <mvc:annotation-driven/>

    <!--    加载国际怀资源文件 id名必须为messageSource 因为springmvc在启动时会自动加载名为messageSource的bean  -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <!--    配置基名    -->
        <property name="basename" value="i18n.i18n"></property>

    </bean>


</beans>

Myi18n 类


@Controller
public class Myi18n
{

    //  自动注入ResourceBundleMessageSource类型的值
    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = "i18Test")
    public String i18Test(Locale locale){
        System.out.println("区域信息:"+locale);
        //  getMessage(key,占位符数组,区域信息)
        String welcomelog = messageSource.getMessage("welcomelog", null, locale);
        System.out.println(welcomelog);
        System.out.println("i18Test方法运行");
        return "i18n";
    }
}

首页 index.jsp
在这里插入图片描述
i18n.jsp 跳转页
在这里插入图片描述
中文语言下
在这里插入图片描述
在这里插入图片描述

英文语言下
在这里插入图片描述
在这里插入图片描述
方法打印信息
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值