biblioteca de etiquetas núcleo JSP jstl

biblioteca de etiquetas jstl

archivos jar necesidad de usar: jstl.jar, standard.jar.
1. La introducción de la información del archivo <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2. c:El uso de etiquetas

etiqueta conjunto

<%@ 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 };

quitar la etiqueta

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

etiqueta de captura

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

etiqueta cabo

1. Cuando EL expresión no puede obtener el contenido, puede especificar el contenido predeterminado.
2. Cuando no se especifica la propiedad escapeXml, no resolverá el código html
expresión 3.El resolverá código HTML personalizado

	<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}

si la etiqueta

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

elegir la etiqueta

Coincidencia principio similar a Java en el interruptor, adaptación progresiva, para cumplir con las condiciones para salir.
Las etiquetas sólo pueden poner <c: when> y <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>

etiqueta forEach

<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>
Publicados 114 artículos originales · ganado elogios 8 · vistas 5480

Supongo que te gusta

Origin blog.csdn.net/OVO_LQ_Start/article/details/104864716
Recomendado
Clasificación