一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java SpringMVC實(shí)現(xiàn)國際化整合案例分析(i18n)

Java SpringMVC實(shí)現(xiàn)國際化整合案例分析(i18n)

2020-09-22 10:37Joker_Ye Java教程

本篇文章主要介紹了Java SpringMVC實(shí)現(xiàn)國際化整合案例分析(i18n),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

所謂國際化就是支持多種語言,web應(yīng)用在不同的瀏覽環(huán)境中可以顯示出不同的語言,比如說漢語、英語等。下面我將以具體的實(shí)例來舉例說明:

(1)新建動(dòng)態(tài)Java web項(xiàng)目,并導(dǎo)入幾個(gè)SpringMVC必需的幾個(gè)jar包,項(xiàng)目結(jié)構(gòu)圖和所需jar包如下:

Java SpringMVC實(shí)現(xiàn)國際化整合案例分析(i18n)

(2)配置web.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
 
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
   
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

常規(guī)配置,沒有什么特殊的地方,不多解釋

(3)SpringMVC的配置文件springmvc-servlet.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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: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-4.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-4.0.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 
  <context:component-scan base-package="cn.zifangsky.* *.controller" />
 
  <context:annotation-config /> <!-- 激活Bean中定義的注解 -->
  <mvc:annotation-driven />
 
  <!-- 視圖相關(guān)配置 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" /> <!-- 視圖前綴 -->
    <property name="suffix" value=".jsp" /> <!-- 視圖后綴 -->
  </bean>
  <!-- 存儲(chǔ)區(qū)域設(shè)置信息 -->
  <bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
  <!-- 國際化資源文件 -->
  <bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
  </bean>
 
  <mvc:interceptors>
    <bean id="localeChangeInterceptor"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
      <property name="paramName" value="lang" />
    </bean>
  </mvc:interceptors>
</beans>

在上面的配置中,SessionLocaleResolver類通過一個(gè)預(yù)定義會(huì)話名將區(qū)域化信息存儲(chǔ)在會(huì)話中。緊接著的“messageSource”配置的是國際化資源文件的路徑,”classpath:messages”指的是classpath路徑下的messages_zh_CN.properties文件和messages_en_US.properties文件。在這個(gè)配置文件的最后配置的是一個(gè)攔截器,該攔截器通過名為”lang”的參數(shù)來攔截HTTP請(qǐng)求,使其重新設(shè)置頁面的區(qū)域化信息

(4)兩個(gè)國際化資源文件:

i)messages_zh_CN.properties文件:

?
1
2
3
4
language.cn = \u4e2d\u6587
language.en = \u82f1\u6587
internationalisation = \u56fd\u9645\u5316
welcome = \u6b22\u8fce\u8bbf\u95ee\u201c\u007a\u0069\u0066\u0061\u006e\u0067\u0073\u006b\u0079\u7684\u4e2a\u4eba\u535a\u5ba2\u201d\uff0c\u0055\u0052\u004c\uff1a\u0068\u0074\u0074\u0070\u003a\u002f\u002f\u0077\u0077\u0077\u002e\u007a\u0069\u0066\u0061\u006e\u0067\u0073\u006b\u0079\u002e\u0063\u006e

ii)messages_en_US.properties文件:

?
1
2
3
4
language.cn = Chinese
language.en = English
internationalisation = \u0020Internationalisation
welcome = Welcome to visit "zifangsky's personal blog",URL\uff1ahttp://www.zifangsky.cn

注:上面一些看起來“亂碼”的地方實(shí)際上是經(jīng)過Unicode編碼的

(5)后臺(tái)處理請(qǐng)求的controller:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.zifangsky.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class I18nController {
 
  @RequestMapping(value = "/hello")
  public ModelAndView welcome() {
    ModelAndView modelAndView = new ModelAndView("welcome");
 
    return modelAndView;
  }
 
}

這個(gè)controller很簡單,就是轉(zhuǎn)到一個(gè)視圖頁面welcome.jsp

(6)首頁的index.jsp:

?
1
<% response.sendRedirect("hello.html"); %>

意思很簡單,就是項(xiàng)目啟動(dòng)之后就請(qǐng)求htllo.html,也就是讓controller中的welcome方法處理這個(gè)請(qǐng)求

(7)welcome.jsp:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>SpringMVC<spring:message code="internationalisation" /></title>
</head>
<body>
  Language: <a href="?lang=zh_CN" rel="external nofollow" ><spring:message code="language.cn" /></a> - <a href="?lang=en_US" rel="external nofollow" ><spring:message code="language.en" /></a>
  <h2>
    <spring:message code="welcome" />
  </h2>
  Locale: ${pageContext.response.locale }
</body>
</html>

可以看出,在需要使用國際化處理的地方都使用了spring的message標(biāo)簽,code屬性對(duì)應(yīng)資源文件中的“鍵”名稱

(8)最后的顯示效果如下:

i)中文:

Java SpringMVC實(shí)現(xiàn)國際化整合案例分析(i18n)

ii)英文:

Java SpringMVC實(shí)現(xiàn)國際化整合案例分析(i18n)

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/hj7jay/article/details/51383248

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 三级伦理影院 | 亚洲国产欧美目韩成人综合 | 精品视频99 | 红楼梦黄色小说 | 无耻三级在线观看 | 男人肌肌捅女人 | 免费观看国产精品 | 故意短裙公车被强好爽在线播放 | 日本手机在线视频 | www.亚洲视频| 大伊香蕉精品视频一区 | 99久久999久久久综合精品涩 | 欧美视频一区二区三区四区 | 99精品久久精品一区二区小说 | 国产精品久久久 | 手机在线伦理片 | 亚洲伦理天堂 | 美女毛片在线 | 国产原创一区二区 | 色聚网久久综合 | 亚洲一二三区久久五月天婷婷 | 成年人在线观看视频 | 隔壁老王国产精品福利 | 欧美撒尿屁股嘘嘘撒尿 | 国产nv精品你懂得 | 日本中文字幕一区二区高清在线 | 国产成人精品1024在线 | 特级淫片大乳女子高清视频 | 日韩无遮挡大尺度啪啪影片 | chanelpreston欧美网站 | 日韩aⅴ在线观看 | 男男同gayxxx | 草草精品视频 | 香蕉免费一区二区三区 | 国产欧美另类 | 国产激情视频在线 | 99精品视频一区在线观看miya | 日本不卡视频免费的 | 嫩草影院永久入口在线观看 | 911精品国产亚洲日本美国韩国 | 免费一区二区 |