el和jstl筆記



el:
jsp的內置表達式語言,從jsp2.0開始.
用來替代<%=..%>
作用:
1.獲取域中數據 ★
2.執行運算 ★
3.獲取常見的web對象
4.調用java的方法
格式:
${el表達式}

獲取域中數據:★

1.獲取簡單數據
${pageScope|requestScope|sessionScope|applicationScope.屬性名}
以後經常使用:
${屬性名}:依次從pageContext,request,session,application查找指定屬性,若查找到返回值,結束該次查找
若查找不到,返回""
------------------------------------------------------------------------------------
例子: <%
request.setAttribute("rkey", "rvalue");
session.setAttribute("skey", "svalue");
session.setAttribute("rkey", "svalue");
application.setAttribute("akey", "avalue");
%>
獲取request中的數據:<br>
老方式:<%=request.getAttribute("rkey") %><br/>
el方式:${requestScope.rkey }<br/>
<hr>

獲取session中的數據:<br>
老方式:<%=session.getAttribute("skey") %><br/>
el方式:${sessionScope.skey }<br/>
<hr>

獲取application中的數據:<br>
老方式:<%=application.getAttribute("akey") %><br/>
el方式:${applicationScope.akey }<br/>

<hr>
獲取失敗老方式:<%=application.getAttribute("aakey") %><br/>
獲取失敗el方式:${applicationScope.aakey }

<hr>
便捷獲取:
${skey },${rkey },${aakey },${akey }
<hr>
${rkey }
----------------------------------------------------------------------------------------------
2.獲取複雜數據
獲取數組中的數據
${域中的名稱[index]}
獲取list中的數據
${域中的名稱[index]}
獲取map中的數據
${域中的名稱.鍵名}
-----------------------------------------------------------------------------
<%
//往request域中放入數組
request.setAttribute("arr", new String[]{"aa","bb","cc"});

//往request域中放入list
List list=new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list", list);

//往request域中放入map
Map m=new HashMap();
m.put("username","tom");
m.put("age",18);
request.setAttribute("map", m);
%>

獲取域中的數組:<br>
老方式:<%=((String[])request.getAttribute("arr"))[1] %><br>
el方式:${arr[1] }<br>
<hr>

獲取域中的list:<br>
老方式:<%=((List)request.getAttribute("list")).get(1) %><br>
el方式:${list[1] }<br>
list的長度:${list.size() }
<hr>

獲取域中的map:<br>
老方式:<%=((Map)request.getAttribute("map")).get("age") %><br>
el方式:${map.age }<br>
<hr>
-----------------------------------------------------------------------------------------------------------








javabean導航
javabean:
java語言編寫的一個可重用的組件,
狹義上來說就是我們編寫的一個普通的java類 例如:User Person 
javabean規範:
1.必須是一個公共的具體的類  public class
2.提供私有的成員變量  private String id;//
3.提供公共訪問字段的方法 get|set|is方法
public String getId(){..}
一旦有公共的方法之後,get|set|is之後的內容,將首字母小寫,將這個東西稱之爲bean屬性
id就是一個bean屬性
4.提供一個無參的構造器
5.一般實現序列化接口  serializable

3.${域中javabean名稱.bean屬性}
---------------------------------------------------------------------
例子 el  獲取對象中字段的值
<%
User u=new User();
u.setId("001");
u.setName("tom");
u.setPassword("123");

//將u放入域中
request.setAttribute("user", u);
%>
獲取域中javabean的id值:<br>
老方式:<%=((User)request.getAttribute("user")).getId() %><br/>
el方式:${user.id }<!-- 相當於調用 getXxx() -->


<hr>
el獲取那麼:${user.name }<br>
錯誤演示:${user.username }
-------------------------------------------------------------------------------------


//////////////////
執行運算:
四則運算 關係(>..) 邏輯(&& ||)
注意:
+:只能進行加法運算,字符串形式數字可以進行加法運算.
empty:判斷一個容器的長度是否爲0(array set list map),還可以判斷一個對象是否爲空
${empty 域中的對象名稱}
取反 not   例如    not empty 
${not empty 域中對象名稱}
三元運算符   ${ 3>4?"yes":"no" }
--------------------------------------------------------------------------------------------------------
例子:
<%
request.setAttribute("i", 3);
request.setAttribute("j", 4);
request.setAttribute("q", "12");
request.setAttribute("k", "k");

List l=null;
request.setAttribute("list", l);

List ll=new ArrayList();
ll.add("22");
request.setAttribute("list_", ll);

User user=null;
request.setAttribute("bean", user);

User user_=new User();
request.setAttribute("bean_", user_);
%>

${i+j }<br/>
${i+q }<br/>
${q+q }<br/>
<%-- ${i+k }<br/> --%>
<hr>
域中list的長度是否爲0:${empty list}<br/>
域中list_的長度是否爲0:${empty list_ }<br/>

<hr>
域中的bean是否爲空:${empty bean }<br/>
域中的bean_是否爲空:${empty bean_ }<br/>

<hr>
${ 3>4?"yes":"no" }<br/>

${i==3 }
--------------------------------------------------------------------------------------






//////////////////
el的內置對象
11個

