jsp进行 i18n国际化配置

下面是对jsp进行国际化配置的步骤:

1.在resources中新建一个messages的文件夹,新建两个properties,内容如下:

promptMessage_me_JD.properties:

#订单处理
orderCancel = 药品订单{0}取消失败
selectUserInfo = 查询药品【{0}】异常,异常信息【{1}】
user_name = hp
promptMessage_no_JD.properties:

#订单处理
orderCancel = 订单{0}取消失败
selectUserInfo = 查询用户【{0}】异常,异常信息【{1}】
user_name = 韩枫


2.在spring-MVC.xml中添加如下配置

<!-- 国际化支持 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>messages.promptMessage</value>
        </list>
    </property>
    <!-- 支持UTF-8的中文 -->
    <property name="cacheSeconds" value="0"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="messageSourceHelper" class="com.hanpeng.utils.MessageSourceHelper">
    <property name="messageSource" ref ="messageSource"></property>
</bean>
<!--实例化,设置默认版本-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="no_JD"></property>
</bean>
<!--国际化拦截器,拦截标识为lang>
<mvc:interceptors>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang"/>
    </bean>
</mvc:interceptors>

3.新建一个Demo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="source/js/jquery.min.js"></script>
</head>
<body>
<a href="?lang=no_JD">正常</a>
<a href="?lang=me_JD">医药</a>
<br/><br/><br/>
<spring:message code="order_name"/>:<input type="text" id="username" />
<spring:message code="order_no"/>:<input type="text" id="password" />
</body>
</html>

一定要引入<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
信息用<spring:message code="XXX"/>来获取

4.这就完成JSP国际化的配置了

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了很好的国际化i18n)支持,其核心是ResourceBundleMessageSource类,这个类实现了MessageSource接口,可以通过其提供的方法获取国际化的消息文本。国际化配置包含以下步骤: 1. 在Spring配置文件中配置ResourceBundleMessageSource Bean。 2. 创建属性文件,包含不同语言版本的消息文本。 3. 在JSP页面或者Java代码中使用MessageSource获取对应语言版本的消息文本。 具体操作如下: 1. 在Spring配置文件中添加如下配置: ``` <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"/> </bean> ``` basename属性指定了消息文件的基础名称,这里的值是“messages”,表示消息文件名为“messages.properties”(默认语言)和“messages_zh_CN.properties”(中文语言)。 2. 创建消息文件。 在classpath下创建一个名为“messages.properties”的文件,内容如下: ``` message.hello=Hello, World! ``` 然后在同一个目录下创建一个名为“messages_zh_CN.properties”的文件,内容如下: ``` message.hello=你好,世界! ``` 这里我们定义了一个名为“message.hello”的消息文本,分别提供了英文和中文两种语言版本。 3. 在JSP页面或者Java代码中使用MessageSource获取对应语言版本的消息文本。 在JSP页面中,可以使用spring:message标签获取消息文本: ``` <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <spring:message code="message.hello"/> ``` 在Java代码中,可以注入MessageSource,并使用其getMessage方法获取消息文本: ``` @Autowired private MessageSource messageSource; String message = messageSource.getMessage("message.hello", null, Locale.getDefault()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值