传智播客JavaWeb day06-jstl
一、jsp标签(sun公司提供的)
二、EL表达式
三、jstl (javaserver pages standard tag library)
1.为什么要有jstl
jsp标签太弱,el表达式功能不完整(比如不能遍历集合),所有就有了jstl。目的就是配合jsp+el取代在jsp中嵌入Java代码的方式,提高程序的可读性、维护性、方便性
2. jstl实质、原理
jstl的实现过程就相当于el表达式中调用Java方法的实现过程。首先定义类包含静态方法,定义一个tdl文件对函数进行描述,然后在页面上用tablib指令引用文件,然后就可以通过前缀加冒号调用函数了
3.jstl五个类型的库(核心标签库、国际化标签库、数据库标签库、xml标签库、jstl函数,其中SQL标签库跟xml标签库不常用所以不学)
3.1 core标签库
3.1.1 c:out
3.1.1.1 输出常量
3.1.1.2 输出变量
3.1.1.3 输出默认值(有值就输出值没有值就输出默认值)
3.1.1.4 html转义输出 ps:escapeXml属性能控制是否按转义输出默认值为true
jsp代码如下:
- <%@ page language="java" import="java.util.*" 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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'cout.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <h1>输出常量</h1><hr>
- <c:out value="你好我这是输出常量"></c:out>
- <h1>输出变量</h1><hr>
- <%String name = "无双";
- pageContext.setAttribute("name",name);
- %>
- <c:out value="${name }"></c:out>
- <h1>输出默认值</h1><hr>
- <c:out value="${name}" default="奥巴马(这个是默认值)"></c:out>
- <c:out value="${name1 }" default="奥巴马(这个是默认值)"></c:out>
- <h1>输出html转义</h1><hr>
- <c:out value="<a href='#'>html转义内容</>" escapeXml="true"></c:out>
- </body>
- </html>
页面效果:
3.1.2 c:set ps:var属性有变量则找到变量没有则声明一个变量;value属性设置变量值;scope:指定域;
3.1.2.1 设置或修改域中属性的值
3.1.2.2 设置或修改域中map的属性值
3.1.2.3 设置或修改域中javabean的属性值
jsp代码:
- <%@ page language="java" import="java.util.*" 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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'cset.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <h1>设置域中属性的值</h1>
- <c:set var="name" value="力宏" scope="page"></c:set> <br>
- ${name }
- <c:set var="name" value="王力宏" scope="page"></c:set> <br>
- ${name }
- <h1>设置域中map的属性值</h1><hr>
- <%Map map = new HashMap();
- pageContext.setAttribute("map", map);
- %>
- <c:set target="${map }" property="cellphone" value="10086"></c:set>
- ${map.cellphone}(设置map值)<br>
- <c:set target="${map }" property="cellphone" value="10087"></c:set>
- ${map.cellphone}(修改map值)
- <h1>设置域中javabean的属性值(用法同map)</h1><hr>
- </body>
- </html>
效果图:
3.1.3 c:remove 移除域中属性的值 <c:remove var="name" scope="page"/>指定变量名称然后指定域
3.1.4 c:catch 捕获异常(把异常抓起来,捕获了页面上就不会有异常了,用户体验就友好了)
- <c:catch var="e">
- <%int i= 1/0; %>
- </c:catch>
- ${e.message}
效果:
3.1.5 c:if 执行判断条件,有一个很重要的属性test接收一个bool值
3.1.6 c:choose、c:when、c:otherwise
c:chooose要c:when和c:otherwise配合使用
3.1.7 c:foreach ps:items属性指定要遍历的集合
3.1.7.1 遍历数组 string[]
3.1.7.2 遍历集合 list
3.1.7.3 遍历map HashMap、linkedHashMap(按顺序输出)
3.1.7.4 循环执行指定的内容若干次
3.1.8 c:forTokens 切割字符串
3.1.9 c:import 包含文件
3.1.10 c:redirect、c:param 重定向
3.1.11 c:url 替代代码进行url重写
代码:cforeach、cfortokens
- <%@ page language="java" import="java.util.*" 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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'cforeach.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <%String[] cities = new String[]{"北京","上海","广州"};
- pageContext.setAttribute("city",cities);
- %>
- <body>
- <h1>遍历数组</h1><br>
- <c:forEach items="${city }" var="c"> ${c}<br></c:forEach><br>
- <h1>遍历集合</h1><br>
- <%
- List list = new ArrayList();
- list.add("中国");
- list.add("美国");
- list.add("俄罗斯");
- pageContext.setAttribute("list", list);
- %>
- <c:forEach items="${list}" var="c">${c}<br></c:forEach>
- <h1>遍历map</h1><br>
- <%
- Map map = new LinkedHashMap();
- map.put("name","力宏");
- map.put("age", 19);
- map.put("wife", "呵呵");
- pageContext.setAttribute("map", map);
- %>
- <c:forEach items="${map }" var="entry">
- ${entry.key }:${entry.value }<br>
- </c:forEach>
- <h1>循环执行指定的内容若干次</h1><br>
- <c:forEach begin="1" end="20" step="1" var="i" varStatus="stat">
- <c:if test="${stat.index%2 == 0 }">
- <font color="red"> ${i }</font> </c:if>
- <c:if test="${stat.index%2!= 0 }">
- <font color="blue">${i}</font>
- </c:if>
- </c:forEach>
- <h1>c:forTokens切割字符串</h1><br>
- <c:forTokens items="www.aqioo.com" delims="." var="c">
- ${c }<br>
- </c:forTokens>
- </body>
- </html>
效果图:
代码:cif
- <%@ page language="java" import="java.util.*" 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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'cif.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <%int day = 4;
- pageContext.setAttribute("day",day);
- %>
- <body>
- <c:if test="${ 2>1}">就是这样的</c:if> <br>
- <c:choose>
- <c:when test="${day==3 }">星期三</c:when>
- <c:otherwise>
- 不是星期三
- </c:otherwise>
- </c:choose>
- </body>
- </html>
代码:credirect
- <%@ page language="java" import="java.util.*" 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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'credirect.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <h1>credirect练习</h1> <br>
- <c:redirect url="/jstl/cforeach.jsp" context="${pageContext.request.contextPath }">
- <c:param name="aa" value="nihao"></c:param>
- </c:redirect>
- </body>
- </html>
效果:直接跳转到cforeach.jsp中去了
代码:curl 当浏览器端禁用cookie后,右键查看源代码就能看到url后跟一个JessesionId
- <%@ page language="java" import="java.util.*" 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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'curl.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <%
- request.getSession();
- String url = response.encodeUrl(request.getContextPath()+"/jstl/cforeach.jsp");
- pageContext.setAttribute("url", url);
- %>
- <body>
- <h1>url重写练习</h1> <br>
- <a href="${url }">这个是用Java代码实现的url重写</a>
- <c:url context="${pageContext.request.contextPath }" value="/jstl/cforeach.jsp" scope="page" var="url1"></c:url>
- <a href="${url1 }">这个是用c:url实现的url重写
- </a>
- </body>
- </html>
传智播客JavaWeb day06-jstl的更多相关文章
- 传智播客JavaWeb day01 快捷键、XML
2015-01-14 一直计划着学习java,今天晚上终于下定决心看了下传智播客朴乾老师的javaweb开发视频day01之第一讲,主要内容是开发工具简单介绍.怎么创建工程.Junit的介绍,我是C# ...
- 传智播客JavaWeb听课总结
一. JavaWeb基础 第一天: 1.Eclipse详解: (1).Bad versionnumber in .class file:编译器版本和运行(JRE)版本不符合.高的JRE版本兼容低版本的 ...
- 传智播客JavaWeb day07、day08-自定义标签(传统标签和简单标签)、mvc设计模式、用户注册登录注销
第七天的课程主要是讲了自定义标签.简单介绍了mvc设计模式.然后做了案例 1. 自定义标签 1.1 为什么要有自定义标签 前面所说的EL.JSTL等技术都是为了提高jsp的可读性.可维护性.方便性而取 ...
- 传智播客JavaWeb day02笔记
2015年1月21日 今天的主要内容:介绍了几款常用Javaweb服务器,重点介绍了tomcat以及tomcat的安装和怎么样检测安装成功 1.JavaWeb常见服务器 Tomcat(免费但是只支持部 ...
- 传智播客JavaWeb day09-mysql入门、数据库操作、数据库表操作、数据行操作
不知不觉已到了第九天了,今天主要讲了关系数据库的基本概述.安装.数据库.表和数据行的操作 1. 基本概述 1.1 数据库就是用来存储数据的.早期是存在文件里面的操作起来效率低而且不是很安全. 1.2 ...
- 传智播客JavaWeb day05-session、url重写
1.session是什么 1.1 session是一种会话技术 ps:还有一种是cookie 2.session的作用 2.1 服务器端会话范围内的数据共享 3.session的生命周期 3.1何时 ...
- 传智播客JavaWeb day11--事务的概念、事务的ACID、数据库锁机制、
1. 什么叫做事务? 2.默认情况下每一条sql语句都是一个事务,然后自动提交事务 ps:如果想多条语句占一个事务,则可以手动设置SetAutoCommit为false 3.关键字 start tr ...
- 传智播客JavaWeb day10-jdbc操作mysql、连接数据库六大步骤
第十天主要讲了jdbc操作mysql数据库,包括连接数据库六大步骤(注册数据库驱动.获得连接对象connetion.生成传输器stament.执行查询获得ResultSet.遍历结果集.关闭资源).介 ...
- 传智播客JavaWeb day03
ServletContext 这堂课主要讲ServletContext这个web域(可以看得见范围的)对象,web在启动的时候会创建唯一的ServletContext域对象. 作用:1.用来web域共 ...
随机推荐
- eclipse和myeclipse一键取消所有断点
- iphone 3gs 美版,6.1.3+降基带+越狱+解锁。成功分享(转)
本人参照这个帖子成功把一个白苹果的机器救活了 2014年1月26日 13点 转自:http://bbs.app111.com/thread-510632-1-1.html 时间:2013年5月31日 ...
- CentOS 7 最小化安装的网络配置
默认的最小化安装CentOS 7系统以后,是没有ipconfig这个命令的,依赖于net-tools工具包. 一.nmtui 这是一个类似于图形化的命令(和setup类似) 通过这个组件窗口可以设置各 ...
- python操作数据库产生中文乱码问题【已解决】
记:最近在使用python进行学生成绩管理系统设计时,遇到了一个中文显示的问题,这个问题困扰了一个上午,查阅了有关资料,锁定了原因——编码问题.最终更改编码设置,问题得到了解决. 具体做法: 1 Py ...
- solr 6.1 服务端 tomcat 搭建及调用
一.下载 apache solr6.1.0 最新版本zip,解压缩生成一个solr6.1.0文件夹 二.安装 1.在d:/projects下新建一个solr 2.把solr6.1.0/server/s ...
- GIT ON WINDOWS
https://help.github.com/articles/generating-an-ssh-key/
- CSS布局:两列,左边宽度自适应,右边宽度固定200px
<div id="box1"> <div id="left1"></div> <div id="right1 ...
- 转linq中的Single()、First()、Take(1) LINQ 标准的查询操作符 排序 orderby、thenby、Take
Single():操作一个集合,同时强要求只有一个对象匹配,并返回这一个. First():操作一个集合,可以有多个对象匹配,但是只返回第一个. Take(1):操作一个集合,可以有对个对象匹配,单只 ...
- class && struct
http://blog.csdn.net/yuliu0552/article/details/6717915 struct可以包含成员函数,可以继承,可以实现多态. struct为数据结构,class ...
- java 多线程—— 线程等待与唤醒
java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...