el表達式和jstl標籤庫javascript
一:el表達式:表達式語言,jsp頁面獲取數據比較簡單
一、el表達式的語法(掌握)
el表達式一般取值是獲取做用域對象中的屬性值:${屬性名}=>是el表達式的簡寫的形式
跟jquery不同,$(選擇器)jquery對象,代碼寫在js的腳本塊中
完整的書寫形式:
四個做用域 四種取值方式獲取不一樣做用域中的屬性值
${pageScope.attrname } pageScope.屬性名
${requestScope.attrname }
${sessionScope.attrname }
${applicationScope.attrname }
注:jsp2.0以上版本,對應 servlet 3.0以上版本,jsp默認忽略EL表達式,因此在使用el表達式的時候須要在 page指令中加上。isELIgnored="false" 開啓EL表達式,true忽略(默認是忽略)
EL表達式取值的兩種方式例子:test.jspcss
1 <%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'test.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <% 27 //將數據放到做用域對象中 28 29 //page域 當前頁面有效 30 pageContext.setAttribute("uname", "印度阿三"); 31 32 //一次請求有效,能夠是多個頁面,轉發頁面 33 request.setAttribute("fav", "睡覺"); 34 35 //session域取值 一次會話,多個請求 36 session.setAttribute("value", "躺在牀上聽音樂"); 37 38 //應用域中取值一個web應用 39 application.setAttribute("小喜慶", "小云雲"); 40 %> 41 <!--page域用el表達式取值 --> 42 page域用el表達式取值: 43 ${uname }  44 ${pageScope.uname }<hr> 45 46 <!--request域用el表達式取值 --> 47 request域用el表達式取值: 48 ${fav }  49 ${requestScope.fav }<hr> 50 51 <!--session域用el表達式取值 --> 52 session域用el表達式取值: 53 ${value }  54 ${sessionScope.value }<hr> 55 56 <!--application域用el表達式取值 --> 57 application域用el表達式取值: 58 ${小喜慶 }  59 ${applicationScope.小喜慶 }<hr> 60 61 </body> 62 </html>
在測試做用範圍頁面以前,必須先運行 test.jsp將數據放到做用域對象中
測試test.jsp四個做用域的做用範圍【getTest.jsp】html
1 <%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'getTest.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <p>在測試 當前頁面以前,必須先運行 test.jsp將數據放到做用域對象中</p><hr> 27 測試test.jsp四個做用域的做用範圍 <br><hr> 28 29 <!--page 當前頁面有效 --> 30 page域用el表達式取值: 31 ${uname } <br> 32 <!--request 一次請求有效,能夠是多個頁面,轉發頁面 --> 33 request域用el表達式取值: 34 ${fav } <br> 35 <!--session 一次會話,多個請求 --> 36 session域用el表達式取值: 37 ${value } <br> 38 <!--application 應用域中取值一個web應用 --> 39 application域用el表達式取值: 40 ${小喜慶 } <br> 41 </body> 42 </html>
若是不一樣的做用域,可是屬性名相同
<!-- 【取值】若是不一樣的做用域,可是屬性名相同 -->
注意:在省略 ***Scope對象的時候,取值的順序,先從小範圍獲取數據 page,若是獲取到了就返回,若是page獲取不到,會去找request域,依次類推,找application ,若是都找不到,則返回null
二、el表達式獲取不一樣數據類型的值(java 代碼 字符串,數值,對象,list,map,數組)
①對象
注:在獲取對象屬性的時候,el表達式的解析的工具類,底層調用 的 get方法,不是直接調用的屬性。el的解析對象用的反射,調用 get方法 Class ----getMethod("get方法")。${student.id }<=> ${student.getId }
②list
③map
注:map取值有兩個:
點獲取.簡單【${map.fav }】
["key"] 比較靈活,能夠處理特殊符號【${map.fav,ff }錯誤的 =>${map["fav,ff"] }】
④數組
student.javajava
1 package boom.el.entity; 2 3 import java.util.Date; 4 5 /** 6 * 學生實體類對象 7 * @author Administrator 8 * 9 */ 10 public class Student { 11 private int id; 12 private String name; 13 private Date hiredate; 14 15 public Student() { 16 } 17 18 public Student(int id, String name, Date hiredate) { 19 super(); 20 this.id = id; 21 this.name = name; 22 this.hiredate = hiredate; 23 } 24 25 public int getId() { 26 return id; 27 } 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 public String getName() { 34 return name; 35 } 36 37 public void setName(String name) { 38 this.name = name; 39 } 40 41 public Date getHiredate() { 42 return hiredate; 43 } 44 45 public void setHiredate(Date hiredate) { 46 this.hiredate = hiredate; 47 } 48 49 @Override 50 public String toString() { 51 return "Sutudent [id=" + id + ", name=" + name + ", hiredate=" 52 + hiredate + "]"; 53 } 54 55 }
test.jspjquery
1 <%@ page language="java" isELIgnored="false" import="java.util.* , boom.el.entity.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>el表達式獲取不一樣數據類型的值 </title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 </head> 23 <body> 24 25 <!-- 獲取對象的值 --> 26 <% 27 /* 獲取對象的值 【建立一個實體類 ,獲取對象的屬性值】*/ 28 Student student = new Student(0,"林徽因",new Date()); 29 //將 Student對象 放到做用域對象中 30 request.setAttribute("student", student); 31 %> 32 ${student }<hr> 33 ${student.id }  ${student.name }  ${student.hiredate }  34 <%-- 在獲取對象屬性的時候 ,el表達式的解析的工具類 底層調用 的 get方法,不是直接調用的屬性 35 el的解析對象用的反射,調用 get方法 Class ----getMethod("get方法") 36 ${student.id }<=> ${student.getId } 37 --%> 38 39 <!-- 獲取list的值 --> 40 <% 41 /* 建立對象存入list中 */ 42 Student stu1 = new Student(1,"陸小曼",new Date()); 43 Student stu2 = new Student(2,"周旋",new Date()); 44 Student stu3 = new Student(3,"阮玲玉",new Date()); 45 /* <!-- list存值 --> */ 46 List<Student> list = new ArrayList(); 47 list.add(stu1); 48 list.add(stu2); 49 list.add(stu3); 50 request.setAttribute("list", list); 51 %> 52 <!-- el表達式取值 --> 53 <hr> 獲取 做用域對象中list<br> 54 ${list.get(0) }<br> 55 <!-- list獲取屬性的具體值 --> 56 ${list.get(1).name }<br> 57 ${list.get(2) }<br><hr> 58 59 <!-- 獲取map的值 --> 60 <% 61 Map map = new HashMap(); 62 map.put("fav", "music"); 63 map.put("codeing", "Java"); 64 map.put("fav,ff", "run"); 65 request.setAttribute("map", map); 66 %> 67 <!-- el表達式取值 --> 68 ${map }<br> 69 ${map.fav }<br> 70 ${map["fav,ff"] }<br><hr> 71 72 <%-- map取值有兩個 73 . 簡單 74 ["key"] 比較靈活 ,能夠處理特殊符號 75 ${map.fav,ff } 錯誤的 =>${map["fav,ff"] } --%> 76 77 78 <!-- 獲取數組的值 --> 79 <% 80 String[] arr = {"haha" , "gaga" ,"heihei"}; 81 request.setAttribute("arr", arr); 82 %> 83 <!-- el表達式取值 --> 84 獲取數組的元素: 85 ${arr }  86 ${arr[0] } 87 88 </body> 89 </html>
三、el表達式的基本運算
+ 字符串相加 非數值型字符串,在el表達式中不能直接相加,須要存放到request域中
四、el表達式能夠在html代碼塊中,javascript 的腳本塊中
五、el表達式的內置對象11個(掌握其中的一部分)
隱含對象 描述
和做用域相關的【前4】,存取數據的隱含對象:【主要做用:獲取做用域對象的數據 】
二:jstl標籤庫
一、jstl標籤庫,jsp標準標籤庫(只要jsp,標籤庫就起做用)
jstl標籤庫經常使用標籤:jstl 標籤庫 for 循環,條件判斷【for循環方法的封裝 ,if 判斷方法的封裝】
二、jstl標籤庫的分類:
①核心標籤庫:c標籤庫
②經常使用標籤:foreach 標籤 遍歷數據、邏輯判斷標籤:c:if、c:when、c:choose、c:otherwise
③格式化標籤庫:時間格式化標籤
④函數標籤庫
⑤xml標籤庫
⑥數據庫sql標籤庫
三、jstl標籤庫使用
jstl 標籤,就是 java代碼對 函數的封裝
myeclipse建立web項目的時候,自動加載jstl
①引入相應的jstl標籤庫
<!-- 核心標籤庫 -->
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!-- 函數標籤庫 -->
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
解釋:@taglib引入、uri 標籤庫地址、
tld 文件 將標籤庫底層的java實現類和 jsp鏈接起來的文件
②使用標籤庫
參數解釋:
<c:set></c:set>:存儲數據、var 存儲數據的變量、scope 做用範圍、value 存儲的數據
<c:out value=""></c:out>:value 輸出數據
<c:remove var="name"/>:移除數據
遍歷:
<!--屬性 :
var 遍歷的變量
items 要被遍歷的數據
-->
<c:forEach var="stu1" items="${list }">
${stu1.id }=>${stu1.name }<hr>
</c:forEach>
web