尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版(三)
生活随笔
收集整理的這篇文章主要介紹了
尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版(三)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
EL表達(dá)式
什么是EL表達(dá)式
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><%request.setAttribute("key", "值");%>表達(dá)式腳本輸出key的值是:<%=request.getAttribute("key")%> <br>EL表達(dá)式輸出key的值是:${key} </body> </html>- 這樣輸出的結(jié)果相同
- EL表達(dá)式在輸出null值(比如key1)的時(shí)候,輸出的是空串;jsp表達(dá)式腳本輸出null值的時(shí)候,輸出的是null字符串
EL表達(dá)式搜索四個(gè)域的順序
EL表達(dá)式主要是在jsp頁面中輸出數(shù)據(jù)
主要是輸出域?qū)ο笾械臄?shù)據(jù)
- session中的數(shù)據(jù)瀏覽器關(guān)閉后才會(huì)消失,所以哪怕注釋session后然后刷新頁面,還是會(huì)輸出session
- 當(dāng)四個(gè)域中都有相同的key的數(shù)據(jù)的時(shí)候,EL表達(dá)式會(huì)根據(jù)四個(gè)域的從小到大的順序去進(jìn)行搜索,找到就輸出
EL表達(dá)式輸出復(fù)雜的Bean對(duì)象
package com.atguigu.pojo;import java.util.Arrays; import java.util.List; import java.util.Map;public class Person {private String name;private String[] phones;private List<String> cities;private Map<String, Object> map;@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", phones=" + Arrays.toString(phones) +", cities=" + cities +", map=" + map +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public String[] getPhones() {return phones;}public void setPhones(String[] phones) {this.phones = phones;}public List<String> getCities() {return cities;}public void setCities(List<String> cities) {this.cities = cities;}public Map<String, Object> getMap() {return map;}public void setMap(Map<String, Object> map) {this.map = map;}public Person(String name, String[] phones, List<String> cities, Map<String, Object> map) {this.name = name;this.phones = phones;this.cities = cities;this.map = map;}public Person() {} } <%@ page import="com.atguigu.pojo.Person" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><%Person person = new Person();person.setName("name111");person.setPhones(new String[]{"123", "456", "789"});List<String> cities = new ArrayList<String>();cities.add("beijin");cities.add("shanghai");cities.add("shenzheng");person.setCities(cities);Map<String, Object> map = new HashMap<>();map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");person.setMap(map);pageContext.setAttribute("key_person", person);%>${ key_person } </body> </html> <%@ page import="com.atguigu.pojo.Person" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><%Person person = new Person();person.setName("name111");person.setPhones(new String[]{"123", "456", "789"});List<String> cities = new ArrayList<String>();cities.add("beijin");cities.add("shanghai");cities.add("shenzheng");person.setCities(cities);Map<String, Object> map = new HashMap<>();map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");person.setMap(map);pageContext.setAttribute("key_person", person);%>${ key_person } <br>${ key_person.name } <br>${ key_person.phones } <br>${ key_person.phones[1] } <br>${ key_person.cities } <br>${ key_person.cities[1] } <br>${ key_person.map } <br>${key_person.map[key2]} <br> </body> </html>- 注意EL表達(dá)式是不需要引入jstl.jar的
- 注意到輸出map是輸出花括號(hào)包裹的鍵值對(duì);輸出List得到的是由[]包裹起來的;輸出數(shù)組得到的是首地址
- 通過實(shí)驗(yàn),我們注意到 :EL表達(dá)式中,找的不是這個(gè)屬性,而是找這個(gè)屬性所對(duì)應(yīng)的get方法
關(guān)系運(yùn)算
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body>${12 == 12} 或 ${12 eq 12} <br> </body> </html>邏輯運(yùn)算
算術(shù)運(yùn)算
empty運(yùn)算
<%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><% // 1、值為null值的時(shí)候,為空request.setAttribute("key1", null); // truerequest.setAttribute("key2", new Object()); // false ,非空!// 2、值為空串的時(shí)候,為空request.setAttribute("key3", ""); // truerequest.setAttribute("key4", " "); // false// 3、值是Object類型數(shù)組,長度為零時(shí)為空request.setAttribute("key5", new Object[]{}); // truerequest.setAttribute("key6", new Object[]{"12"}); // false// 4、list集合,元素個(gè)數(shù)為零List<String> list = new ArrayList<>();request.setAttribute("key7", list); // true// 5、map集合,元素個(gè)數(shù)為零Map<String, Object> map = new HashMap<String, Object>();request.setAttribute("key8", , map); // true%>${ empty key1 } </body> </html>三元運(yùn)算
${ 12 == 12 ? "123" : "456" }
點(diǎn)運(yùn)算和中括號(hào)運(yùn)算
.點(diǎn)運(yùn)算,可以輸出bean對(duì)象中某個(gè)屬性的值
[]中括號(hào)運(yùn)算,可以輸出有序集合中某個(gè)元素的值
并且中括號(hào)運(yùn)算還可以輸出map集合中 key 里含有特殊字符的key的值(比如. 和 邏輯運(yùn)算符)
- 不要忘了引號(hào),單引號(hào)或者雙引號(hào)都可以
11個(gè)EL隱含對(duì)象
pageScope、requestScope、sessionScope、applicationScope演示
- 要先在某個(gè)域中setAttribute
- 之前直接 ${ key1 } 是按四個(gè)域從小到大的順序去搜索的
- 如果使用比如 ${ applicationScope.key1 } 我們就可以選擇性地去搜索了
pageContext演示
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body>${ pageContext } <br>1、協(xié)議<%--request.getScheme() 可以獲取請(qǐng)求的協(xié)議--%><%=request.getScheme()%> <br> <%-- http --%>${ pageContext.request.scheme } <br> <%-- 因?yàn)樵趀l表達(dá)式中默認(rèn)會(huì)找點(diǎn)后面內(nèi)容所對(duì)應(yīng)的"get"方法 --%> <%-- http --%>2、請(qǐng)求的服務(wù)器ip或域名<%=request.getServerName()%> <br> <%-- localhost --%>${ pageContext.request.serverName } <br>3、服務(wù)器端口<%=request.getServerPort()%> <br>${ pageContext.request.serverPort } <br>4、獲取當(dāng)前的工程路徑<%=request.getContextPath()%> <br>${ pageContext.request.contextPath } <br>5、獲取請(qǐng)求方法<%=request.getMethod()%> <br>${ pageContext.request.method } <br>6、獲取客戶端ip地址<%=request.getRemoteHost()%> <br>${ pageContext.request.remoteHost } <br>7、獲取會(huì)話的id編號(hào)<%=session.getId()%> <br>${ pageContext.session.id } <br> </body> </html>- 有同學(xué)就要問啦,不是說好的el表達(dá)式比表達(dá)式腳本簡介嘛?有一個(gè)小技巧 :
其他EL隱含對(duì)象的示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body>${ param } </body> </html>
- 當(dāng)一個(gè)參數(shù)有多個(gè)值時(shí),用param只能得到其中一個(gè),要得到全部的,應(yīng)該用paramValues
- 如果有多個(gè)值的時(shí)候用headerValues,但是直接得到的是數(shù)組,還要配合下標(biāo)使用
- 要先在web.xml中配置參數(shù),用initParam才有值
JSTL標(biāo)簽庫
JSTL標(biāo)簽庫的介紹
標(biāo)簽庫的使用步驟
- 在web-inf目錄下創(chuàng)建一個(gè)lib目錄用來放jar包
- 還要 Add as Library,記得選 模塊庫,不是項(xiàng)目庫
set標(biāo)簽
- 其中,page是默認(rèn)值
if標(biāo)簽
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><c:if test="${ 12 == 12 }"><h1>12 == 12</h1></c:if> </body> </html>choose,when,otherwise標(biāo)簽
使用foreach遍歷1-10輸出
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><c:forEach begin="1" end="10" var="i"><h1> ${ i } </h1></c:forEach> </body> </html> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><table><c:forEach begin="1" end="10" var="i"><tr><td>第${ i }</td></tr></c:forEach></table> </body> </html>使用foreach遍歷Object數(shù)組
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><%request.setAttribute("arr", new String[]{"123", "456", "789"});%><c:forEach items="${ requestScope.arr }" var="item">${ item } <br></c:forEach> </body> </html>使用foreach遍歷map集合
<%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><%Map<String, Object> map = new HashMap<String, Object>();map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");request.setAttribute("map", map);%><%-- for ( Map.Entry<String, Object> entry : map.entrySet()) --%><c:forEach items="${ requestScope.map }" var="entry"><h1>${ entry } : ${ entry.key } = ${ entry.value }</h1></c:forEach> </body> </html>使用foreach遍歷list集合
<%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page import="java.util.List" %> <%@ page import="com.atguigu.pojo.Student" %> <%@ page import="java.util.ArrayList" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><%List<Student> studentList = new ArrayList<Student>();for (int i = 1; i <= 10; i ++ )studentList.add(new Student(i, "username" + i, "pass" + i, 18 + i, "phone" + i));request.setAttribute("stus", studentList);%><table><tr><th>編號(hào)</th><th>用戶名</th><th>密碼</th><th>年齡</th><th>電話</th><th>操作</th></tr><c:forEach items="${ requestScope.stus }" var="stu"><tr><td>${ stu.id }</td><td>${ stu.username }</td><td>${ stu.password }</td><td>${ stu.age }</td><td>${ stu.phone }</td><td> 刪除、修改 </td></tr></c:forEach></table> </body> </html>foreach標(biāo)簽所有屬性組合使用介紹
總結(jié)
以上是生活随笔為你收集整理的尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版(三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浙江农林大学第二十一届程序设计竞赛校选拔
- 下一篇: SQL进阶教程 | 史上最易懂SQL教程