日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 监听器,国际化

發布時間:2023/12/18 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 监听器,国际化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.?監聽器

1.1?概述

監聽器: 主要是用來監聽特定對象的創建或銷毀、屬性的變化的!

?是一個實現特定接口的普通java類!

?

對象:

自己創建自己用 (不用監聽)

別人創建自己用 (需要監聽)

?

Servlet中哪些對象需要監聽?

request / session / servletContext

分別對應的是request監聽器、session相關監聽器、servletContext監聽器

?

監聽器(listener)

?

?

監聽器接口:

一、監聽對象創建/銷毀的監聽器接口

Interface ServletRequestListener?????監聽request對象的創建或銷毀

Interface HttpSessionListener????????監聽session對象的創建或銷毀

Interface ServletContextListener?????監聽servletContext對象的創建或銷毀

二、監聽對象屬性的變化

Interface ServletRequestAttributeListener?監聽request對象屬性變化: 添加、移除、修改

Interface HttpSessionAttributeListener????監聽session對象屬性變化: 添加、移除、修改

Interface ServletContextAttributeListener ?監聽servletContext對象屬性變化

?

三、session相關監聽器

Interface HttpSessionBindingListener???監聽對象綁定到session上的事件

????Interface HttpSessionActivationListener(了解) 監聽session序列化及反序列化的事件

?

?

404(路徑寫錯)

500(服務器錯誤,調試)

1.2?生命周期監聽器

聲明周期監聽器: 監聽對象的創建、銷毀的過程!

監聽器開發步驟:

1.?寫一個普通java類,實現相關接口;

2.?配置(web.xml)

?

A.??ServletRequestListener????

監聽request對象的創建或銷毀。

?

代碼:

/**

?* ?監聽request對象的創建或銷毀

?* @author?Jie.Yuan

?*

?*/

public?class?MyRequestListener implements?ServletRequestListener{

?

// 對象銷毀

@Override

public?void?requestDestroyed(ServletRequestEvent sre) {

// 獲取request中存放的數據

Object obj = sre.getServletRequest().getAttribute("cn");

System.out.println(obj);

System.out.println("MyRequestListener.requestDestroyed()");

}

?

// 對象創建

@Override

public?void?requestInitialized(ServletRequestEvent sre) {

System.out.println("MyRequestListener.requestInitialized()");

}

}

?

Web.xml

<!-- 監聽request對象創建、銷毀 -->

<listener>

<listener-class>cn.itcast.a_life.MyRequestListener</listener-class>

</listener>

?

?

?

B.?HttpSessionListener

監聽session對象的創建或銷毀。

?

C.?ServletContextListener

監聽servletContext對象的創建或銷毀。

?

?

1.3?屬性監聽器

監聽:request/session/servletContext對象屬性的變化!

ServletRequestAttributeListener

HttpSessionAttributeListener

ServletContextAttributeListener

?

總結:先寫類,實現接口; ?再配置

?

?

1.4 其他監聽器: session相關監聽器

?

?

HttpSessionBindingListener???

監聽對象綁定/解除綁定到sesison上的事件!

?

步驟:

對象實現接口; 再把對象綁定/解除綁定到session上就會觸發監聽代碼。

作用:

(上線提醒!)

?

/**

?* 監聽此對象綁定到session上的過程,需要實現session特定接口

?* @author?Jie.Yuan

?*

?*/

public?class?Admin implements?HttpSessionBindingListener {

???…?省略get/set

// 對象放入session

@Override

public?void?valueBound(HttpSessionBindingEvent event) {

System.out.println("Admin對象已經放入session");

}

// 對象從session中移除

@Override

public?void?valueUnbound(HttpSessionBindingEvent event) {

System.out.println("Admin對象從session中移除!");

}

}

?

?

思考:

這個session監聽器,和上面的聲明周期、屬性監聽器區別?

--à?不用再web.xml配置

???-à?因為監聽的對象是自己創建的對象,不是服務器對象!

?

?

?

1.4?案例

需求:做一個在線列表提醒的功能!

??????用戶--à?登陸

???????????????---à?顯示登陸信息,列表展示。(list.jsp)

???--à??顯示在線用戶列表 ???????(list.jsp)

-à?列表點擊進入“在線列表頁面” ??onlineuser.jsp

?

實現:

1.?先增加退出功能; ?再把session活躍時間1min;

2.?寫監聽器,監聽servletContext對象的創建: 初始化集合(onlineuserlist)

