JSP 5:JSTL标签库

※ JSTL标签库
JSP Standard Tag Library(JSTL)
1)让web项目支持JSTL标签库
在myeclipse中,建一个web项目的时候,在对话框下面会有提示在当前项目是否需要加入JSTL标签库的支持.(J2EE5.0是默认会加入对JSTL的支持的)
在eclipse中,建一个文本项目,默认都是不支持JSTL,所以需要我们自己把JSTL的jar包导入到项目中(复制粘贴到项目中的lib目录):jstl.jar standard.jar

2)把JSTL标签库导入到某一个jsp页面中
使用jsp中的taglib指令:

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

prefix="c"相当于给这个标签库起一个别名,将来在页面中就是用以c开头的标签来使用标签库中的标签。这个别名也可以叫其他的名字。

※※ <c:forEach>标签:
遍历List集合:
students是放进request对象中的一个List集合,集合中存放的是Student类型的对象.
items="“属性值是要遍历的集合
var=”" 属性值是每次遍历到的对象用什么名字的变量去接收。

         <c:forEach items="${students}" var="stu">
		<tr>
			<td>${stu.id }</td>
			<td>${stu.name }</td>
			<td>${stu.age }</td>
		</tr>
	 </c:forEach>

遍历Map集合:
map是一个Map类型的集合,放到了request对象中,entry是我们顶一个的一个变量,用做接收每次遍历到的集合中的一组键值对,我们可以通过entry.key entry.value分别拿到这次遍历到的key值和value值

        <c:forEach items="${map}" var="entry">
  		${entry.key }-->${entry.value.id } &nbsp; ${entry.value.name } &nbsp; ${entry.value.age }<br>
  	</c:forEach>
测试代码:
//pat.Servlet
package com.briup.web.Servelt;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.briup.web.bean.Address;
import com.briup.web.bean.User;
@WebServlet("/pat")
public class ParamTest1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<User> list=new  ArrayList<>();
		User user=new User("tom", "1111");
		Address addr=
				new Address("江苏省", "昆山", "巴城");
		user.setAddr(addr);
		User user1=new User("tom", "1111");
		Address addr1=
				new Address("江苏省", "昆山", "巴城");
		user1.setAddr(addr1);
		User user2=new User("tom", "1111");
		Address addr2=
				new Address("江苏省", "昆山", "巴城");
		user2.setAddr(addr2);
		list.add(user);
		list.add(user1);
		list.add(user2);
		request.setAttribute("list", list);
		HttpSession session=request.getSession();
		User [] us=new User[3];
		us[0]=user;
		us[1]=user1;
		us[2]=user2;
		session.setAttribute("us", us);
		Map<String, User> map=new HashMap<>();
		map.put("a", user);
		map.put("b", user1);
		map.put("c", user2);
		ServletContext sc=getServletContext();
		sc.setAttribute("map", map);
		request.getRequestDispatcher("/El1.jsp")
		.forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


<!-- EL1.jsp -->
<%@page import="com.briup.web.bean.User"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"
    	 prefix="c" %>
    <%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<!-- 获取后台传给页面的map对象 -->
	<table>
		<tr>
			<th>用户名</th>
			<th>密码</th>
			<th>省份</th>
			<th>城市</th>
			<th>街道</th>
		</tr>
		<c:forEach items="${applicationScope.map}"
		 var="en">
			<tr>
				<td>${en.value.name}</td>
				<td>${en.value.passwd}</td>
				<td>${en.value.addr.province}</td>
				<td>${en.value.addr.city}</td>
				<td>${en.value.addr.street}</td>
			</tr>
		</c:forEach>
	</table>
	<hr>
	<!-- items指向的是map集合,var所指明的
	变量代表键和值
	如果获取键 :entry.key 和值 entry.value
	 -->
	<c:forEach items="${applicationScope.map}"
		 var="entry">
		${entry.key}----${entry.value}<br>
	</c:forEach>
	<hr>
	<!-- 获取后台传给页面的数组对象 -->
	<table>
		<tr>
			<th>用户名</th>
			<th>密码</th>
			<th>省份</th>
			<th>城市</th>
			<th>街道</th>
		</tr>
		<c:forEach items="${sessionScope.us}" var="user">
			<tr>
				<td>${user.name}</td>
				<td>${user.passwd}</td>
				<td>${user.addr.province}</td>
				<td>${user.addr.city}</td>
				<td>${user.addr.street}</td>
			</tr>
		</c:forEach>
	</table>
	<hr>
	<!-- 获取后台传给页面的list集合对象 -->
	<table>
		<tr>
			<th>用户名</th>
			<th>密码</th>
			<th>省份</th>
			<th>城市</th>
			<th>街道</th>
		</tr>
		<%--
		for(Object u:list){
			
		}
		items指向的是集合-》list
		items集合从容器中用EL表达式获取
		var 循环每次返回的对象->u
		--%>
		<c:forEach items="${requestScope.list}" var="user">
			<tr>
				<td>${user.name}</td>
				<td>${user.passwd}</td>
				<td>${user.addr.province}</td>
				<td>${user.addr.city}</td>
				<td>${user.addr.street}</td>
			</tr>
		</c:forEach>
	</table>
	<hr>
	<table>
		<tr>
			<th>用户名</th>
			<th>密码</th>
			<th>省份</th>
			<th>城市</th>
			<th>街道</th>
		</tr>
		<%
		List<User> list= (List<User>)request.getAttribute("list");
		for(User u:list){
		%>
		<tr>
			<td><%= u.getName()%></td>
			<td><%= u.getPasswd()%></td>
			<td><%= u.getAddr().getProvince()%></td>
			<td><%= u.getAddr().getCity()%></td>
			<td><%= u.getAddr().getStreet()%></td>
		</tr>
		<%} %>
	</table>
