zoukankan      html  css  js  c++  java
  • SpringMVC国际化配置

    一、什么是国际化:

      国际化是设计软件应用的过程中应用被使用与不同语言和地区
      国际化通常采用多属性文件的方式解决,每个属性文件保存一种语言的文字信息,
        不同语言的用户看到的是不同的内容

    二、springmvc.xml的配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.ssm.controller" />
        <mvc:annotation-driven />
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- 存储区域设置信息 SessionLocaleResolver类通过一个预定义会话名将区域化信息存储在会话中 从session判断用户语言defaultLocale 
            :默认语言 -->
        <bean id="localeResolver"
            class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
            <property name="defaultLocale" value="zh_CN" />
        </bean>
    
        <!-- 国际化资源文件 messageSource配置的是国际化资源文件的路径, classpath:messages指的是classpath路径下的 
            messages_zh_CN.properties文件和messages_en_US.properties文件 设置“useCodeAsDefaultMessage”,默认为false,这样当Spring在ResourceBundle中找不到messageKey的话,就抛出NoSuchMessageException, 
            把它设置为True,则找不到不会抛出异常,而是使用messageKey作为返回值。 -->
        <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="defaultEncoding" value="UTF-8" />
            <property name="useCodeAsDefaultMessage" value="true" />
            <property name="basenames">
                <list>
                    <value>classpath:resource/message</value>
                </list>
            </property>
        </bean>
        <!--该拦截器通过名为”lang”的参数来拦截HTTP请求,使其重新设置页面的区域化信息 -->
        <mvc:interceptors>
            <bean id="localeChangeInterceptor"
                class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="lang" />
            </bean>
        </mvc:interceptors>
    </beans>

    三、国际化语言属性文件

    我的路径是这样,其他的都可以

    1. message_en_US.properties

    TITLE = BEGIN TO START
    USERNAME = UserName
    PASSWORD = PassWord
    LOGIN = Login

    2. message_zh_CN.properties

    TITLE = 开始冒险之旅
    USERNAME = 账号:
    PASSWORD = 密码:
    LOGIN = 登录

    四、编写Controller

    @Controller
    public class NewsController {
        
        @RequestMapping(value = "/getLogin.html")
        public String getLogin(){
            return "result";
        }
    }

    五、前端编写并测试

    1. 文件位置

    2. index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="getLogin.html?lang=zh_CN">中文</a>
    <br />
    <a href="getLogin.html?lang=en_US">英文</a>
    </body>
    </html>

    3. result.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
       <div class="login">
          <h1><spring:message code="TITLE" /> </h1>
          <form action="test.do" method="post">
             <input type="text" name="name" placeholder=<spring:message code="USERNAME" /> required="required" value="" />
             <input type="password" name="password" placeholder=<spring:message code="PASSWORD" /> required="required" value="" />
             <button type="submit" class="btn btn-primary btn-block btn-large"><spring:message code="LOGIN" /> </button>
          </form>
       </div>
    </body>
    </html>

    4. 测试

  • 相关阅读:
    第二十一章流 1流的操作 简单
    第二十章友元类与嵌套类 1友元类 简单
    第十九章 19 利用私有继承来实现代码重用 简单
    第二十章友元类与嵌套类 2嵌套类 简单
    第十九章 8链表类Node 简单
    第二十一章流 3用cin输入 简单
    第十九章 10 图书 药品管理系统 简单
    第十九章 11图书 药品管理系统 简单
    第二十一章流 4文件的输入和输出 简单
    第十九章 12 什么时候使用私有继承,什么时候使用包含 简单
  • 原文地址:https://www.cnblogs.com/ya-qiang/p/9388238.html
Copyright © 2011-2022 走看看