pageScope
requestScope
sessionScope
applicationScope

param
paramValues

header
haederValues

initParam

cookie★



cookie內置對象:
${cookie} 獲取map{key=Cookie}
例如:創建cookie
Cookie c=new Cookie("username","tom");
通過${cookie}獲取相當於
{username=new Cookie("username","tom")}
相當於map的key是cookie的鍵
map的value是當前cookie

若項獲取名稱username的cookie的value值(獲取tom)
${cookie.username.value}--javabean導航
注意:
java中Cookie的api
getName():獲取cookie的名稱
getValue():獲取cookie的value值
我們稱name和value是cookie的bean屬性

使用cookie內置對象:
${cookie.給cookie起名字.value}

例如:
獲取jsession的值
${cookie.JSESSIONID.value}

pageContext:獲取不是map集合,相當於jsp的pageContext內置對象
在jsp頁面中獲取項目名
${pageContext.request.contextPath}
例如:<a href="${pageContext.request.contextPath }/demo8.jsp">pagecontext內置對象獲取項目名(掌握)</a>
-----------------------------------------------------------------------------------------------------
jstl:
jsp標準的標籤庫語言
apache的
用來替代java腳本
使用步驟:
1.導入jar包 (jstl.jar和standard.jar)
2.在頁面上導入標籤庫
<%@taglib prefix="" uri=""%>

例如:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
jstl的分類:
core:核心類庫 ★
fmt:格式化|國際化
xml:過時了
sql:過時了
函數庫:很少使用
core:掌握
★c:if
★c:forEach



★c:if 判斷
<c:if test="${el表達式}">滿足的時候輸出的內容</c:if>
例如:
<c:if test="${3>4 }">
3大於4
</c:if>
<c:if test="${3<=4 }">
3不大於4
</c:if>
//////////////


例子 比如可以給我們登陸成功頁面 加上判斷
<c:if test="${not empty name }">


${name}歡迎你!


</c:if>


<c:if test="${empty name }">


 
你尚未登陸請重新登陸<a href="/ServletDemo/login.jsp">點擊登陸</a>


</c:if>
---------------------------------------------------------------------------------------------------------
















★c:forEach 循環
格式1:
<c:forEach begin="從那裏開始" end="到那裏結束" step="步長" var="給變量起個名字" varStatus="循環狀態變量">
${i }--${vs.count }--${vs.current }<br>
</c:forEach>
 
varStatus:用來記錄循環的狀態
常用的屬性:
count:記錄次數 從1開始
current:當前遍歷的內容
index: 索引 從0 開始
例如:
<c:forEach begin="1" end="20" step="2" var="i" varStatus="vs">
${i }--${vs.count }--${vs.current }<br>
</c:forEach>

格式2:
<c:forEach items="${el獲取域中的容器}" var="n">
${n }
</c:forEach>

例如:
//遍歷list
<c:forEach items="${list }" var="n">
${n }
</c:forEach>

//遍歷map
<c:forEach items="${map }" var="en">
${en.key }-- ${en.value }<br/>
  </c:forEach>
-----------------------------------------------------------------------------------
例子:
<%
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("張三", "23"));
list.add(new Student("李四", "24"));
list.add(new Student("王五", "25"));
list.add(new Student("趙六", "26"));


request.setAttribute("list", list);
%>


<table height=300 width=400 align=center>
<tr bgcolor=green>
<th>編號</th>
<th>姓名</th>
<th>年齡</th>
</tr>
<c:forEach items="${list }" var="stu" varStatus="s">
<c:if test="${s.count%2==0 }">
<tr bgcolor=red>
</c:if>
<c:if test="${s.count%2!=0 }">
<tr bgcolor=yellow>
</c:if>


<td>${s.count }</td>
<td>${stu.name }</td>
<td>${stu.age }</td>
</tr>
</c:forEach>


</table>
-----------------------------------------------------------------------------------------------------------------
* c:url:
功能:
* 自動補項目路徑
* 自動url重寫
<a href='<c:url value="/foreach.jsp" />'>foreach.jsp</a>


---------------------------------------------------------------------------------------------------------------------------
  <%
  HttpServletRequest req = (HttpServletRequest)(pageContext.getRequest());
  String contextPath = req.getContextPath();
  session.setAttribute("aaa", "aaa");
  %>
  <%=contextPath %><br>
  ${pageContext.request.contextPath}
    <a href="${pageContext.request.contextPath}/foreach.jsp">foreach.jsp</a><br>
   
   
    <a href='<c:url value="/foreach.jsp" />'>foreach.jsp</a>




--------------------------------------------------------------------------------------------------------------------
* MVC模式 一種前輩們總結出來的開發模式
* M :Model 模
* 封裝數據,處理業務邏輯
* V : View 視圖
* 界面顯示
* C :Controller 控制器
* 調度




* JavaWeb的三層架構
* org.westos.web.servlet :web層
*  org.westos.service:service層
*org.westos.dao:dao層
* org.westos.domain : JavaBean
*  org.westos.util:工具包
org.westos.test:測試包



發佈了38 篇原創文章 · 獲贊 1 · 訪問量 5450
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章