spring国际化小结

    ResourceBundleMessageSource配置

<bean id="messageSource"

        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="messages"></property>
        <property name="useCodeAsDefaultMessage" value="true"></property>

    </bean>


message是你的properties文件的通用名,

例如中文messages_zh_CN.properties,英文messages_en_US.properties


如果想把properties文件放到某一个包下,如:com.diyjava.language下

那怎么配置呢

<property name="basenames" value="com.diyjava.language.messages"></property>


jsp文件

messages_zh_CN.properties内容如下

app.content= 哈哈,欢迎您!

那怎样国际化jsp呢

jsp引入spring标签

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

在需要显示地方输入如下标签

<spring:message code="app.content"/>


注:可以用jstl标签,如<fmt:message key="app.content" />



如果带有参数呢,那又怎么办呢?比如:哈哈,xxx,欢迎您!

messages_zh_CN.properties内容如下

app.content=哈哈, {0},欢迎您!


<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>


<spring:message code="app.content" arguments="${userName}"/>


userName是后台返回的参数


动态国际化


<bean id="localeChangeInterceptor"
		class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
		<property name="paramName" value="lang" />
	</bean>

	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

	<bean id="handlerMapping"
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="interceptors">
			<ref bean="localeChangeInterceptor" />
		</property>
	</bean>


	@Autowired
        private SessionLocaleResolver localeResolver;

        @RequestMapping("login.do")
	public String login(String local,HttpServletRequest request,HttpServletResponse response){
		if("zh".equals(local)){
			localeResolver.setLocale(request, response, Locale.CHINA);
		}else if("en".equals(local)){
			localeResolver.setLocale(request, response, Locale.US);
		}
		return "login";
	}

<div style="margin-left: 0px;">
			<a href="?local=en">英文</a>   <a href="?local=zh">中文</a>
		</div>

http://localhost:8080/MyTest2/login.do?local=en



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值