SPring MVC i18n 国际化使用流程Demon(Maven项目的搭建和web、spring框架的添加)

第一次接触国际化i18n,写了一个小demon测试中英文切换。Demon使用Spring框架,从创建Maven项目到搭建框架到业务层代码的编辑以下都记录下来了。


1.创建Maven项目--SpringMVCi18n



2.添加web 框架
  • property--->Macen-->Project Facets--->添加web框架
  • Project Facet 选择Dynamic web Module ,Java 
  • Runtimes 选择  Apache  Tomcat V7
  • Further configuration available 点击输入 src/main/webapp
  • 添加 source folder  ,  src/main/resource 、src/test/resource;
  • Build path-->configure build path-->Java Build path-->设置包属性
  • Build path-->configure build path-->Deployment Assembly-->支配程序集 依赖包
  • pom.xml  jar-->war
<?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
</web-app>


3.添加spring框架
  • 添加spring依赖
    • 根据需求添加对应的依赖
    • javax.servlet-api
    • jsp-api
    • jstl
    • spring-core
    • spring-aop
    • spring-beans
    • spring-context
    • spring-context-support
    • spring-expression
    • spring-jdbc
    • spring-test
    • spring-tx
    • spring-web
    • spring-webmvc
    • aopalliance
  • web.xml添加spring监听
    
<listener >
        <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >
    </listener >


  • 配置spring的配置文件 applicationContext.xml  (可以选择MVC另起一个配置文件)
  • web.xml配置文件 
-------以上Maven项目构建成功------
-------接下来测试i18n国际化-------

4.国际化测试中英文转换
  • 相关方面的基本知识可网上了解  http://blog.csdn.net/zhoudaxia/article/details/37536195
  • 项目包层次

  • 只测试静态国际化数据,动态需要储存在数据库。

5.applicationContext.xml配置相应信息
          
<!-- 配置国际化对应信息 -->
    <bean id = "messageSource"
        class= "org.springframework.context.support.ResourceBundleMessageSource" >
        <!-- 国际化信息所在的文件名 -->
        <property name = "basenames">
            <list >
                <value >i18n/message </value >
            </list >
        </property >
        <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
        <property name = "useCodeAsDefaultMessage" value= "true" />
    </bean >

    <mvc:interceptors >
        <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
        <bean
            class= "org.springframework.web.servlet.i18n.LocaleChangeInterceptor"  />
    </mvc:interceptors >

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


6.Control代码
     
@RequestMapping ("/test" )
    public ModelAndView defaultjsp (
            HttpServletRequest request,
            @RequestParam( value = "langType" , defaultValue = "zh" )String langType) {
        ModelAndView view = new ModelAndView( "first" );

        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 ());

        // 从后台代码获取国际化信息
        RequestContext requestContext = new RequestContext (request );
        view. addObject( "name" , requestContext .getMessage ("name" ));
        view. addObject( "address" , requestContext .getMessage ("address" ));

        return view;
    }

7.jsp页面
            
 <table border = "2">

             <tr >
                   <td colspan = "2">< span style=" float: right" > <a
                               href= " <%=path %> /test?langType=en"> en</ a> | < a
                               href= " <%=path %> /test?langType=zh"> zh</ a>
                   </span ></td >
             </tr >
             <tr >
                   <td >${name } </td >
                   <td ></td >
             </tr >

             <tr >
                   <td >${address } </td >
                   <td ></td >
             </tr >

       </table >





你可能感兴趣的:(spring,maven,mvc,国际化)