javaWeb之jstl简单总结(9个标签)

JSTL的核心标签

<c:out> 
    <c:out>主要用来显示数据的内容,就像是 <%= scripting-language %> 一样,例

Hello ! <c:out value="${username}" /> 
	
	<%
		request.setAttribute("book", "<<java>>");
	%>
	
	<!-- 用el表达式 -->
	
	book:${requestScope.book }
	
	<br/>
	<!-- jstl表达式 -->
	
	book:<c:out value="${requestScope.book }"/>  
	
	/*
	输出
	c out
	book:<> 
	book:<<java>> 
	*/

<c:set>         
    <c:set>主要用来将变量储存至 JSP 范围中或是 JavaBean 的属性中     其中value属性支持el表达式还可以为 JavaBean 的属性赋值。 target和value都支持el表达式
    
    语法 1:将 value 的值储存至范围为 scope 的 varName 变量之中

<c:set value="value" var="varName" [scope="{ page|request|session|application }"]/> 
	
<c:set var="subject" value="${param.subject }" scope="session"></c:set>

语法 2:将 value 的值储存至 target 对象的属性中

< c:set value="value" target="target" property="propertyName" /> 
	
<c:set target="${requestScope.cust }" property="id" value="${param.id }"></c:set>

<c:remove> 
    <c:remove>主要用来移除变量。

<c:remove var="varName" [scope="{ page|request|session|application }"] />
	
<c:remove var="data" scope="request"/>

jstl.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html >
<html>
<head>
<meta  charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h4>c out</h4>
	
	<c:out value="${param.name }"></c:out>
	<%
		request.setAttribute("book", "<<java>>");
	%>
	
	<!-- 用el表达式 -->
	
	book:${requestScope.book }
	
	<br/>
	<!-- jstl表达式 -->
	
	book:<c:out value="${requestScope.book }"/>
	<br/>
	
	<h4>c set</h4>
	<c:set var="data" value="1997-02-11" scope="request"></c:set>
	
	data:${requestScope.data }
		<br/>
	<c:remove var="data" scope="request"/>
	
	data:${requestScope.data }
</body>
</html>

<c:if>
    <c:if test="testCondition" [var="varName"] 
     [scope="{page|request|session|application}"]> 
    具体内容</c:if> (只有c:if没有else,但可以把判断的结果储存起来,以备之后使用)

<c:set var="age" value="19" scope="request"></c:set>
	<c:if test="${requestScope.age>20 }" var="hh" scope="page">微米黑骨</c:if>
	<c:out value="${pageScope.hh }"></c:out>

	/*
	输出结果
	false
	*/

<c:choose>,<c:when>,<c:otherwise>相当于 if... else if ...  else if...  else

<c:choose> 
     本体内容( <when> 和 <otherwise> ) 
    </c:choose> 
其中<c:when>,<c:otherwise>不能脱离<c:choose>
<c:otherwise>必须在<c:when>之后使用

<c:otherwise>必须在<c:when>之后使用
	<c:choose>
		<c:when test="${param.age>60 }">老年人</c:when>
		<c:when test="${param.age>35 }">成年人</c:when>
		<c:when test="${param.age>18 }">青年人</c:when>
		<c:otherwise>haha</c:otherwise>
	</c:choose>

<c:forEach> 
    <c:forEach> 为循环控制,它可以将集合(Collection)中的成员循序浏览一遍。运作方式为当
    条件符合时,就会持续重复执行<c:forEach>的本体内容。

语法 1:迭代一集合对象之所有成员
<%
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		request.setAttribute("ss", list);
	%>
	<c:forEach items="${requestScope.ss }" var="tt">
		${tt }&nbsp;&nbsp;&nbsp;
	</c:forEach>
	
	//a    b    c    d 
	
	//begin从0开始计算
	<c:forEach items="${requestScope.ss }" var="tt" begin="0" step="2" >
		${tt }&nbsp;&nbsp;&nbsp;
	</c:forEach>
	
	//a    c    e    g 
	
	语法 2:迭代指定的次数
	<c:forEach begin="1" end="10" step="2" var="data">
		${data } &nbsp;&nbsp;&nbsp;
	</c:forEach>
	
	//1     3     5     7     9  

	
	
	看status(状态)的属性	
	<%
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		list.add("f");
		list.add("g");
		request.setAttribute("ss", list);
	%>
	<c:forEach items="${requestScope.ss }" var="tt" begin="0" step="1" varStatus="status">
		${status.index }&nbsp;${status.count }&nbsp;${status.first }&nbsp;${status.last }&nbsp;${tt }&nbsp;&nbsp;<br/>
	</c:forEach>
	
	/*
	0 1 true false a  
	1 2 false false b  
	2 3 false false c  
	3 4 false false d  
	4 5 false false e  
	5 6 false false f  
	6 7 false true g
	*/
	
	<!-- 遍历 Map -->
	<% 
		Map<String, Customer> custMap = new HashMap<String, Customer>();
		custMap.put("a", new Customer(1, "AAA")); //index: 0 
		custMap.put("b", new Customer(2, "BBB")); //index: 0 
		custMap.put("c", new Customer(3, "CCC")); //index: 0 
		custMap.put("d", new Customer(4, "DDD")); //index: 0 
		custMap.put("e", new Customer(5, "EEE")); //index: 0 
		custMap.put("f", new Customer(6, "FFF")); //index: 0 
		
		request.setAttribute("custMap", custMap);
	%>
	
	<br><br>
	<c:forEach items="${requestScope.custMap }" var="cust">
		${cust.key } - ${cust.value.id } - ${cust.value.name }<br>
	</c:forEach>
	
	<!-- 遍历 数组 -->
	<% 
		String [] names = new String[]{"A", "B", "C"};
		request.setAttribute("names", names);
	%>
	<br><br>
	<c:forEach var="name" items="${names }">${name }-</c:forEach>
	
	
	<!-- 获取request属性名 -->
	<c:forEach items="${pageContext.request.attributeNames }" var="tt">
		${tt }--
	</c:forEach>
	
	/*
	ss--
	*/

