前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、c:out、c:if、c:choose、c:when、c:catch、c:url、c:param、c:forEach
-
c:out:向页面输出一串文本
两种用法:
<c:out value="value" default="defaultvalue" escapeXml="true||false"/>
<c:out value="value" default="defaultvalue" escapeXml="true||false"></c:out>
value:指定输出的文本文件
default:当value为空时指定的默认值
escapeXml:是否进行html编码后进行输出,默认为true
-
c:if:
两种用法:
<c:if test="test" var="result" scope="page|request|session|application"/>
<c:if test="test" var="result" scope="page|request|session|application"></c:if>
test:用于设置逻辑表达式
var:用于指定表达式中变量的名字
scope:指定var变量的作用范围
-
c:choose
用法:
c:when:判断语句
c:otherwise:否则
<c:choose> <c:when test="${ score>=90 }">优秀</c:when> <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>
-
c:catch
用来屏蔽掉异常
用法:
<c:catch> <% int i = 1 / 0; %> </c:catch>
var 可以设置值来接收异常信息(下面用error接收了异常信息)
<c:catch var="error"> <% int i = 1 / 0; %> </c:catch> <li>${ error }</li>
-
c:param
两种用法:
使用value指定参数值
<c:param name="name" value="value">
在标签体中指定参数值
<c:param name="name" paramter value</c:param>
name:用于指定参数名称
value:用于指定参数值(当使用
<c:patam>
标签作为一个URL的参数时,它会自动对字符进行URL编码) -
c:url
两种用法:
<c:url value="value" context="context" var="varname" scope="page|request|session|application">
<c:url value="value" context="context" var="varname" scope="page|request|session|application"> <c:param>标签 </c:url>
value:用于构造指定URL
context:用于指定同一服务器下的其他web应用的名称
var:用于指定将构造的URL地址保存到域对象的属性名称
scope:用于指定将构造好的URL保存到域对象中
-
c:import
用于加入页面
<c:import url="<string>" var="<string>" scope="<string>" varReader="<string>" context="<string>" charEncoding="<string>"/>
<c:import url="http://www.baidu.com" charEncoding="UTF-8"></c:import>
url 待导入资源的URL,可以是相对路径和绝对路径,并且可以导入其他主机资源 是 无 context 当使用相对路径访问外部context资源时,context指定了这个资源的名字。 否 当前应用程序 charEncoding 所引入的数据的字符编码集 否 ISO-8859-1 var 用于存储所引入的文本的变量 否 无 scope var属性的作用域 否 page varReader 可选的用于提供java.io.Reader对象的变量 否 无 -
c:forEach
两种用法:
迭代包含多个对象集合
<c:forEach var="varname" items="collection" varStatus="varStatusname" begin="begin" end="end" step="step">body content</forEach>
迭代指定范围的集合
<c:forEach var="varname" varstatus="varstatus" begin="begin" end="end" step="step">body content</c:forEach>
var:用于指定将当前迭代到元素保存page域中的名称
items:用于指定将要迭代的集合对象
varStatus:当前迭代状态信息的对象保存到page域中的名称
begin:用于指定从集合中第几个元素开始进行迭代
step:用于指定迭代步长
二、演示实例
package javaweb.servlet;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/jstl")
public class JTSLServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("title", "武汉城市学院");
request.setAttribute("message",
"<p style='color:red;font-size:20px;'>黄家大湾特1号</p>");
int score = (int)(Math.random() * 101);
request.setAttribute("score", score);
request.setAttribute("citys", new String[]{
"武汉", "黄冈", "黄石"});
Map<Integer, String> classes = new LinkedHashMap<Integer, String>();
classes.put(1, "19级软件1班");
classes.put(2, "19级软件2班");
classes.put(3, "19级软件3班");
classes.put(4, "19级软件4班");
request.setAttribute("classes", classes);
request.getRequestDispatcher("/jsp/jstl.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--引入core标签库--%>
<%@ 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>
<ul>
<li>${
title }</li>
<li>${
message }</li>
<li><c:out value="${ title }"/></li>
<li><c:out value="${ title1 }" default="默认值"/></li>
<li><c:out value="${ message }"></c:out></li>
<li><c:out value="${ message }" escapeXml="true"></c:out></li>
<li><c:out value="${ message }" escapeXml="false"></c:out></li>
<hr/>
<li>
查询的分数为:<strong style="color:red;">${
score }</strong>,
<c:if test="${ score>=60 }">及格</c:if>
<c:if test="${ score<60 }">不及格</c:if>
,
<c:choose>
<c:when test="${ score>=90 }">优秀</c:when>
<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>
</li>
<hr/>
<c:catch>
<%
int i = 1 / 0;
%>
</c:catch>
<c:catch var="error">
<%
int i = 1 / 0;
%>
</c:catch>
<li>${
error }</li>
<li>
<c:url value="/404.jsp">
<c:param name="a" value="1"></c:param>
<c:param name="b" value="2"></c:param>
</c:url>
</li>
<li><c:url value="/404.jsp" var="url"/></li>
<li><a href="<c:url value="/404.jsp"/>">404</a></li>
<li><a href="${ url }">404</a></li>
<%--
<li>
<c:import url="http://www.baidu.com" charEncoding="UTF-8"></c:import>
</li>
--%>
<c:set value="<%= true %>" var="test" scope="page"></c:set>
<li>${
pageScope.test }</li>
<li>
<ul>
<c:forEach items="${ citys }" var="i" varStatus="s">
<li>${
s.index},${
s.count},${
s.first},${
s.last}:${
i }</li>
</c:forEach>
</ul>
</li>
<li>
<select>
<c:forEach items="${ classes }" var="i">
<option value="${ i.key }">${
i.value }</option>
</c:forEach>
</select>
</li>
</ul>
</body>
</html>