在SpringMVC中用fmt标签实现国际化/多语言

SpringMVC中用fmt标签实现国际化主要解决界面的多语言化,ftm标签会根据浏览器的语言值来先择对应的文件配置,如中文简体的浏览器值是zh_CN,那么ftm标签就会用以zh_CN.properties结尾的配置文件中的key来取值,从而实现自多语言的自动切换,配置应用如下:

  1. 引用JSTL相关依赖包

    <dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.0.1</version>
    			<scope>provided</scope>
    </dependency>
    <!-- jstl相关jar包 -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>jstl</artifactId>
    			<version>1.2</version>
    		</dependency>
    
    		<dependency>
    			<groupId>taglibs</groupId>
    			<artifactId>standard</artifactId>
    			<version>1.1.2</version>
    		</dependency>

  2. 在Springmvc配置文件中国际化bean
     

    	<bean id="messageSource"
    		class="org.springframework.context.support.ResourceBundleMessageSource">
    		<!-- 表示语言配置文件是以language开头的文件,(如果是简体中文语言的浏览器则会读取language_zh_CN.properties文件) -->
    		<property name="basename" value="language" />
    	</bean>

  3. 创建language.properties文件,并同时创建对应语言语文件,如简体中文则为zh_CN.properties结尾,美国英文则为 en_US.properties结尾
     

    -----language.properties------
    
    language.username=username
    language.password=password
    
    
    -----language_en_US.properties-----
    language.username=Username
    language.password=Password
    
    
    ------language_zh_CN.properties-----
    
    language.username=\u7528\u6237\u540D
    language.password=\u5BC6\u7801
    

    如图:

  4. 在JSP页面中使用
     

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
       <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>国际化语言</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/testConsumes" method="post">
    <fmt:message key="language.username"></fmt:message><input  type="text" name="username" value="">
    <fmt:message key="language.password"></fmt:message><input  type="text" name="password" value="">
    <input type="submit" value="提交">
    </form>
    当前语言: ${pageContext.response.locale}
    </body>
    </html>

  5. 测试结果

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值