細節補充
●功能
·訪問存儲對象
當內容爲空時 el內部自動轉換成空串 ${user }
原因是el表達式訪問數據他是表現層的一個技術 儘量規避空指針
·取javabean的屬性
1)${user.username }訪問user對象的username屬性(找getUsername方法而不是找屬性)
2)${user.address.city }訪問user對象的address屬性的city屬性
·取數組、list或map
1)${arrayname[index]}
2)${listname[index]}
3)${mapname.key} key不能取數字 當key爲數字時${mapname.["1"]}
·二元
${3>2?'大於':'小於'}
●el中的隱式對象
1)pageContext
${pageContext.request.ContextPath}
使用場景:拿到web應用根路徑 在提交表單和鏈接的時候可以用
2)param 一個map集合 封裝了所有的請求參數
${param }
使用場景:request由servlet轉發到頁面後可以獲得數據${param.username }做回寫免得用戶每次都要重新寫
${param.gender=='男'?'selected':'' }
3)paramValues一個map集合 key是參數名 value 是參數值
${paramValues.preference[0]}||{paramValues.preference[1]}
4)header map封裝所有請求消息頭 由於有-有歧義 所以用[]語法
${header["accept-encoding"] }
5)headerValues map封裝所有請求消息頭
6)pageScope
7)requestScope
8)sessionScope session域
${sessionScope.user}
9)applicationScope
10)cookie name當做key value是對象
${cookie.JSESSIONID}拿到cookie對象
${cookie.JSESSIONID.name}
${cookie.JSESSIONID.value}
11)初始化參數initParam
${initParam.encoding }
●el函數
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/function"%>
函數調用在el表達式中採用 前綴+冒號+函數名
1)取長度 (hello="helloWorld")
${fn:length(hello)}
${fn:length(list)}
${fn:split(formBean.birthday, '-')[0] }
${fn:contains('唱歌')?'checked':'' }
2)自定義函數
i.建立類
ii.建立 必須公共靜態 方法
iii.在web-inf/建立File XXX.tld
將fn.tld中的內容複製過去
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 functions library</description>
<display-name>JSTL functions</display-name>
<tlib-version>1.1</tlib-version>
<short-name>my</short-name>
<uri>http://java.sun.com/jsp/jstl/functions</uri> 這麼寫總出錯 /WEB-INF/myfn.tld這麼寫可以
<function>
<name>say</name> 頁面調用時叫什麼這裏就寫什麼
<function-class>com.bjsxt.struts.MyFunctions</function-class>方法的類
<function-signature>java.lang.String sayHello(java.lang.String)
</function-signature>方法的描述
</function>
</taglib>
前臺可以調用
<%@ taglib prefix="上面定義的shortname" uri="上面定義的uri" %>
${my:say("Tom")}
●el函數庫
·param.preference包含唱歌
${fn:contains(param.preference, '唱歌')?'checked':'' }
·以-連接
${fn:join(paramValues.skill,'-' )}
·轉譯html標籤
${fn:escapeXml('<a href="#">aaaaa</a>') }
·截取0-10
${fn:substring(requestScope.description, 0, 10 )}
·拿到長度
${fn:length(requestScope.description)>10?'...':'' }
●格式化
// 獲得請求參數
String language = request.getParameter("language");
Locale locale = request.getLocale();
if(language!=null) {
// 斷開
String[] parts = language.split("-");
// 創建 locale
locale = new Locale(parts[0], parts[1]);
}
// 國際化
// 日期
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG, locale);
System.out.println(df.format(new Date()));
// 數字
NumberFormat nf1 = NumberFormat.getNumberInstance(locale);
System.out.println(nf1.format(23.78));
// 貨幣
NumberFormat nf2 = NumberFormat.getCurrencyInstance(locale);
System.out.println(nf2.format(49.66));
// 百分比
// 對於靜態文本讀取資源文件
ResourceBundle bundle = ResourceBundle.getBundle("myresource", locale);