jstl
Posted ki16
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jstl相关的知识,希望对你有一定的参考价值。
<%@page import="test_el_jstl.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘jstl.jsp‘ starting page</title>
</head>
<body>
<%
request.setAttribute("weather", "今天天气很热!");
pageContext.setAttribute("aaa", "aaa");
request.setAttribute("aaa", "bbb");
session.setAttribute("aaa", "ccc");
application.setAttribute("aaa", "ddd");
request.setAttribute("score", 78);
List list = new ArrayList();
User u1 = new User(1, "admin", "admin", "老王");
User u2 = new User(2, "cai10", "admin", "蔡依林");
User u3 = new User(3, "cai20", "admin", "蔡依林");
User u4 = new User(4, "cai30", "admin", "蔡依林");
User u5 = new User(5, "cai40", "admin", "蔡依林");
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
list.add(u5);
request.setAttribute("persons", list);
Date date = new Date();
request.setAttribute("d1", date);
String dateStr = "1999/12/12 03:04:05";
request.setAttribute("ds", dateStr);
String str2 = "18810950652";
request.setAttribute("phone", str2);
%>
<h3>c:out标签:用于将作用域中的对象输出,default属性会在value属性为空时显示</h3>
<c:out value="${weather}" default="汗流浃背!"></c:out>
<h3>c:set标签:用于将一个数据保存在作用域中(默认最小作用中),通过scope设定存放的作用域</h3>
<c:set var="name" value="laowang" scope="request"></c:set>
${requestScope.name}
<c:set var="table">
<table border="1" style="border-collapse: collapse;">
<tr>
<td>1*1</td>
<td>1*2</td>
</tr>
<tr>
<td>2*1</td>
<td>2*2</td>
</tr>
</table>
</c:set>
${table}-${table}-${table}
<h3>c:remove标签:将作用域中的指定key的数据删除(默认删除所有作用域中的指定key的数据)</h3>
<c:remove var="aaa" scope="page" />
${aaa}
<h3>c:if标签:条件判断(单条件)</h3>
<c:if test="${score >= 80}" var="flag">
优秀!
</c:if>
<c:if test="${!flag}">
良好!
</c:if>
<h3>c:choose 、 c:when、 c:otherwise标签:条件判断(多条件)</h3>
<h4>注意以下几点:1.c:when必须在c:otherwise之前出现</h4>
<h4>2.c:otherwise只能出现一次</h4>
<h4>3.c:when和c:when和c:otherwise之前不允许出现其他任何字符</h4>
<c:choose>
<c:when test="${score >=80 }">
优秀!
</c:when>
<c:when test="${score >=70 }">
良好
</c:when>
<c:when test="${score >=60 }">
及格
</c:when>
<c:otherwise>
不及格
</c:otherwise>
</c:choose>
<h3>c:forEach标签:循环</h3>
<h4>items:循环体,通过使用EL表达式获取循环的内容</h4>
<h4>begin & end:只能为数字,当没有items属性时,从begin数字循环到end数字;当有items属性时,begin & end就表示下标</h4>
<h4>step:步长</h4>
<h4>var:表示每次的循环体</h4>
<h4>varStatus:当前循环体的状态:有几个属性--first(判断当前循环体是否为第一个)、last(判断当前循环体是否为最后一个)、count(当前循环次数)、index(当前循环体的下标)</h4>
<c:forEach begin="3" end="9" var="each">
${each}
</c:forEach>
<br>
<c:forEach items="4123,2512,222,111,555,677,999" begin="1" end="3" var="each">
${each}
</c:forEach>
<br>
<c:forEach items="${persons}" step="2" var="person">
${person.username}<br>
</c:forEach>
<br>
<c:forEach items="${persons}" var="person" step="2" varStatus="status">
${person.username} --- ${status.first} --- ${status.last} --- ${status.count} --- ${status.index}<br>
</c:forEach>
<br>
<table border="1" style="border-collapse: collapse;">
<tr><td>id</td><td>用户名</td><td>真实姓名</td></tr>
<c:forEach items="${persons}" var="person">
<tr><td>${person.id}</td><td>${person.username }</td><td>${person.realname }</td></tr>
</c:forEach>
</table>
<h3>c:redirect标签:重定向</h3>
<%-- <c:redirect url="http://www.baidu.com"></c:redirect> --%>
<h3>c:import标签:指定一个url的页面在本页面中</h3>
<%--<c:import url="http://www.baidu.com"></c:import> --%>
<fmt:formatDate value="${d1}" pattern="yyyy年MM月dd日 HH时mm分ss秒"/>
<fmt:parseDate value="${ds}" var="d2" pattern="yyyy/MM/dd HH:mm:ss"></fmt:parseDate>
${fn:length(phone) }
</body>
</html>
以上是关于jstl的主要内容,如果未能解决你的问题,请参考以下文章