zoukankan      html  css  js  c++  java
  • springMVC之国际化

    1、工程结构

    2、jar包

    3、配置文件spring-config.xml,springMVC配置文件

    <?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:oxm="http://www.springframework.org/schema/oxm"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/oxm
           http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/aop 
             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
        <!--  通知spring容器通过注解的方式装配bean --> 
          <context:annotation-config /> 
        <!--  通知spring容器采用自动扫描机制查找注解的bean --> 
          <context:component-scan base-package="com.*" /> 
        
         
        <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>

    4、spring-context.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:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        
       
       <context:component-scan base-package="com.*" />    
        
       <!-- 定义国际化消息-->   
       <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">   
         
         <!--   其中basename用来指定properties文件的通用名
                  如实例中的message_en_US.properties,message_zh_CN.properties通用名都是message
          -->
         <property name="basename" value="message"/>
         <property name="useCodeAsDefaultMessage" value="true" />
         
       </bean>   
        
    </beans>
    注:<property name="basename" value="message"/>,这里的message即为国际化内容配置文件的前缀

    5、web.xml,需要在这里配置对国际化的监听,加载国际化配置文件

    <?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" 
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
          <display-name>spring</display-name>
          <!-- 加载国际化配置 -->
          <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-context.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
          
          <!-- log4j配置文件路径 -->
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/log4j.properties</param-value>
        </context-param>
    
        <context-param>
            <param-name>log4jRefreshInterval</param-name>
            <param-value>6000</param-value>
        </context-param>
        
        <!-- 加载log4j配置文件 -->
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
        
        <!-- springmvc配置 -->
          <servlet>
            <servlet-name>springMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springMVC</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    6、index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>index</title>
      </head>
      
      <body>
        
        <!-- 使用message 标签配置需要显示的国际化文本, 
               code  对应国际化文件中对应的键的名称  -->
        <span style="color: #2D2D2D;">
            <spring:message code="main.title"/>
        </span>
        <br>
        <input type="text" value="<spring:message code="main.target"/>">  
      </body>
    </html>

    7、message_zh_CN.properties

    main.title=u4e2du56fd 
    main.target=u6211u7231u4f60 

    8、message_en_US.properties

    main.title=u4e2du56fd 
    main.target=u6211u7231u4f60

    9、页面结果

  • 相关阅读:
    gulp serve 报错 gulp.ps1
    执行git命令时出现fatal: 'origin' does not appear to be a git repository错误
    利用 SASS 简化 `nth-child` 样式的生成
    git的一些常用命令
    回调函数
    匿名函数
    css消除行内元素的间隙
    @click.native的使用
    Element-ui 下拉列表 选项过多时如何解决卡顿问题
    vue组件通信(父子之间,兄弟之间)
  • 原文地址:https://www.cnblogs.com/sunjf/p/spring_international.html
Copyright © 2011-2022 走看看