在eclipse中javaEE環境下:導入必要的架包
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 配置SpringMVC的DispatcherServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置 HiddenHttpMethodFilter: 把 POST 請求轉為 DELETE、PUT 請求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
spring的bean的配置文件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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置自動掃描的包 --> <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- default-servlet-handler 將在 SpringMVC 上下文中定義一個 DefaultServletHttpRequestHandler, 它會對進入 DispatcherServlet 的請求進行篩查, 如果發現是沒有經過映射的請求, 就將該請求交由 WEB 應用服務器默認的 Servlet 處理. 如果不是靜態資源的請求,才由 DispatcherServlet 繼續處理 一般 WEB 應用服務器默認的 Servlet 的名稱都是 default. 若所使用的 WEB 服務器的默認 Servlet 名稱不是 default,則需要通過 default-servlet-name 屬性顯式指定 --> <mvc:default-servlet-handler/> <!-- 一般都會配置這個 <mvc:annotation-driven ></mvc:annotation-driven>, 由於。。。requestmapping請求實現不了,使用這個,會使requestmapping請求一定實現 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置國際化資源文件 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18n"></property> </bean> <!-- 下面兩個實現國際化的配置,可以實現中英文在頁面上的跳轉切換, 只需在這兒配置和在i18n.jsp頁面中添加兩個超鏈接<br><br> <a href="i18n?locale=zh_CH">中文</a> <a href="i18n?locale=en_US">英文</a> --> <!-- 配置 SessionLocalResolver(實現國際化時使用) --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean> <!-- 配置LocaleChanceInterceptor(也是實現國際化的時候使用) --> <mvc:interceptors> <!-- 配置LocaleChanceInterceptor(也是實現國際化的時候使用) --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean> </mvc:interceptors> <!-- 使用這個標簽可以不用 handler方法,直接跳轉到某個頁面頁面 (因為此xml文件中有視圖解析器,所以,只需寫需要跳轉到的jsp頁面的名字即可) <mvc:view-controller path="/i18n" view-name="i18n"/>--> <mvc:view-controller path="/i18n2" view-name="i18n2"/> </beans>
實現國際化的file文件:這兒有三個,開發時可以寫多個;
1)i18n.properties
i18n.user=User
i18n.password=Password
2)i18n_zh_CN.properties,中文的user和password
i18n.user=\u7528\u6237\u540D
i18n.password=\u5BC6\u7801
3)i18n_en_US.properties ,英文的user和password
i18n.user=User
i18n.password=Password
是國際化的handler類方法:這兒可以不使用該方法,直接在springmvc.xml文件中進行配置就行,為了驗證,所以使用了此方法,輸出國際化的user值
package com.atguigu.springmvc.crud.test; @Controller public class SpringMVCTest { @Autowired private EmployeeDao employeeDao; //實現國際化的類; @Autowired private ResourceBundleMessageSource messageSource; //實現國際化的方法,可以打印user @RequestMapping("/i18n") public String testI18n(Locale locale){ String val=messageSource.getMessage("i18n.user", null, locale); System.out.println(val); return "i18n"; } }
三個jsp頁面
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> <center> <!-- 關於國際化: 1. 在頁面上能夠根據瀏覽器語言設置的情況對文本(不是內容), 時間, 數值進行本地化處理 2. 可以在 bean 中獲取國際化資源文件 Locale 對應的消息 3. 可以通過超鏈接切換 Locale, 而不再依賴於瀏覽器的語言設置情況 解決: 1. 使用 JSTL 的 fmt 標簽 2. 在 bean 中注入 ResourceBundleMessageSource 的示例, 使用其對應的 getMessage 方法即可 (即在springmvc.xml文件中配置), 3. 配置 LocalResolver 和 LocaleChangeInterceptor 4.建立。。。.properties的配置文件 國際化實現的步驟: 需要有file文件,需要在springmvc.xml文件中配置,需要在jsp頁面中使用jstl標准標簽庫, 亦可以在handler方法中獲取.properties文件中的值 --> <br><br> <a href="i18n">To i18n page</a> </center> </body> </html>
i18n.jsp:需要導入jstl標准標簽庫,使用fmt這個標簽(國際化 標簽),鼠標點擊中文/英文,可以試問user和password值的中英文切換
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>
<fmt:message key="i18n.user"></fmt:message>
<br><br>
<a href="i18n2">To i18n2 page</a>
<br><br>
<a href="i18n?locale=zh_CH">中文</a>
<br><br>
<a href="i18n?locale=en_US">英文</a>
</body>
</html>
i18n2.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>
<fmt:message key="i18n.password"></fmt:message>
<br><br>
<a href="i18n">To i18n page</a>
</body>
</html>