java web 学习之JSTL 自定义标签

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值