jstl2.jsp 

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html >
<html>
<head>
<meta  charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:forEach begin="1" end="10" step="2" var="data">
		${data } &nbsp;&nbsp;&nbsp;
	</c:forEach>
	
	<br/>
	<%
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		list.add("f");
		list.add("g");
		request.setAttribute("ss", list);
	%>
	<c:forEach items="${requestScope.ss }" var="tt">
		${tt }&nbsp;&nbsp;&nbsp;
	</c:forEach>
	<br/>
	<c:forEach items="${requestScope.ss }" var="tt" begin="0" step="1" varStatus="status">
		${status.index }&nbsp;${status.count }&nbsp;${status.first }&nbsp;${status.last }&nbsp;${tt }&nbsp;&nbsp;<br/>
	</c:forEach>
	
	<br/>
	
	<!-- 获取request属性名 -->
	<c:forEach items="${pageContext.request.attributeNames }" var="tt">
		${tt }--
	</c:forEach>
</body>
</html>

URL操作

   JSTL 包含三个与 URL 操作有关的标签,它们分别为:<c:import>、<c:redirect>和<c:url>。
    它们主要的功能是:用来将其他文件的内容包含起来、网页的导向,还有 url 的产生。笔者将依序
    介绍这三个标签。

<c:import> :可以包含任意页面到当前页面

<h4>aqswdefrgth</h4>
<c:import url="http://www.baidu.com" charEncoding="utf-8"></c:import>
<c:import url="/jstl.jsp" charEncoding="utf-8"></c:import>
	

<c:redirect>:使当前Jsp页面重定向到指定的页面,使当前jsp转发到指定的页面

 相对路径
	标签处理器解析	("/")代表的使WEB应用的根目录
	<c:redirect url="/jstl.jsp"></c:redirect>  ("/")代表的使WEB应用的根目录  http://localhost:8080/JSTL
	<jsp:forward page="/jstl.jsp"></jsp:forward> ("/")代表的使WEB应用的根目录  http://localhost:8080/JSTL
	浏览器解析       ("/")代表的使WEB站点的根目录 
	response.redirect("/")  ("/")代表的使WEB站点的根目录  http://localhost:8080
 绝对路径
 	<c:redirect url="http://www.baidu.com"></c:redirect>

    <c:url> 
     <c:url>主要用来产生一个 URL。可以根据Cookie是否可用,来智能进行URL重写。对Get请求的参数进行解码。
     可以把产生的URL存储在域对象的属性中。
     (请求参数)还可以使用c:param为URL添加参数。c:url会对参数进行自动的转码。
     

	<c:url value="/jstl.jsp" var="testurl" scope="page">
		<c:param name="name" value="wjc"></c:param>
	</c:url>
	
	url:${testurl }  相当于  	url:${pageScope.testurl }
	<a href="${testurl }">sdf</a>
	<!-- url:/JSTL/jstl.jsp?name=%e7%8e%8b%e5%98%89%e8%af%9a -->

jstl3.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html >
<html>
<head>
<meta  charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>	aqswdefrgth</h4>

	 <a href="jstl.jsp?id=${param.name }" >asdfghjkl</a>
<%-- 	

		<c:import url="/jstl.jsp" charEncoding="utf-8"></c:import>
<c:redirect url="jstl.jsp"></c:redirect>

	<jsp:forward page="/jstl.jsp"></jsp:forward>
	
	<c:redirect url="http://www.atguigu.com"></c:redirect>

	
	<c:url value="/jstl.jsp" var="testurl" scope="page">
		<c:param name="name" value="王嘉诚"></c:param>
	</c:url>
	
	url:${testurl }
	 <!-- url:/JSTL/jstl.jsp?name=%e7%8e%8b%e5%98%89%e8%af%9a -->
	 
	
	--%>
	
	<c:url value="/jstl.jsp" var="testurl" scope="page">
		<c:param name="name" value="王嘉诚"></c:param>
	</c:url>
	<a href="${testurl }">sdf</a>

	 <!-- url:/JSTL/jstl.jsp?name=%e7%8e%8b%e5%98%89%e8%af%9a -->
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值