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());
另备上简便实现:
String message = webApplicationContext.getMessage("test", null, request.getLocale());