JSP內置對象/EL/JSTL

一:MVC設計模式

MVC是一個設計模式,它強制的使應用層的輸入,處理和輸出分開。使用MVC設計模式被分爲三個核心層:模型層,視圖層,控制層。它們各自處理自己的任務。

顯示層(View):此層主要是負責將內容顯式給用戶。比如:JSP

控制層(Controller):負責判斷所有的用戶請求參數是否合法,根據請求的類型調用模型層執行操作,再講處理結果交給顯示層顯示。eg:servlet

模型層(Model):操作數據庫的獨立的操作組件,或使用lavaBean(POJO)保存數據。

二:JSP內置對象

JSP中提供了九個內置對象,這些內置對象由容器爲用戶進行實例化,用戶直接使用即可。


 eg:

(1):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>response內置對象</title>
</head>
<body>
    <h3>3秒之後跳轉到index頁面</h3>
    <%
      //  response.setHeader("refresh","3"); 定時刷新
      response.setHeader("refresh","3;URL=index.jsp");   // 定時跳轉
    %>
</body>
</html>
(2):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>index.jsp</title>
</head>
<body>
   <h3>跳轉過來了</h3>
</body>
</html>
(3):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>處理錯誤的頁面</title>
</head>
<body>
   <%
      String errorMsg=exception.getMessage();
      out.println("<h3>有點問題:"+errorMsg+"</h3>");
   %>
</body>
</html>

(4):<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JSPProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- context-param配置的是上下文參數,給應用設置參數,所有Servlet(JSP)共享這個參數 -->
  <context-param>
      <param-name>ctx</param-name>
      <param-value>ContextParamValue</param-value>
  </context-param>
  
  <jsp-config>
      <taglib>
          <taglib-uri>http://www.mycompany.com</taglib-uri>
          <taglib-location>/WEB-INF/c.tld</taglib-location>
      </taglib>
  </jsp-config>
</web-app>

三:EL表達式語言

使用表達式語言(EL)可以在JSP頁面進行方便的輸出內容:

EL語法:${表達式}

EL有自己的內置對象:

PageContext ,pageScope,requestScope,sessionScope,applicationScope,param,paramValues,header,headerValues,cookie,initParam

1. 使用EL訪問不同的屬性範圍:

${pageScope.屬性名}${requestScope.屬性名},${sessionScope.屬性名},${applicationScope.屬性名},這四種屬性訪問範圍由小到大。

Eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL訪問不同屬性範圍的值</title>
</head>
<body>
   <%
       pageContext.setAttribute("info","page範圍的屬性值");
       request.setAttribute("info","request範圍的屬性值");
       session.setAttribute("info","session範圍的屬性值");
       application.setAttribute("info","application範圍的屬性值");
   %>
   
   <h3>page===>  ${pageScope.info}</h3>
   <h3>request===>  ${requestScope.info}</h3>
   <h3>session===>  ${sessionScope.info}</h3>
   <h3>application===>  ${applicationScope.info}</h3>
   <h3>${info}=====>  ${info}</h3>
</body>
</html>

2. EL訪問JSP內置對象

使用ELpageContext內置對象訪問JSP的內置對象:${pageContext.對應的jsp內置對象}

Eg:EL獲取上下文路徑:特殊:${pageContext.servletContext.contextPath}

EL訪問sessionID:${pageContext.session.id}

Eg::

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL訪問jsp內置對象</title>
</head>
<body>
   <%
       String contextPath=application.getContextPath();
   %>
   <h3>直接通過JSP內置對象獲取上下文路徑:<%=contextPath%></h3>
   <h3>通過EL獲取上下文路徑:${pageContext.servletContext.contextPath}</h3>
   <h3>通過EL獲取sessionID: ${pageContext.session.id}</h3>
   <h3>通過EL判斷當前的請求方式:  ${pageContext.request.method}</h3>
</body>
</html>

3. EL訪問參數(訪問客戶端發送的參數。全局參數,一組參數)

用途1:使用ELparam內置對象訪問客戶端發送的參數${param.參數名}

eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL訪問客戶端發送的請求參數</title>
</head>
<body>
   <h3>客戶端發送來的username參數是:${param.username}</h3>
   <h3>客戶端發送來的pwd參數是:${param.pwd}</h3>
</body>
</html>

用途2:使用ELinitParam內置對象訪問上下文參數(全局參數),在web.xml中配置上下文參數:

<context-param>

<param-name>admin</ param-name >

<param-value>Obama</ param-value>

</context-param>

eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL訪問全局參數(上下文參數)</title>
</head>
<body>
   <h3>全局參數爲ctx的值是:${initParam.ctx}</h3>
</body>
</html>

用途3:使用ELparamValues內置對象訪問一組參數

           ${paramValues.參數名[0]}訪問一組參數值

           ${paramValues.參數名[n]}訪問n+1組參數值

eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>興趣愛好頁面</title>
</head>
<body>
   <form action="paramValues.jsp" method="post">
            運動:   <input type="checkbox" name="hobby" value="sport"/>  <br/>
            電影:  <input type="checkbox" name="hobby" value="movie"/>  <br/>
            讀書:  <input type="checkbox" name="hobby" value="reading"/>  <br/>
            音樂:  <input type="checkbox" name="hobby" value="music"/>  <br/>
            <input type="submit" value="提交"/>
   </form>
</body>
</html>

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL訪問一組參數</title>
</head>
<body>
   <h3>愛好是:</h3>
   <h3> ${paramValues.hobby[0]} </h3>
   <h3> ${paramValues.hobby[1]} </h3>
   <h3> ${paramValues.hobby[2]} </h3>
   <h3> ${paramValues.hobby[3]} </h3>
</body>
</html>

4. 訪問cookie

通過ELcookie內置對象訪問JSESSIONID名稱的cookie的語法:

${cookie[“JSESSIONID”].name},${cookie[“JSESSIONID”].value}

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL的cookie內置對象訪問Cookie</title>
</head>
<body>
   <h3>Cookie名爲JSESSIONID的值爲: ${cookie["JSESSIONID"].value}</h3>
   <h3>sessionID爲:${pageContext.session.id}</h3>
</body>
</html>

5. 訪問header

${header[“cookie”] }

 EL的運算符

1. 算數運算符
      + - * /(div)  %(mod)
      eg: ${param.num1+param.num2}   參數相加


 2. 關係運算符
       < (lt)  > (gt)  == (eq)  !=(ne)  >=(ge) <=(le)


 3. 邏輯運算符

       &&(and)   ||(or)  !(not)

4. empty運算符
   ${empty 表達式}  判斷是否爲null或空字符串””


5. 三目運算符
       ${返回true或false的表達式 ? "爲true時輸出的內容":"爲false時輸出的內容"}


6. 括號運算符
       ()用來改變運算順序的


eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL運算符</title>
</head>
<body>
   <h3>num1+num2= ${param.num1+param.num2}</h3>
   <h3>num1/num2= ${param.num1 / param.num2} 也可以div ${param.num1 div param.num2}</h3>
   <h3>num1是否等於num2: ${param.num1 == param.num2}</h3>
   <h3>true && true: ${true  && true}</h3>
   <h3>true || false: ${true  || false}</h3>
   <h3>${empty param.abc}</h3>
   <h3>三目運算符:${param.num1>param.num2 ? "num1真的大於num2":"num1不大於num2" }</h3>
</body>
</html>

四:JSTL(JSP Standard Tag Libraries)

引入JSTL的步驟:

   第一步:將jstl-xx.jar導入/WEB-INF/lib文件夾下。




   第二步:將jstl-xx.jar解壓後的META-INF文件夾下的xxx.tld文件拷貝到/WEB-INF/的某目錄下


   第三步:在JSP頁面使用taglib指令引入xxx.tld文件。

  

 JSP頁面使用taglib指令引入xxx.tld文件

  <%@ taglib uri="指向xxx.tld文件的路徑"     prefix="JSTL標籤前綴"%>
注意:uri中指向xxx.tld文件的路徑有兩種寫法:
          第一種:直接指向xxx.tld文件的路徑
第二種:在web.xml中配置taglib的uri
        <jsp-config>
                 <taglib>
                       <taglib-uri>http://www.xxx.com</taglib-uri>
                       <taglib-location>/WEB-INF/c.tld</taglib-location>
                   </taglib>
         </jsp-config>
1.輸出標籤
    <c:out value="輸出的內容,支持EL"  default=" 默認值,支持EL"/>
2.設置標籤
      設置屬性範圍的屬性值
      <c:set var="屬性名" value="屬性值,支持EL" scope="屬性範圍"/>

設置對象的屬性值
   <c:set target=“${perObj}” property=“對象的屬性名” value=“支持EL"/>
3.捕獲異常標籤
      <c:catch var="保存異常信息的屬性名">
           // 有可能發生異常的代碼

      </c:catch>

4.判斷標籤
     <c:if test="判斷表達式,支持EL">
            // 判斷結果爲true,執行此處
     </c:if>
5.forEach標籤(遍歷list集合或Map集合)
      <c:forEach  var="當前正在遍歷的對象,作爲屬性用(msg)"  items="要遍歷的集合,支持EL">
            ${msg}

      </c:forEach>

6.choose標籤
      <c:choose>
          <c:when test="${param.score>=90}">
               <h3>優秀</h3>
          </c:when>
          <c:when test="${param.score>=80}">
               <h3>良好</h3>
          </c:when>
          <c:otherwise>
               <h3>不及格,要加油!</h3>
          </c:otherwise>
      </c:choose>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章