JSTL 是 Apache 对 EL 表达式的扩展,依赖 EL 。JSTL 是标签语言。JSP 在 2.0 以后开始放弃 java 脚本,EL 表达式代替
解决了输出的问题。而 JSTL 标签将要代替
- <%=...%>
解决另外的 Java 脚本问题。
- <% ...%>
同 EL 函数库的导入相似,如果使用 MyEclipse 开发工具,则不需要人为导入 jar 包,因为项目发布到 Tomcat 时,MyEclipse 会在 lib 目录下存放 jstl 的 jar 包。我们只需要在 JSP 页面中使用 taglib 指令导入标签库即可。 jstl 一共有四个标签库,分别是:
,所以又叫做 c 标签库
- c
,所以又叫做 c 标签库
- fmt
以导入 core 核心标签库为列:
- <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
: 输出
- <c:out>
:设置(可以创建域的属性)
- <c:set>
- "code"value = "<script>alert('hello');</script>"scope = "request" / > // 在request域中添加name为a,值为弹窗hello的数据
- "${code }" / > //全域查找,输出nme为a的值。 并且把<script>标签给转义了。所有直接输出字符串,而不是弹窗
:删除域变量
- <c:remove>
:指定路径
- <c:url>
=
- <c:url value="/index.jsp">
=/ 项目名字 / index.jsp。 所以超链接或者表单中的路径,可以使用前两种都可以。
- ${pageContext.request.contextpath}/index.jsp
- "<c:url value='/index.jsp'/>">点击这里回到主页jsp'>"name"value="张三"/>//加了参数,如果参数包含中文,则自动URL编码 ,输出:/项目名/index.jsp?username=A%E%G%D%G%D%G%D%
:对应 java 中的 if 语句
- <c:if>
- iftest="${empty param.name }">//参数的name不为空输出helloif>
:对应 Java 中的 if/else
- <c:choose>
- "">..."">..."">...... ...
- 等同与if(...) {
- }else if( ....) {
- }else if( ....) {
- }else if( ....) {
- } ...else{ ...}
:用来循环遍历数组、集合。可以用计数方式来循环
- <c:forEach>
用来输出数组、集合
- "i"begin = "1"end = "10"step = "2" > //for循环
- $ {
- i
- }
循环状态
- <%
- String[] strs = {"one","two"};
- request.setAttribute("strs", strs);
- %>"${strs }"var="str">//输出数组和集合${str }
```
fmt 标签库是用来格式化输出的,通常需要格式化的有时间和数字
- <fmt : formatDate value="" pattern="" />
- <%
- Date date =newDate();
- request.setAttribute("date", date);
- %>"${requestScope.date }"pattern="yyyy-MM-dd HH:mm:ss"/>// 按照给定的格式输出时间
- <%
- request.setAttribute("num1",3.1415926);
- %>"${requestScope.num1 }"pattern="0.000"/>//按照0.000保留小数点后面的位数,四舍五入,不足补0"${requestScope.num1 }"pattern="#.###"/>//按照#.###保留小数点后面的位数,四舍五入,不足不补0
- void doTag()//每次执行标签时都会调用这个房Called by the container to invokethistag.
- JspTaggetParent()//返回父标签Returns the parent ofthistag,forcollaboration purposes.void setJspBody(JspFragment jspBody)//设置 标签体 Provides the body ofthistag as a JspFragment object, able to be invoked zero or more times by the tag handler.void setJspContext(JspContext pc)//设置jsp上下文对象,儿子就是PageContext,一般都是用pageContextCalled by the container to providethistag handler with the JspContextfor thisinvocation.void setParent(JspTag parent)
- Sets the parent ofthistag,forcollaboration purposes.
标签处理类的这些方法都是由 Tomcat 调用:过程如下;
- public class MyTag1 implements SimpleTag {
- private PageContext pageContext;
- private JspFragment body;
- /**
- * 所有的setXxx()方法都会在doTag()方法之前被tomcat调用!
- * 所在doTag()中就可以使用tomcat传递过来的对象了。
- */
- public void doTag() throws JspException,
- IOException {
- pageContext.getOut().print("Hello Tag!");
- }
- public JspTag getParent() {
- return null;
- }
- public void setJspBody(JspFragment body) {
- this.body = body;
- }
- public void setJspContext(JspContext context) {
- this.pageContext = (PageContext) context;
- }
- public void setParent(JspTag arg0) {}
- }
- void doTag()
- Default processing of the tag does nothing.staticJspTagfindAncestorWithClass(JspTag from, Class klass)
- Find the instance of a givenclasstype that is closest to a given instance.protectedJspFragmentgetJspBody()
- Returns the body passed in by the container via setJspBody.protectedJspContextgetJspContext()
- Returns the page context passed in by the container via setJspContext.
- JspTaggetParent()
- Returns the parent ofthistag,forcollaboration purposes.void setJspBody(JspFragment jspBody)
- Stores the provided JspFragment.void setJspContext(JspContext pc)
- Stores the provided JSP context in theprivatejspContext field.void setParent(JspTag parent)
- Sets the parent ofthistag,forcollaboration purposes.
标签体的内容有如下四种;| 内容 | 说明 || :---| :---||empty | 无标签体(常用)||scriptless | 可以是 EL 表达式或者其他的标签或者字符串(常用)||JSP|(不使用)||tagdependent|(不使用)|
- 指定当前标签的名称
- class>class> 指定当前标签的标签处理类!
- 指定标签体的类型
来导入 tld 文件
- <%@taglib %>
- <%@ taglib prefix= uri= %>
可以设置,如果执行这个标签,后面的标签都会不执行。实现这一功能的方法是在标签处理类中的 doTag() 方法中使用 SkippageException 来结束! Tomcat 会调用标签处理类的 doTag() 方法,然后 Tomcat 会得到 SkipPageException,它会跳过页面其他内容
- public void doTag() throws JspException,
- IOException {
- throw new SkipPageException(); //抛出这个异常后,在本标签后面的内容,将看不到!
- }
添加属性的步骤
- test 指定属性名
- true指定属性是否为必需的
- true指定属性是否可以使用EL
- /**
- * Class MyTag1
- * 继承SimpleTagSupport类,重写doTag()方法。没有标签体
- */
- package cn.kmust.tag;
- public class MyTag1 extends SimpleTagSupport {
- public void doTag() throws JspException,
- IOException {
- //因为这个SimpleTagSupport中早就为我们写好了出doTag()之外的其他方法,所以通过this.getXXX()即可获得其他方法的返回对象。
- this.getJspContext().getOut().print("Hello one !"); //通过getJspContext获得pageContext,然后getOut获得输出对象,通过print像页面输出 。
- }
- }
- /**
- * Class MyTag2
- * 有标签体
- */
- package cn.kmust.tag;
- public class MyTag3 extends SimpleTagSupport {
- public void doTag() throws JspException,
- IOException {
- Writer out = this.getJspContext().getOut(); //获取当前jsp页面的输出流
- this.getJspBody().invoke(out); //执行标签体内容,把结果写到指定的流中,即页面上。
- //需要说明的是,invoke()的参数可以写成null, 如果是null的话,表示使用的就是当前页面的out !this.getJspBody().invoke(null);
- }
- }
- /**
- * Class MyTag3
- * 抛出SkipPageException异常,如果执行这个标签,则后面的标签都不会再执行了
- */
- package cn.kmust.tag;
- public class MyTag3 extends SimpleTagSupport {
- public void doTag() throws JspException,
- IOException {
- this.getJspContext().getOut().print("只能看到我,下面什么都没有!");
- throw new SkipPageException(); //抛出这个异常后,在本标签后面的内容,将看不到!
- }
- }
- /**
- * Class MyTag4
- * 带有一个属性
- */
- package cn.kmust.tag;
- public class MyTag4 extends SimpleTagSupport {
- private boolean test; //定义这个属性
- /*
- * 这个方法会由tomcat来调用,并且在doTag()之前
- */
- public void setTest(boolean test) {
- this.test = test;
- }
- public void doTag() throws JspException,
- IOException {
- if (test) {
- /*
- * 执行标签体
- */
- this.getJspBody().invoke(null); //如果传递的输出流为null,表示使用的就是当前页面的out!
- }
- }
- }
/** * tld文件的配置 。 名字是kmust-tag.tld 。 位置: WEN-INF/tlds */"1.0"encoding="UTF-8">"http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version="2.1"> 1.0<short-name>kmust</short-name> http://www.kmust.cn/tags/it-1.0</uri> //这是uri是随便起的 myTag1 class>cn.kmust.tag.MyTag2class>//标签处理类的位置empty//没有标签体 myTag2 class>cn.kmust.tag.MyTag3class> scriptless//有标签体 myTag3 class>cn.kmust.tag.MyTag4class> empty myTag4 class>cn.kmust.tag.MyTag5class> scriptless//属性test true true
/** * jsp页面中利用自定义的标签 */<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="it"uri="/WEB-INF/tlds/kmust-tag.tld"%>//导入自定义的tld文件的路径"${empty param.xxx }">//test属性的值是 "参数" ,标签处理类中的定义是 :如果参数不为空,就执行标签体中的内容 //标签体中也是一个自定义的标签,这个标签的意思是 : 执行我,后面的标签就不执行了 //输出hello one! <% request.setAttribute("xxx","zhangSan"); %> ${xxx } //标签体中的这个EL表达式会到标签处理类中执行。 我是张三的大哥 //标签体中的内容也可以是字符串
404/error404.jsp500/error500.jspjava.lang.RuntimeException/error.jsp当出现404时,会跳转到error404.jsp页面; 当出现RuntimeException异常时,会跳转到error.jsp页面; 当出现非RuntimeException的异常时,会跳转到error500.jsp页面。
PageContext(page 域)、ServletRequest(request 域)、HttpSession(session 域)、ServletContext(application 域)。page 域、request 域、session 域、application 域这几个词表明的是域的范围。 。Sevlet 只能使用后三个域。JSP 能使用所用的域。 简单来说, 域对象简单说就是能在 Servlet 之间(page 域使用在 JSP 页面中)传递参数,因为要降低耦合度,所以我们创建的每个 Servlet 之间都是不能相互交流的,可以说,域对象是串联起多个 Servlet 的线,可以为多个 Servlet 之间的交流传递数据,这就显得比较重要。域对象内部有个 Map,用来存取数据。
所有的域对象都有如下的方法:
javavoid setAttribute(java.lang.Stringname, java.lang.Objecto)//保存值 Stores an attribute inthisrequest. java.lang. ObjectgetAttribute(java.lang.Stringname)//获取值 Returns the value of the named attribute as an Object, ornull ifno attribute of the given name exists.void removeAttribute(java.lang.Stringname)//移除值 Removes an attribute fromthisrequest.`
这四个域的范围不同:
需要注意的是,PageContext 能够代理其他三个域,即,PageContext 实现其他三个域的存取操作
JSP 的九大内置与上述的四大域对象可以说没有太大的关系。内置对象是 JSP 已经定义好的对象,这些对象中有的是 Servlet 中类的实例化对象,有点的 io 中类的实例化对象等等 。都是 jSp 中可能常用到的类 。 需要注意的是,pageContext 这个内置对象一个顶九个,可以获取其他八个内置对象。也就是说,一个 pageContext 内置对象就可以实现其他八个内置对象的功能 。
来源: http://www.cnblogs.com/zyuqiang/p/6850516.html