3.?登陸功能: 用戶登陸時候,把數據保存到servletContext

4.?List.jsp ?增加超鏈接, 點擊時候提交直接跳轉到online.jsp

5.?寫監聽器: 監聽session銷毀,把當前登陸用戶從onlineuserlist移除!

?

?

2.?國際化

Javaweb增強:過濾器、監聽器、國際化、文件上傳下載、javaMail

?

l?國際化又簡稱為 i18ninternationalization

?

國際化的人:

人,英語,漢語; ?可以說這個人是國際化的人;

?

軟件的國際化:

軟件

中國: 顯示中文,以及服務符合中國習慣的文本字符串!

?1999-09-09

美國: 顯示英文,以及服務符合他國習慣的文本字符串!

這種軟件,就叫國際化的軟件!

?

如何做到國際化的軟件,要求:

1.?軟件中存儲特定的字符串

2.?知道瀏覽器當前使用哪種語言(Locale ?

?

Locale ?本地化

Java提供了一個本地化的對象!封裝當前語言、國家、環境等特征!

?

?

public?class?App {

?

@Test

//1. 本地化對象:Locale

// 封裝語言、國家信息的對象,有java.util提供

public?void?testLocale() throws?Exception {

// 模擬中國語言等環境

//Locale?locale?= Locale.CHINA;

Locale?locale = Locale.getDefault(); // 當前系統默認的語言環境

System.out.println(locale.getCountry()); ?? // CN ?國家的簡稱

System.out.println(locale.getDisplayCountry()); // 國家名稱

System.out.println(locale.getLanguage()); // zh?語言簡稱

?

// 模擬美國國家

Locale?l_us = Locale.US;

System.out.println(l_us.getCountry());

System.out.println(l_us.getDisplayCountry());

}

}

?

?

國際化

靜態數據國際化

網站中顯示的固定文本的國際化: “用戶名”“密碼“

?

國際化的軟件:

1.?存儲所有國家顯示的文本的字符串

a)?文件: properties

b)?命名: ?基礎名_語言簡稱_國家簡稱.properties

例如:msg_zh_CN.properties ????存儲所有中文

??????Msg_en_US.properties ???存儲所有英文

2.?程序中獲取

ResourceBundle類,可以讀取國際化的資源文件!

?

?

?

動態文本國際化

中文:1987-09-19 ??1000

英文: Sep/09 1987 ?$100

?

l?數值,貨幣,時間,日期等數據由于可能在程序運行時動態產生,所以無法像文字一樣簡單地將它們從應用程序中分離出來,而是需要特殊處理。Java 中提供了解決這些問題的 API (位于 java.util 包和 java.text 包中)

?

// 國際化 - 靜態數據

@Test

public?void?testI18N() throws?Exception {

?

// 中文語言環境

Locale locale = Locale.US;

?

// 創建工具類對象ResourceBundle

ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.f_i18n.msg", locale);

// 根據key獲取配置文件中的值

System.out.println(bundle.getString("hello"));

System.out.println(bundle.getString("username"));

System.out.println(bundle.getString("pwd"));

?

}

?

// 國際化 - 動態文本 - 0. 概述

@Test

public?void?testI18N2() throws?Exception {

// 國際化貨幣

NumberFormat.getCurrencyInstance();

// 國際化數字

NumberFormat.getNumberInstance();

// 國際化百分比

NumberFormat.getPercentInstance(); ?

// 國際化日期

//DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale)

}

?

// 國際化 - 動態文本 - 1. 國際化貨幣

@Test

public?void?testI18N3() throws?Exception {

// 模擬語言環境

Locale locale = Locale.CHINA;

// 數據準備

double?number = 100;

// 工具類

NumberFormat nf = NumberFormat.getCurrencyInstance(locale);

// 國際化貨幣

String m = nf.format(number);

// 測試

System.out.println(m);

}

?

//面試題: ?代碼計算: ?$100 * 10 ?

@Test

public?void?eg() throws?Exception {

String str = "$100";

int?num = 10;

?

// 1. 分析str值是哪一國家的貨幣

Locale us = Locale.US;

// 2. 國際化工具類

NumberFormat nf = NumberFormat.getCurrencyInstance(us);

// 3. 解析str國幣

Number n = nf.parse(str);

?

System.out.println(n.intValue() * num);

}

?

// 國際化 - 動態文本 - 2. 國際化數值

@Test

public?void?testI18N4() throws?Exception {

// 模擬語言環境

Locale locale?= Locale.CHINA;

NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);