</body>
</html>

※ <c:out>标签:
向页面输出内容

        <c:out value="hello"></c:out>
  	<c:out value="${5+5}"></c:out>
	//students是放在request中的List集合,集合里面是Student对象
  	<c:out value="${students[2].id}"></c:out>

※ <c:set>标签:
向某一个范围对象中存放一个值。

<c:set var="name" value="zhangsan" scope="request"></c:set>

※ <c:remove>标签:
从某个范围对象中把某个值给移除掉.

<c:remove var="name" scope="request"/>

※ <c:if>标签:
条件判断

        <%
  	request.setAttribute("score",40);
  	%>
  		
  	<c:if test="${score>85 }">
  		<font color="red">你的分数超过了85分</font>
  	</c:if>
	<c:if test="${score>95 }">
  		<font color="red">你的分数超过了95分</font>
  	</c:if>

	这样写相当于:
	if(score>85){
		...
	}
	if(score>95){
		...
	}

※ <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>
	相当于:
	if(score>=90){
	
	}else if(score>=80){
	
	}else if(score>=70){
	
	}eles if(score>=60){
	
	}else{
	
	}  
测试代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"
      prefix="c"%>
    <%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
	<%--
	等价于if(){
					}else if(){
					}else
					test最终结果boolean类型
					为true,执行对应标签中
					的文本
	 --%>
	<c:choose>
		<c:when test="${param.score>90}">
			优秀<br>
		</c:when>
		<c:when test="${param.score<=90&&param.score>80}">
			良好<br>
		</c:when>
		<c:when test="${param.score<=80&&param.score>60}">
			一般<br>
		</c:when>
		<c:otherwise>
			继续努力
		</c:otherwise>
	</c:choose>
	<hr>
	<%--等价于if(){}
	但是没有else  
	注意:test对应的属性值是boolean类型
	test属性值为true,执行标签中键文本
	的内容
	--%>
	<c:if test="${param.score>90}">
		<font color="red">ok</font>
	</c:if>
	<c:if test="${param.score<=90&&param.score>80}">
		<font color="red">so so</font>
	</c:if>
	<hr>
	<!-- 向某个容器(pageContext,request,
	session,application)存值
	var指的是存入容器键的名字
	value存入容器的值
	scope指的是容器
	 -->
	<c:set var="name" value="kkk" 
				scope="page"></c:set>
	<c:set var="us" value="${sessionScope.us}" 
				scope="page"></c:set>
	<!-- 从某个容器中移除数据 
	scope指定容器移除该容器中的指定数据
	没有指明容器,移除所有容器中
	var对应变量的数据
	-->
	<%-- <c:remove var="names" scope="page"/> --%>
	<c:remove var="us"/>
	${pageScope.name}<br>
	pageX:${pageScope.us}<br>
	sessionS:${sessionScope.us}<br>
	<hr>
	<!-- 将value中的内容输出给浏览器 
	out.write();
	-->
	<c:out value="test"></c:out>
	<c:out value="1+1"></c:out>
	<c:out value="${1+1}"></c:out>
	<c:out value="${1<1}"></c:out>
	<c:out value="${sessionScope.us}"></c:out>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值