EL&JSTL

EL&JSTL(重要)
1、EL技術
一、EL表達式概述
        EL(Express Language)表達式能夠嵌入jsp頁面內部,建設jsp腳本的編寫,EL出現的目的是要替代jsp頁面腳本的編寫。(不能進行邏輯運算)
 
二、EL從域中取出數據(EL最重要的做用(90%以上))
EL最主要的做用是得到四大域中的數據,格式${EL表達式}
EL得到pageContext域中的值:${pageContextScope.key);}
EL得到request域中的值:${requestScope.key);}
EL得到session域中的值:${sessionScope.key);}
EL得到application域中的值:${applicationScope.key);}
EL從四個域中得到某個值${key;}
---一樣是依次從pageContext域,request域,session域,application域中    獲取屬性,在某個域中獲取後將不在向後尋找。
 
<!-- 模擬取域中的數據 -->
       <%
             //request存入一個字符串
             request.setAttribute("company", "黑馬程序員");
             //session域中存入一個對象
             User user = new User();
             user.setId(1);
             user.setName("zhangsan");
             user.setPassword("123");
             session.setAttribute("user", user);
             //application域(ServletContext)存入一個集合
             List<User> list = new ArrayList<User>();
             User user1 = new User();
             user1.setId(2);
             user1.setName("lisi");
             user1.setPassword("456");
             list.add(user1);
             User user2 = new User();
             user2.setId(3);
             user2.setName("wangwu");
             user2.setPassword("789");
             list.add(user2);
             application.setAttribute("list", list);
       %>
       
       <!-- jsp腳本取出域中的值 -->
       <%=request.getAttribute("company")%>
       <%out.write("<br>"); %>
       <% User sessionUser = (User) session.getAttribute("user");
          out.write(sessionUser.getName());%>
          <%out.write("<br>"); %>
        <% List<User> appList = (List<User>) application.getAttribute("list");
           for(User appUser : appList){
              if(appUser.getId() == 3){
                    out.write(appUser.getName());
              }
           }
        %>
        <!-- 使用EL表達式得到域中的值 -->
        <%out.write("<br>"); %>
        ${requestScope.company}
        <%out.write("<br>"); %>
        ${sessionScope.user.name}
        <%out.write("<br>"); %>
        ${applicationScope.list[1].name}
         <%out.write("<br>"); %>
        <!-- 使用el表達式全域查找 -->
        ${company}
         <%out.write("<br>"); %>
        ${user.name }
         <%out.write("<br>"); %>
        ${list[1].name }
 
三、EL的內置對象11個(解決客戶端接收服務器數據問題)
pageScope,requestScope,sessionScope,applicationScope
---- 獲取JSP中域中的數據
 
param,paramValues     - 接收參數.
--------至關於request.getParameter()  rrquest.getParameterValues()
 
header,headerValues     - 獲取請求頭信息
---------至關於request.getHeader(name)
 
initParam                - 獲取全局初始化參數
---------至關於this.getServletContext().getInitParameter(name)
 
cookie                     - WEB開發中cookie
----------至關於request.getCookies()---cookie.getName()---cookie.getValue()
 
pageContext            - WEB開發中的pageContext.
----------pageContext得到其餘八大對象
 
${pageContext.request.contextPath}
至關於
<%=pageContext.getRequest().getContextPath%>  這句代碼不能實現
得到WEB應用的名稱
 
四、EL執行表達式
例如:
${1+1}
<!--empty 判斷某個對象是否爲null  是null返回true-->
${empty user}
${user == null?true:false}
 
2、JSTL技術
一、JSTL概述
        JSTL(JSP Standard Tag Library),JSP標準標籤庫,能夠嵌入在jsp頁面中使用標籤的形式完成業務邏輯等功能。jstl出現的目的同el同樣也是要代替jsp頁面中的腳本代碼。JSTL標準標準標籤庫有5個子庫,但隨着發展,目前常使用的是他的核心庫。
 
標籤庫 標籤庫的URI 前綴
Core http://java.sun.com/jsp/jstl/core c
118N http://java.sun.com/jsp/jstl/fmt fmt
SQL http://java.sun.com/jsp/jstl/sql sql
XML http://java.sun.com/jsp/jstl/xml x
Functions http://java.sun.com/jsp/jstl/functions fn
 
核心使用就是Core標籤庫。
 
二、JSTL下載與導入
JSTL下載:
從Apache的網站下載JSTL的JAR包。進入「http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/」網址下載JSTL的安裝包。jakarta-taglibs-standard-1.1.2.zip,而後將下載好的JSTL安裝包    進行解壓,此時,在lib目錄下能夠看到兩個JAR文件,分別爲jstl.jar和standard.jar。其中,jstl.jar文件包含JSTL規範中定義的接口和相關類,standard.jar文件包含用於實現JSTL的.class文件以及JSTL中5個標籤庫描述符文件(TLD)。
jstl.jar
standard.jar
 
而後將兩個jar包導入咱們工做的lib中。
 
使用jsp的taglib指令導入核心標籤庫
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
快捷鍵:Alt+/
 
三、JSTL核心庫的經常使用標籤
1)<c:if test=」」>標籤
其中test是返回boolean的條件
 
<!-- test表明的返回boolean的表達式 -->
       <c:if test="${1==1}">
             xxxxx
       </c:if>
       <c:if test="${1!=1}">
             yyyyy
       </c:if>
 
首頁用戶登陸:
<ol class="list-inline">
                    <!-- 用戶沒有登陸 -->
                    <c:if test="${empty user}">
                           <li><a href="login.jsp">登陸</a></li>
                           <li><a href="register.jsp">註冊</a></li>
                    </c:if>
                    <!-- 用戶已經登陸 -->
                    <c:if test="${!empty user}">
                           <li>${user.name }</li>
                           <li><a href="#">提出</a></li>
                    </c:if>
                    
                    <li><a href="cart.jsp">購物車</a></li>
                    <li><a href="order_list.jsp">個人訂單</a></li>
             </ol>
