Spring在代码中国际化

    Spring在jsp中国际化使用国际化标签很方便,但在java类中国际化,还得处理下。在这稍整理,以备后查。


   1、新建类。

package cn.tisson.ichp.lifecaring.util;


import javax.servlet.ServletContext;


import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class WebContext implements ServletContextAware {
private ServletContext servletContext;


@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}


public WebApplicationContext getContext() {
WebApplicationContext applicationContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
return applicationContext;
}


}

   2、在配置文件中声明。

        <bean id="webContext" class="cn.tisson.ichp.lifecaring.util.WebContext"/>

   3、在java代码中调用。

        @Autowiredprivate 

        WebContext webContext; 

        webContext.getContext().getMessage("test", null, request.getLocale());


另备上简便实现:

 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
String message = webApplicationContext.getMessage("test", null, request.getLocale());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值