JSTL的使用即JSTL中常用的标签
第一步:当然是找到jar包,然后导了,这个就不对说了。
第二步:在使用时要在jsp的表头声明它:<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core”%>,prefix:你使用事标签的前缀,URI:看图,我把它解释为我要使用的标签的来源路径。
IF与choose标签:
<%@ 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>
<!--
1. 单支条件判断:类似于if(表达式){.....}
仅当test的返回值为true时,才能执行标签中间的内容
-->
<c:if test="">条件为空</c:if>
<c:if test="false">条件为false</c:if>
<c:if test="true">条件为true</c:if><br/><br/>
<% pageContext.setAttribute("username", "admin"); %>
<c:if test="${empty username}">游客,请登录</c:if>
<c:if test="${not empty username}">${username},欢迎登录</c:if><br/><br/>
<!--
2. 多支条件判断:类似于if(表达式){.....}else if(表达式){.....}else{......}
仅当test的返回值为true时,才能执行标签中间的内容
-->
<c:choose>
<c:when test="${not empty username and username eq 'admin'}">管理员,${username} ,欢迎登陆!</c:when>
<c:when test="${not empty username and username ne 'admin'}">普通用户,${username} ,欢迎登陆!</c:when>
<c:otherwise>游客,请登录</c:otherwise>
</c:choose>
</body>
</html>
forEach标签:
<%@page import="com.neuedu.entity.CertType"%>
<%@page import="java.util.*"%>
<%@ 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>
<h1>1.循环打印1....10之间的整数:for(int i=1;i<=10;i++){ .... }</h1>
<c:forEach begin="1" end="10" step="1" var="i">
${i}
</c:forEach>
<h1>2.遍历数组:for(int num:array){.....}</h1>
<%
int[] array = new int[]{11,22,33,44,55};
pageContext.setAttribute("array", array);
%>
<c:forEach items="${array}" var="num">
${num}
</c:forEach>
<h1>3.遍历集合:for(Object obj:list){.....}</h1>
<%
List list = new ArrayList();
list.add(123);
list.add(true);
list.add("abc");
pageContext.setAttribute("list", list);
%>
<c:forEach items="${list}" var="obj">
${obj}
</c:forEach>
<h1>4.遍历逗号","分割的字符串</h1>
<c:forEach items="hello,kitty,true,finally,123" var="str">
${str}
</c:forEach>
<h1>5.遍历绑定下拉列表</h1>
<%
List<CertType> certTypeList = new ArrayList<CertType>();
certTypeList.add(new CertType(1,"二代身份证"));
certTypeList.add(new CertType(2,"港澳同行证"));
certTypeList.add(new CertType(3,"台湾通行证"));
certTypeList.add(new CertType(4,"护照"));
pageContext.setAttribute("certTypeList", certTypeList);
%>
<select>
<c:forEach items="${certTypeList}" var="certType">
<option value="${certType.id}">${certType.content}</option>
</c:forEach>
</select>
</body>
</html>