Spring mvc下国际化的实现

本文介绍如何在Spring框架中实现国际化,包括配置web.xml和spring-config.xml,创建资源文件及使用spring:message标签显示国际化消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前一直用的是struts的国际化方法。现在尝试一下spring下面的国际化。具体步骤如下:

1.web.xml中增加

<jsp-config>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/resources/spring-form.tld</taglib-location>
</taglib>
</jsp-config>

这样jsp中就能识别spring:message标签了

2. spring-config.xml 中增加

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>

如果需要有多个资源文件,比如 异常是一个,页面文字是一个的话,这样写

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

<property name="basenames">

<list>

<value>error</value>

<value>message</value>

</list>

</property>

</bean>

3. 具体的jsp中增加

标签库引用 <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

具体标签 <spring:message code="system.name" />

4. 在src 下建立资源文件: messages.properties

里面记得写unicode的值,如

username.illegal=\u7528\u6237\u540d\u4e0d\u5408\u6cd5\uff0c\u5fc5\u987b\u4e3a\u5b57\u6bcd\u6570\u5b57\u7ec4\u6210\uff0c\u957f\u5ea6\u4e3a5-10

如果有多国语言的话,增加资源文件 messages_zh_CN.properties messages_en_US.properties

打包后的系统,资源文件请放在 WEB-INF下

如果出现一下问题:

org.springframework.context.NoSuchMessageException:Nomessagefoundundercode'XXXXXX'forlocale'en_US'.
如果你使用eclipse的话,应该把属性文件放在src文件夹下,而不是工程下,或者是因为文件名不对messages.properties messages_zh_CN.properties messages_en_US.properties

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值