SpringMVC + Freemarker 国际化

来源:互联网 发布:淘宝卖家如何发货 编辑:程序博客网 时间:2024/05/20 22:26

1.xml配置文件

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="configLocation" value="classpath:conf/freemarker.properties" />
        <property name="templateLoaderPath" value="/pages/" />
        <property name="defaultEncoding" value="UTF8" />
        <property name="freemarkerVariables">
            <map>
<!--                <entry key="picHostUrl" value="${upload.server.url}" />
                <entry key="defaultImage" value="${default.image}" />-->
                <entry key="buildNumber" value="${deployDate}" />
                <entry key="auto_import" value="layout/spring.ftl as spring" />
            </map>
        </property>
    </bean>


    <mvc:resources mapping="/statics/**" location="/statics/" cache-period="31536000"/>


<!-- 拦截器配置,拦截顺序:先执行后定义的,排在第一位的最后执行。-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>


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



<!-- 配置国际化资源文件路径 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
  <!-- 定义消息资源文件的相对路径 -->
<value>messages/messages</value>
</property>
</bean>

2.messages/messages目录下建立属性文件

hi=hello
something=The People's Republic of China
Chinese=Chinese
English=English
index=Index
welcome=Welcome
OtherPage=Other Page


hi=\u4F60\u597D
something=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD
Chinese=\u4E2D\u6587
English=\u82F1\u6587
OtherPage=\u5176\u4ED6\u9875\u9762
index=\u9996\u9875
welcome=\u6B22\u8FCE

3.切换的类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping(value = "/global")
public class GlobalController {

    @RequestMapping(value="/changeLocale", method = {RequestMethod.GET})
    public String test(HttpServletRequest request,Model model, @RequestParam(value="langType", defaultValue="zh") String langType){
        if(langType.equals("zh")){
            Locale locale = new Locale("zh", "CN");
            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
        }
        else if(langType.equals("en")){
            Locale locale = new Locale("en", "US");
            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
        }
        else
            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());

        return "redirect:"+request.getParameter("url");
    }

}


java类中引入如下:

@Autowired

protected MessageSource messageSource;

4.

原创粉丝点击