7.jsp頁面中的路徑
一般情況下,jsp中路徑問題是和我們之前在servlet中討論的html裏面的路徑問題是一樣的,但是在jsp中有一種情況是要特殊對待的。
如果在jsp頁面的上面寫了這樣一個腳本:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
並且再<head>標籤中加入了一個子標籤:
<base href="<%=basePath%>" />
如果在jsp頁面裏面有上面說提到的倆個東西,那麼在這個jsp頁面中,我們再去寫上一個相對路徑(最左邊沒有加/的那種路徑),它就不是相對於地址欄中的當前路徑了,而是要相對於這個basePath變量所代表的這個路徑.
<base href="..">這個標籤的作用:
在沒有這個標籤的情況下,頁面中相對路徑的跳轉,都要參照地址欄中的當前路徑來跳轉,但是頁面中加上了這個<base>標籤後,頁面中的相對路徑的跳轉都要參照這個標籤中所寫的路徑來跳轉。
注意:這裏說的相對路徑的跳轉指的是最左邊沒有/的那種路徑的跳轉.
8.EL表達式
形式:${ }
作用:從一個範圍裏面取值或者從一個對象中取值或是向頁面輸出值.
1.接收客戶端參數.
${param.name1 }
2.指定範圍並取值
${pageScope.name2 }
${requestScope.name3 }
${sessionScope.name4 }
${applicationScope.name5 }
3.可以不指定範圍再去取值
${name}
這時候會按照pageContext request session application這樣一個順序依次的去找有沒有一個叫name的值存在,一旦找到了就輸出出來,最終沒有找到那麼就什麼都不輸出。
4.取出一個對象中的屬性值.
${requestScope.student.id}
${requestScope.student.name}
${requestScope.student.age}
或者
${student.id}
${student.name}
${student.age}
或者
${student["id"]}
${student["name"]}
${student["age"]}
注意:比如 ${student.id}表示是要調用student對象中的getId方法,至於對象中有沒有id屬性對這個操作沒有任何影響.
如果Student類中一個方法是getAddress,返回一個Address類的對象,Address類中有一個方法getCity,這個時候我們就可以這樣寫去拿到city屬性的值.
${student.address.city}
5.輸出字符串
${"hello"}
6.輸出運算結果或者boolean表達式
${1+1 }
${(1+2)*3-4+5*3 }
${1<3 }
//爲空的話返回true
${empty "" }
${empty "hello" }
//取否 不爲空的話返回true
${not empty "hello" }
${! empty "hello" }
${param.score >50 }
${param.score >60?"good":"bad" }
7.輸出數組、集合中的元素
<%
String[] str = {"hello","world"};
List<String> list = new ArrayList<String>();
list.add("zhangsan");
list.add("lisi");
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("a",100);
map.put("b",200);
map.put("c",300);
request.setAttribute("str",str);
request.setAttribute("list",list);
request.setAttribute("map",map);
%>
${str[0] }<br>
${list[1] }<br>
${map["c"] }<br>
9.JSTL標籤庫
JSP Standard Tag Library(JSTL)
1.讓web項目支持JSTL標籤庫
在myeclipse中,建一個web項目的時候,在對話框下面會有提示在當前項目是否需要加入JSTL標籤庫的支持.(J2EE5.0是默認會加入對JSTL的支持的)
在eclipse中,建一個文本項目,默認都是不支持JSTL,所以需要我們自己把JSTL的jar包導入到項目中(複製粘貼到項目中的lib目錄):jstl.jar standard.jar
2.把JSTL標籤庫導入到某一個jsp頁面中
使用jsp中的taglib指令:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
prefix="c"相當於給這個標籤庫起一個別名,將來在頁面中就是用以c開頭的標籤來使用標籤庫中的標籤。這個別名也可以叫其他的名字。
<c:forEach>標籤:
遍歷List集合:
students是放進request對象中的一個List集合,集合中存放的是Student類型的對象.
items=""屬性值是要遍歷的集合
var="" 屬性值是每次遍歷到的對象用什麼名字的變量去接收。
<c:forEach items="${students}" var="stu">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.age }</td>
</tr>
</c:forEach>
遍歷Map集合:
map是一個Map類型的集合,放到了request對象中,entry是我們頂一個的一個變量,用做接收每次遍歷到的集合中的一組鍵值對,我們可以通過entry.key entry.value分別拿到這次遍歷到的key值和value值
<c:forEach items="${map}" var="entry">
${entry.key }-->${entry.value.id } ${entry.value.name } ${entry.value.age }<br>
</c:forEach>
<c:out>標籤:
向頁面輸出內容
<c:out value="hello"></c:out>
<c:out value="${5+5}"></c:out>
//students是放在request中的List集合,集合裏面是Student對象
<c:out value="${students[2].id}"></c:out>
<c:set>標籤:
向某一個範圍對象中存放一個值。
<c:set var="name" value="zhangsan" scope="request"></c:set>
<c:remove>標籤:
從某個範圍對象中把某個值給移除掉.
<c:remove var="name" scope="request"/>
<c:if>標籤:
條件判斷
<%
request.setAttribute("score",40);
%>
<c:if test="${score>85 }">
<font color="red">你的分數超過了85分</font>
</c:if>
<c:if test="${score>95 }">
<font color="red">你的分數超過了95分</font>
</c:if>
這樣寫相當於:
if(score>85){
...
}
if(score>95){
...
}
<c:choose>標籤
<c:when>標籤
<c:otherwise>標籤
例如:
<c:choose>
<c:when test="${score>=90 }">優</c:when>
<c:when test="${score>=80 }">良</c:when>
<c:when test="${score>=70 }">中</c:when>
<c:when test="${score>=60 }">及格</c:when>
<c:otherwise>差</c:otherwise>
</c:choose>
相當於:
if(score>=90){
}else if(score>=80){
}else if(score>=70){
}eles if(score>=60){
}else{
}