今天使用SpringMVC写国际化代码,发现如下问题; 首先我配置了三个国际化代码资源文件
// i18n.properties i18n.username=username i18n.password=password // i18n_en_US.properties i18n.username=username i18n.password=password // i18n_zh_CN.properties i18n.username=\u7528\u6237\u540D i18n.password=\u5BC6\u7801然后配置Spring的配置文件:
<!-- 配置国际化资源文件 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18n"></property> </bean>然后分别放到两个页面使用fmt标签输出,这两个页面分别是:
/SpringMVCTest/WebContent/WEB-INF/views/success.jsp/SpringMVCTest/WebContent/hello.jsp 采用如下方式输出: <fmt:message key="i18n.username"></fmt:message> <fmt:message key="i18n.password"></fmt:message>结果发现,在两个页面运行结果如下:
// success.jsp 用户名 密码 // hello.jsp ???i18n.username??? ???i18n.password???为什么会出现上述问题呢?
查阅相关资料发现,因为success.jsp在WEB-INF下,不可以直接访问,要通过转发才可以访问到,而转发要经过 Handler 处理类。hello.jsp因为可以直接访问,并没有经过转发,所以无法使用国际化配置。而success.jsp因为使用了转发,转发过程要交给SpringMVC去处理,而SpringMVC的配置文件中配置了ResourceBundleMessageSource,所以可以访问国际化配置。
所以请谨记,
