二.JSP标签
作用:
1.流程判断
2.跳转页面
....
分类:
1.内置标签:不需要在JSP页面导入标签
2.jstl标签:需要在JSP页面中导入标签
3.自定义标签:开发者自定义,需要在JSP页面导入标签
动态标签:
1.转发标签:<jsp:forWard/>
2.参数标签:<jsp:pararm/>
3.包含标签:<jsp:include/>
1.内置标签
- 转发标签<jsp:forWard/>
index.jsp主要代码
<jsp:forward page="index2.jsp">
<jsp:param name="mahan" value="huan"></jsp:param>
</jsp:forward>
index2.jsp
<%--
Created by IntelliJ IDEA.
User: pc
Date: 17-4-14
Time: 下午4:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%=request.getParameter("mahan")%>
</body>
</html>
- 包含标签<jsp:include/>
index.jsp主要代码
<jsp:include page="head.jsp"></jsp:include>
上面是头部
head.jsp代码
<%--
Created by IntelliJ IDEA.
User: pc
Date: 17-4-14
Time: 下午4:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>我是头部</h1>
</body>
</html>
2.jstl标签
全名(java standard tag libarary) java标准标签库
1.核心标签库(C标签库)--重点
2.国际哈化标签库(fmt标签库)
3.EL函数库(fn函数库)
4.XML标签库(x标签库)--次要
5.sql标签库(sql标签库)--次要
- 核心标签库
保存数据:
<c:set></c:set>
获取数据:
<c:out value=""></c:out>
单条件判断:
<c:if test=""></c:if>
<多条件判断>
<c:choose></c:choose>
<c:when test=""></c:when>
<c:otherwise></c:otherwise>
循环数据:
<c:forEach></c:forEach>
<c:forTokens items="" delims="">
</c:forTokens>
重定向 :
<c:redirect></c:redirect>
1)导入jstl支持的jar包:下载地址 密码: g853
2)导入标签库
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3)标签
a.保存数据:<c:set></c:set>.
默认将数值存入page域中
<c:set var="mahuan" value="huanhuan"></c:set>
${mahuan}
scope="域对象"(page,request,session,application)
<c:set var="mahuan" value="huanhuan" scope="request"></c:set>
${requestScope.mahuan}
b.获取数据:<c:out value=""></c:out>
从域中获取数值
<c:set var="mahuan" value="huanhuan" scope="request"></c:set>
<c:out value="${mahuan}"></c:out>
default="默认值" 当值为null时显示默认值
escapeXml=" "(true false)是否对value的值进行转义默认转义(true)
<%
String str =null;
pageContext.setAttribute("aaa",str);
%>
<c:out value="${aaa}" default="<h2>默认值</h2>" escapeXml="false"></c:out>
c.单条件判断:<c:if test=""></c:if>
test=" "(true false)判断是否要输出
<c:if test="true">
判断输出
</c:if>
<c:if test="${30>22}">
判断输出
</c:if>
d.<多条件判断>
<c:choose></c:choose>
<c:when test=""></c:when>
<c:otherwise></c:otherwise>
<c:set var="mahuan" value="78" ></c:set>
<c:choose>
<c:when test="${mahuan>90 && mahuan<=100}">
特等奖
</c:when>
<c:when test="${mahuan>80 && mahuan<=90}">
一等奖
</c:when>
<c:when test="${mahuan>70 && mahuan<=80}">
二等奖
</c:when>
<c:when test="${mahuan>60 && mahuan<=70}">
三等奖
</c:when>
<c:otherwise>
参赛奖
</c:otherwise>
</c:choose>
e.循环数据:<c:forEach></c:forEach>
begin= " " 从那个元素开始遍历,默认从0开始
end=" " 到那个数据结束,默认到最后一个结束
step=“ ” 步长 默认为1
items=“ ” 需要遍历的数据集合
var=“ ” 每个元素的名称
varStatus= “ ” 当前正在遍历元素的状态对象(count属性:当前位置,从1开始)--序列
list集合:
<%
List<Student> list= new ArrayList<Student>();
list.add(new Student("张三","21"));
list.add(new Student("小明","32"));
list.add(new Student("小花","44"));
pageContext.setAttribute("list",list);
%>
<c:forEach begin="0" end="2" items="${list}" step="1" var="Student" varStatus="varsta">
序号:${varsta.count} 姓名: ${Student.name} 年龄:${Student.age}<br>
</c:forEach>
map集合:
<%
Map<String ,Student> map = new HashMap<String,Student>();
map.put("011",new Student("张飞","44"));
map.put("012",new Student("小花","47"));
map.put("013",new Student("小明","52"));
pageContext.setAttribute("map",map);
%>
<c:forEach begin="0" end="2" items="${map}" step="1" var="Student" varStatus="varsta">
序号:${varsta.count} 编号${Student.key} 姓名: ${Student.value.name} 年龄:${Student.value.age}<br>
</c:forEach>
f.<c:forTokens items="" delims=""> </c:forTokens>
循环特殊字符串
<%
String sr = "ma-huan-huan-ni-hao";
pageContext.setAttribute("huan",sr);
%>
<c:forTokens items="${huan}" delims="-" var="s">
${s}<br>
</c:forTokens>
g.<c:redirect></c:redirect>
重定向
<c:redirect url="http://www.jianshu.com/u/ac5fd281184c"></c:redirect>
-
完
文集:JavaEE--学习笔记