String str = nf.format(1000000000);

System.out.println(str);

}

?

// 國際化 - 動態文本 - 3. 國際化日期

/*

?* 日期

?* ??FULL ??201534日 星期三

?* ??LONG ??201534

?* ??FULL ??201534日 星期三

?* ???MEDIUM 2015-3-4

?* ???SHORT ?15-3-4

?* ???

?* 時間

?* ??FULL ??下午043159CST

?* ??LONG ??下午043237

?* ???MEDIUM 16:33:00

?* ???SHORT ?下午4:33

?* ???

?*

?*/

@Test

public?void?testI18N5() throws?Exception {

?

// 日期格式

int?dateStyle = DateFormat.SHORT;

// 時間格式

int?timeStyle = DateFormat.SHORT;

?

// 工具類

DateFormat df =

DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.CHINA);

String date = df.format(new?Date());

?

System.out.println(date);

}

?

// 面試2: 請將時間值:09-11-28 上午102539CST,反向解析成一個date對象。

@Test

public?void?eg2() throws?Exception {

String str = "09-11-28 上午102539CST";

// 創建DateFormat工具類,國際化日期

DateFormat df = DateFormat.getDateTimeInstance(

DateFormat.SHORT, DateFormat.FULL, Locale.getDefault());

Date d = df.parse(str);

?

System.out.println(d);

}

?

?

Jsp頁面國際化

<html>

??<head>

?? <%

?? ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.f_i18n.msg",request.getLocale());

?? %>

????<title><%=bundle.getString("title") %></title>

<meta?http-equiv="pragma"?content="no-cache">

<meta?http-equiv="cache-control"?content="no-cache">

<meta?http-equiv="expires"?content="0">????

??</head>

??

??<body>

??<form?name="frmLogin"?action="${pageContext.request.contextPath }/admin?method=login"?method="post">

?? <table?align="center"?border="1">

?? <tr>

?? <td><%=bundle.getString("username") %></td>

?? <td>

?? <input?type="text"?name="userName">

?? </td>

?? </tr>

?? <tr>

?? <td><%=bundle.getString("pwd") %></td>

?? <td>

?? <input?type="password"?name="pwd">

?? </td>

?? </tr>

?? <tr>

?? <td>

?? <input?type="submit"?value="<%=bundle.getString("submit") %>">

?? </td>

?? </tr>

?? </table>

??</form>

??</body>

</html>

?

Jsp頁面國際化 ?使用jstl標簽

JSTL標簽:

核心標簽庫

國際化與格式化標簽庫

數據庫標簽庫(沒用)

函數庫

?

<fmt:setLocale value=""/>????????設置本地化對象

?? <fmt:setBundle basename=""/>?????設置工具類

?? <fmt:message></fmt:message>?????顯示國際化文本

格式化數值

<fmt:formatNumber pattern="#.##" value="100.99"></fmt:formatNumber>

格式化日期:

<fmt:formatDate pattern="yyyy-MM-dd" value="${date}"/>

?

<html>

??<head>

?? <!-- 一、設置本地化對象 -->

?? <fmt:setLocale?value="${pageContext.request.locale}"/>

?? <!-- 二、設置工具類 -->

?? <fmt:setBundle?basename="cn.itcast.f_i18n.msg"?var="bundle"/>

?

????<title><fmt:message?key="title"?bundle="${bundle}"></fmt:message></title>

<meta?http-equiv="pragma"?content="no-cache">

<meta?http-equiv="cache-control"?content="no-cache">

<meta?http-equiv="expires"?content="0">????

??</head>

??

??<body>

??<form?name="frmLogin"?action="${pageContext.request.contextPath }/admin?method=login"?method="post">

?? <table?align="center"?border="1">

?? <tr>

?? <td><fmt:message?key="username"?bundle="${bundle}"></fmt:message></td>

?? <td>

?? <input?type="text"?name="userName">

?? </td>

?? </tr>

?? <tr>

?? <td><fmt:message?key="pwd"?bundle="${bundle}"></fmt:message></td>

?? <td>

?? <input?type="password"?name="pwd">

?? </td>

?? </tr>

?? <tr>

?? <td>

?? <input?type="submit"?value="<fmt:message?key="submit"?bundle="${bundle}"/>">

?? </td>

?? </tr>

?? </table>

??</form>

??</body>

</html>

?

轉載于:https://www.cnblogs.com/burningmyself/p/7451419.html

總結

以上是生活随笔為你收集整理的Java 监听器,国际化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。