JSP jstl core tag library

jstl tag library

Jar files need to use: jstl.jar, standard.jar.
1. The introduction of file information <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2. c:Use tag

set label

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="com.javaweb.beans.*,java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<br />--------------------c:set 为bean属性赋值------------------
	<br />
	<%
		Student stu = new Student();
		pageContext.setAttribute("stu", stu);
	%>
	<c:set value="里斯" property="name" target="${pageScope.stu}"></c:set>
	stu = ${stu}
	<br />
	<br />--------------------c:set 为Map属性赋值------------------
	<br />
	<%
		Map<String, Object> map = new HashMap<>();
		pageContext.setAttribute("map", map);
	%>
	<c:set value="历史" property="name" target="${pageScope.map}"></c:set>
	<c:set value="科目" property="types" target="${pageScope.map }"></c:set>
	map = ${map };
	<br /> map.value1 = ${map.name }; map.value2 = ${map.types };

remove tag

	<%
		Student stu = new Student();
		pageContext.setAttribute("stu", stu);
	%>
	<br />--------------------c:remove 删除------------------
	<br />
	<c:remove var="stu" />
	stu = ${stu };

catch tag

<br />--------------------c:cath 捕获异常------------------
	<br />
	<c:catch var="ex">
		<%
			int i = 3 / 0;
		%>
	</c:catch>
	ex = ${ex.message };

out label

1. When EL expression can not get content, you can specify the default content.
2. When you do not specify escapeXml property, will not resolve the html code
3.el expression will resolve custom html code

	<br />--------------------c:out 输出------------------
	<br /> 
	xxx =<c:out value="${xxx}" default="计算机"></c:out>
	<br /> 
	xxx = ${empty xxx ? '计算机':xxx}
	<br />
	<c:set var="topic" value="<h1>我哎呦</h1>"></c:set>
	topic =<c:out value="${topic}"></c:out>
	<br /> 
	topic =<c:out value="${topic}" escapeXml="false"></c:out>
	<br /> topic = ${topic}

if the label

	<br />--------------------c:if------------------
	<br />
	<c:set var="username" value="张三"></c:set>
	<c:if test="${username == '张三'}">
		<a href="#">进入系统</a>
	</c:if>

choose label

Matching principle similar to Java in the switch, progressive matching, to meet the conditions to exit.
Tags can only put <c: when> and <c: otherwise>.

<br />--------------------c:choose------------------
	<br />
	<c:set var="pageNum" value="5"></c:set>
	<c:set var="pagetotal" value="5"></c:set>
	<c:choose>
		<c:when test="${pageNum == 1}">
			首页     上一页    <a href="#">下一页</a>
			<a href="#"> 末页 </a>
			<br />
			当前是${pageNum}/${pagetotal}
		</c:when>
		<c:when test="${pageNum == pagetotal}">
			<a href="#">首页</a>
			<a href="#">上一页</a>下一页 末页<br />
			当前是${pageNum}/${pagetotal}
		</c:when>
		<c:otherwise>
			<a href="#">首页</a>
			<a href="#">上一页</a>
			<a href="#">下一页</a>
			<a href="#">末页</a>
			<br />
		</c:otherwise>
	</c:choose>

forEach tag

<head>
<style type="text/css">
	.odd{
		background-color: blue;
	}
	.even{
		background-color: red;
	}
</style>
</head>
<br />--------------------c:forEach------------------
	<br />
	<%
		List<Student> stus = new ArrayList<>();
		stus.add(new Student(11,"张1"));
		stus.add(new Student(11,"张2"));
		stus.add(new Student(11,"张3"));
		stus.add(new Student(11,"张4"));
		stus.add(new Student(11,"张5"));
		stus.add(new Student(11,"张6"));
		stus.add(new Student(11,"张7"));
		pageContext.setAttribute("stus", stus);
	%>
	<table border="1">
		<tr>
			<th>序号</th>
			<th>年龄</th>
			<th>姓名</th>
		</tr>
		<c:forEach items="${stus }" var ="stu" varStatus="vs">
		<!--实现表格逐行变色-->
			<tr class="${vs.count % 2 ==0 ? 'odd':'even'}">
				<!--vs.count获取当前遍历的次数 -->
				<td>${vs.count}</td>
				<td>${stu.age}</td>
				<td>${stu.name}</td>	
			</tr>
		</c:forEach>
	</table>
Published 114 original articles · won praise 8 · views 5480

Guess you like

Origin blog.csdn.net/OVO_LQ_Start/article/details/104864716