使用session保存用戶登陸信息。
 
2)<c:forEach>標籤
使用方式有兩種組合形式:
<!-- forEach模擬 -->
       <!-- for(int i=0;i<=5;i++) {syso(i);} -->
       <c:forEach begin="0" end="5" var="i">
       ${i}<br>
       </c:forEach>
<!-- 模擬加強for  productList--List<Product> -->
       <!-- for(Product product : productList){syso(product.getPname());} -->
       <!-- items:一個集合或數組,var表明集合中的某一個元素(自命名) -->
       <c:forEach items="${productList}" var="pro">
       ${pro.pname}
       </c:forEach>
 
       <%
             //模擬List<String> strList
             List<String> strList = new ArrayList<String>();
             strList.add("itcast");
             strList.add("itheima");
             strList.add("boxuegu");
             strList.add("shandingyu");
             request.setAttribute("strList", strList);
             
             //遍歷List<User>的值
             List<User> userList = new ArrayList<User>();
             User user1 = new User();
             user1.setId(2);
             user1.setName("lisi");
             user1.setPassword("123");
             userList.add(user1);
             User user2 = new User();
             user2.setId(3);
             user2.setName("wangwu");
             user2.setPassword("123");
             userList.add(user2);
             application.setAttribute("userList", userList);
             
             //遍歷Map<String,String>的值
             Map<String,String> strMap = new HashMap<String,String>();
             strMap.put("name", "lucy");
             strMap.put("age", "18");
             strMap.put("addr", "西三旗");
             strMap.put("email", "licy@itcast.cn");
             session.setAttribute("strMap", strMap);
             
             //遍歷Map<String,User>的值
             Map<String,User> userMap = new HashMap<String,User>();
             userMap.put("user1", user1);
             userMap.put("user2", user2);
             request.setAttribute("userMap", userMap);            
       %>
       
       
       
       <h1>取出strList的數據</h1>
       <c:forEach items="${strList }" var="str">
             ${str }<br/>
       </c:forEach>
       
       <h1>取出userList的數據</h1>
       <c:forEach items="${userList}" var="user">
             user的name:${user.name }------user的password:${user.password }<br/>
       </c:forEach>
       
       <h1>取出strMap的數據</h1>
       <c:forEach items="${strMap }" var="entry">
             ${entry.key }====${entry.value }<br/>
       </c:forEach>
       
       <h1>取出userMap的數據</h1>
       <c:forEach items="${userMap }" var="entry">
             ${entry.key }:${entry.value.name }--${entry.value.password }<br/>
       </c:forEach>
       
 
練習:
遍歷Map<User,Map<String,User>>的值
entry.key-----User
entry.value------List<String,User>
Map<User,Map<String,User>>  userMMap = new HashMap<User,Map<String,User>>();
 
3、JavaEE的開發模式
一、什麼是模式?
    模式在開發過程當中總結出的「套路」,總結出的一套約定俗成的設計模式。
 
二、JavaEE經歷的模式
model1模式
技術組成:jsp+javaBean
model1的弊端:隨着業務複雜性 致使jsp頁面比較混亂。
 
model2模式
技術組成:jsp+servlet+javaBean
model2的優勢:開發中 使用各個技術擅長的方面。
servlet:擅長處理java業務代碼。
jsp:擅長頁面的現實。
 
MVC:---- web開發的設計模式
M:Model---模型 javaBean:封裝數據
V:View-----視圖 jsp:單純進行頁面的顯示
C:Controller----控制器 Servelt:獲取數據--對數據進行封裝--傳遞數據--    指派顯示的jsp頁面
 
三、JavaEE的三層架構
服務器開發時分爲三層
web層:與客戶端交互。
service層:複雜業務處理。
dao層:與數據庫進行交互。
開發實踐時,三層架構經過包結構體現。
 
MVC與三層架構有什麼關係?
 
客戶端 服務器 DB
客戶端發起請求,發起訪問。 Web層 service層 dao層  
servlet
 
javaBean
 
jsp
 
struts2
springMVC
 
業務代碼
 
 
 
spring
 
數據庫操做的代碼
 
 
hibernate
mybatis
 
四、使用三層架構完成商品列表的顯示
 
a、三層架構,包的實現,前綴:公司域名。例如:com.scalpel.xxx 
其餘包名:com.scalpel.web , com.scalpel.service,  com.scalpel.dao,  com.scalpel.domain,  com.scalpel.utils。(基本這樣搭建,每一個包下面能夠再搭建更小的包。)
 
b、業務分析:客戶端發起請求,發起訪問商品列表的請求----->web層servlet接收客戶端請求------>找service層ProductService------->dao層ProductDao------>到數據庫查找到數據----->List<Product>------>返回dao,service,web----->jsp顯示(el+jstl進行顯示)。
 
 
 
 
 
4、總結
EL表達式:
    一、從域中取出數據${域中存儲的數據的name}
    二、域中的內置對象,${pageContext.request.contextPath}    獲取web名稱
    三、表達式
JSTL標籤(核心庫):
    一、引入標籤<%@ taglib uri=""  prefix="c"%>
    二、<c: if test="">
    三、<c:forEach items="數組或集合"  var="數組或集合中的每個元素">
JavaEE三層架構+MVC:
    web層:收集頁面數據,封裝數據,傳遞數據,指定響應的jsp頁面。
    service層:邏輯業務代碼的編寫。
    dao層:數據庫的訪問代碼的編寫。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息