【核心标签库 <c:if>】
l 用于条件判断
l 没有与if配套的else标签。如果要实现if..else甚至else if的效果,要用后面讲的<c:choose>
属性名 | 是否支持EL表达式 | 属性类型 | 属性说明 |
test | true | Boolean | 指定条件表达式 |
var | false | String | 为test判断的结果起一个名字,并存入scope指定的web域中 |
scope | false | String | page、request、session、application |
empty判断是否有元素
<pre name="code" class="javascript">package com.rupeng.web3;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ELTest3Servlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
req.setAttribute("age", 3);
req.setAttribute("name", "rupeng");
req.setAttribute("nickName", null);
List<String> names = new LinkedList<String>();
//names.add("fafdas");
//names.add("321");
req.setAttribute("names", names);
String[] names2={"afsadfa","342423"};
req.setAttribute("names2", names2);
req.getRequestDispatcher("/ElTest3.jsp").forward(req, resp);
}
}
【核心标签库 <c:choose>】
c:choose、c:when 、c:otherwise
这组标签可以用来代替if-else-if-else语句。和switch不一样,因为switch是判断单个变量的多个值,而when的条件可以各种各样。
属性
只有when标签有test属性,表示需要判断的表达式
注意点
c:choose需要包裹c:when和c:otherwise,
c:otherwise(可选)需要放到c:when标签后面
【ELTest3.jsp】
<pre name="code" class="html"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.rupeng.web3.Person"%>
<%@page import="java.util.HashMap"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${age ge 18}" var="isCNR">
我们都是成年人啦
</c:if>
<c:out value="${pageScope.isCNR}"></c:out>
<c:if test="${nickName eq null}">
没有昵称
</c:if>
<c:if test="${empty nickName}">
没有昵称
</c:if>
<c:if test="${not empty nickName}">
我的昵称是:<c:out value="${nickName}" />
</c:if>
<c:out value="${names[1]}"/>
<c:if test="${empty names}">
names为空
</c:if>
<c:if test="${empty names2}">
names2为空
</c:if>
<c:choose>
<c:when test="${age lt 18}">
未成年人
</c:when>
<c:when test="${age lt 50}">
青年人
</c:when>
<c:otherwise>
老年人
</c:otherwise>
</c:choose>
<img src="fbb.jpg" />
</body>
</html>
【核心标签库 <c:url>】
c:url标签用来构造url地址,提供给其他html标签如a,img使用。
可以在构造时加入请求参数:直接在value属性值之后追加,还可以使用c:param添加(*)
能够处理虚拟路径到全路径的转换,因为他考虑了ContextPath。
除非明确知道在同级路径下或者确定的自路径下,否则不要用相对路径、更不要用"..",因为很容易乱套。建议其他情况都用从网站根目录开始的绝对路径"/"。网站部署时候的contextPath可能和开发的时候不一样,还有可能没有contextPath
<img src="<c:urlvalue='/fbb.jpg'/>"/> <img src="/fbb.jpg"/>
form中的action用c:url可以很好的解决全路径的问题
【核心标签库 <c:redirect>】
c:redirect 转发或者重定向
【el5.jsp】
<pre name="code" class="html"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<img src="/myweb3/fbb.jpg" />
<img src="<c:url value='/fbb.jpg'/>"/>
<p>
<c:url value='/fbb.jpg'/> //动态的获得url 不写死
</p>
<a href="<c:url value='/index.jsp' var='fPath' />">index.jsp</a>
<c:out value="${fPath}" />
<c:redirect url="/index.jsp"></c:redirect>
</body>
</html>
【核心标签库 <c:param>】
作用:为c:url、c:redirect、 c:import(后面讲)标签添加请求参数,并对参数进行url编码,追加到url地址后面
用法:<c:param name="name" value="value" />
【el6.jsp】
<pre name="code" class="html"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("age",8);
%>
<a href="<c:url value='/index.jsp'><c:param name='name' value='如>鹏<'></c:param><c:param name='age' value='${age}'></c:param></c:url>">rupeng</a>
</body>
</html>
【JSTL函数(*) 】
用来在JSP页面中操作字符串
JSTL函数就是EL表达式的函数,需要在el表达式中使用,而EL函数实质上是Java类的静态方法。很少用。
fn:toLowerCase fn:toUpperCase
fn:trim fn:escapeXml fn:length
fn:split fn:join
fn:indexOf fn:replace
fn:contains fn:containsIgnoreCase
fn:startsWith fn:endWith
fn:substring fn:substringAfter fn:substringBefore