目录
一,什么是JSP🍑
二,什么是JSTL(JSTL标签库)🍓
三,如何使用JSTL🍅
在项目中如何使用JSTL标签?在发开中使用JSTL标签库需要执行如下两个步骤。
(1)在工程中引用JSTL的两个jar文件和标签库描述符文件(扩展名为 .tld)。
(2)在需要使用JSTL的JSP页面中使用taglib指令导入标签库描述文件。例如,要使用JSTL核心标签库,需要在JSP页面的上方增加如下的taglib命令。
四,为何要使用JSTL🍎
JSTL标签的生命周期
自定义标签
1、自定义标签类
2、编写标签库描述文件(tld)
3、在JSP上使用自定义的标签(测试)
总结🍊
一,什么是JSP🍑
- JSP(全称JavaServer Pages),JSP将Java代码和特定变动内容嵌入到静态的页面中,实现以静态页面为模板,动态生成其中的部分内容。JSP文件在运行时会被其编译器转换成更原始的Servlet代码。JSP编译器可以把JSP文件编译成用Java代码写的Servlet,然后再由Java编译器来编译成能快速执行的二进制机器码,也可以直接编译成二进制码。
- 简单来说:JSP是一个模版引擎,它简化了开发,封装了servlet响应html标签的繁琐代码。JSP技术以Java语言作为脚本语言,为用户的HTTP请求提供服务,并能与服务器上的其它Java程序共同处理复杂的业务需求。
JSP就是一种特殊的servlet.
二,什么是JSTL(JSTL标签库)🍓
JSTL的全称是Java Server Pages Standard Tag Library ,即JSP标准标签库。它包含了在开发JSP页面时经常用到的一组标准标签,这些标签提供了一种不用嵌入Java代码就可以开发复杂的JSP页面的途径。JSTL标签库包含了多种标签,如通用标签,条件判断标签和迭代标签等。
标签的概念👇
- 是标记语言(Mark Language),是一种注释文本的语言,以便于计算机可以操作。很多与“ML”结尾的语言都是标记语言,比如:HTML,XML,XHTML,VML等等
- 标记语言与其他语言一样,也需要运行它们的环境,比如HTML的运行环境时浏览器,XML也要自己的解析和运行的环境
标签类型👇
- UI标签, 输出页面元素
- 控制标签, 如if标签,foreach标签等
- 数据标签,用于向页面输入数据
基本结构👇
- <开始标签>标签体</结束标签>
空标签(没有标签体的标签)👇
- <开始标签 属性名="属性值"/></结束标签>
- <br/><br/>
- <开始标签 属性名="属性值"/>
jstl标签库的概念👇
是一个JSP标签集合,它封装了JSP应用的通用核心功能, 基于JSP标签我们可以理解为,是JSP应该通用功能的一种封装方式
三,如何使用JSTL🍅
在项目中如何使用JSTL标签?在发开中使用JSTL标签库需要执行如下两个步骤。
(1)在工程中引用JSTL的两个jar文件和标签库描述符文件(扩展名为 .tld)。
正如使用JDBC连接数据库那样,使用JSTL定义的标签库也必须在工程中导入两个jar文件:jstl.jar和standard.jar。除此之外,标签类库描述符文件也是必须的,这些资源都能在网上下载得到。
在MyEclipse中已经集成了JSTL,因此这一个步骤可以由工具实现。
(2)在需要使用JSTL的JSP页面中使用taglib指令导入标签库描述文件。例如,要使用JSTL核心标签库,需要在JSP页面的上方增加如下的taglib命令。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
taglib 指令通过uti属性引用某个标签库的配置文件,JSP页面中通过prefix属性指定的前缀即可访问该标签库中的某个标签功能,语法如<C:标签名>
完成以上的两个步骤后,就可以用JSTL方便开发JSP页面,而无须嵌入Java代码了。
四,为何要使用JSTL🍎
我们通过使用EL表达式很容易地输出了用户名信息,从而在一定程度上简化了JSP页面开发的复杂度。但是由于EL表达式不能实现逻辑控制,遍历循环等功能,如果在开发JSP页面时遇到这样的需求,除了编写Java脚本,还能如何解决?答案就是使用JSTL标签。
JSTL标签的生命周期

自定义标签
1、自定义标签类
- out 标签类(继承BodyTagSupport)
/**
* out 标签作用:向JSP页面中写入数据
*/
public class OutTag extends BodyTagSupport{ private String val;private String defaultVal;public void setVal(String val) {this.val = val;}//为空时设置默认值的方法public void setDefaultVal(String defaultVal) {this.defaultVal = defaultVal;}//标签的开始方法(需要给自定义的标签添加什么操作,就重写对应的生命周期中的方法)@Overridepublic int doStartTag() throws JspException {//this.pageContext:通过当前类获取pageContext对象//pageContext对象中有一个getOut写出方法JspWriter out = this.pageContext.getOut(); try {if(val==null&&val.equals("")) {out.println(this.defaultVal);} else {out.println(this.val);}} catch (Exception e) {e.printStackTrace();} //跳过标签体,out定义的是空标签return SKIP_BODY;}
}/**
* out 标签作用:向JSP页面中写入数据
*/
public class OutTag extends BodyTagSupport{ private String val;private String defaultVal;public void setVal(String val) {this.val = val;}//为空时设置默认值的方法public void setDefaultVal(String defaultVal) {this.defaultVal = defaultVal;}//标签的开始方法(需要给自定义的标签添加什么操作,就重写对应的生命周期中的方法)@Overridepublic int doStartTag() throws JspException {//this.pageContext:通过当前类获取pageContext对象//pageContext对象中有一个getOut写出方法JspWriter out = this.pageContext.getOut(); try {if(val==null&&val.equals("")) {out.println(this.defaultVal);} else {out.println(this.val);}} catch (Exception e) {e.printStackTrace();} //跳过标签体,out定义的是空标签return SKIP_BODY;}
}
- if 标签类(继承BodyTagSupport)
@SuppressWarnings("serial")
public class IfTag extends BodyTagSupport{private boolean test;public boolean isTest() {return test;}public void setTest(boolean test) {this.test = test;} @Overridepublic int doStartTag() { if(this.isTest()) {//判断标签体时return EVAL_BODY_INCLUDE;} return SKIP_BODY; }
}
- foreach标签类
public class ForeachTag extends BodyTagSupport {private List<?> items;private String var;public void setItems(List<?> items) {this.items = items;}public void setVar(String var) {this.var = var;} @Overridepublic int doStartTag() { if(Objects.isNull(this.items) || this.items.size() <= 0) {return SKIP_BODY;}Iterator<?> it = this.items.iterator();Object next = it.next();this.pageContext.setAttribute(var, next);this.pageContext.setAttribute("iterator", it);return EVAL_BODY_INCLUDE;}@Overridepublic int doAfterBody() {Iterator<?> it= (Iterator<?>)this.pageContext.getAttribute("iterator");if(it.hasNext()) {this.pageContext.setAttribute(var, it.next());return EVAL_BODY_AGAIN;}return SKIP_BODY;}
}
- select标签类
public class SelectTag extends BodyTagSupport { private String id; private String name;private List<?> items;private String cssClass;private String cssStyle;private String value;private String text;private String selectValue;public void setText(String text) {this.text = text;}public void setValue(String value) {this.value = value;}public void setSelectValue(String selectValue) {this.selectValue = selectValue;}public void setId(String id) {this.id = id;}public void setName(String name) {this.name = name;}public void setItems(List<?> items) {this.items = items;}public void setCssClass(String cssClass) {this.cssClass = cssClass;}public void setCssStyle(String cssStyle) {this.cssStyle = cssStyle;} @Overridepublic int doStartTag() { JspWriter out = this.pageContext.getOut();try {String html = getSelectHtml();out.print(html);} catch (Exception e) {e.printStackTrace();} return SKIP_BODY;}//通过注入的属性值,来构造select元素的字符串private String getSelectHtml() throws Exception {StringBuilder sb = new StringBuilder(); //构造页面需要的select元素sb.append("<select id='"+id+ "' name='"+name+"'");if(!StringUtils.isNullOrEmpty(this.cssClass)) {sb.append(" class='"+this.cssClass+"'");}if(!StringUtils.isNullOrEmpty(this.cssStyle)) {sb.append(" style='"+this.cssStyle+"'");}sb.append(">"); //循环生成optionsfor(Object option: this.items) {String val = BeanUtils.getProperty(option, this.value);String txt = BeanUtils.getProperty(option, this.text);//value="id" text="name" if(val.equals(this.selectValue)) {sb.append("<option value='"+val+"' selected>" + txt + "</option>");} else {sb.append("<option value='"+val+"' >" + txt + "</option>");}}sb.append("</select>");return sb.toString();}
}
2、编写标签库描述文件(tld)
将你定义的标签具备相应的格式规范编辑在 tld 文件中
<!DOCTYPE taglibPUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN""http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 标签库描述符 -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><short-name>Simple Tags</short-name><uri>/zking</uri><tag><name>out</name><tag-class>com.myjstl.OutTag</tag-class><body-content>empty</body-content><attribute><name>val</name><required>true</required><rtexprvalue>true</rtexprvalue><description>out标签val属性,用于输出val的值</description></attribute><attribute><name>defaultVal</name><required>false</required><rtexprvalue>false</rtexprvalue><description>用于定义默认值</description></attribute></tag><tag><name>if</name><tag-class>com.myjstl.IfTag</tag-class><body-content>jsp</body-content><attribute><name>test</name><required>true</required><rtexprvalue>true</rtexprvalue><description>if标签</description></attribute></tag><tag><name>foreach</name><tag-class>com.zking.mymvc.tag.ForeachTag</tag-class><body-content>jsp</body-content><description>foreach标签</description><attribute><name>items</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>var</name><required>true</required><rtexprvalue>false</rtexprvalue></attribute></tag><tag><name>select</name><tag-class>com.zking.mymvc.tag.SelectTag</tag-class><body-content>empty</body-content><attribute><name>id</name><required>true</required><rtexprvalue>false</rtexprvalue></attribute><attribute><name>name</name><required>true</required><rtexprvalue>false</rtexprvalue></attribute><attribute><name>items</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>cssClass</name><required>false</required><rtexprvalue>false</rtexprvalue></attribute><attribute><name>cssStyle</name><required>false</required><rtexprvalue>false</rtexprvalue></attribute><attribute><name>value</name><required>true</required><rtexprvalue>false</rtexprvalue></attribute><attribute><name>text</name><required>true</required><rtexprvalue>false</rtexprvalue></attribute><attribute><name>selectValue</name><required>false</required><rtexprvalue>false</rtexprvalue></attribute></tag></taglib>
👆详解
- <tag></tag>:里面放一个自定义标签的所有格式规范
- <name>out</name>:表示自定义的标签名
- <tag-class>com.zking.mymvc.tag.OutTag</tag-class>:表示第一步中自定义的标签类的完整类名(这里是通过反射机制找到自定义的标签类的)
- <body-content>empty</body-content>:代表没有标签体,填jsp就代表有
<attribute>:代表自定义的标签类中的属性规范 <name>val</name>:自定义的属性名字
<required>true</required>:自定义的标签中必须写出的属性
<rtexprvalue>true</rtexprvalue>:属性值是否可以使用EL表达式
<description>out标签val属性,用于输出val的值</description>:自定义属性的描述
</attribute>
3、在JSP上使用自定义的标签(测试)
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="/zking" %>
<!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><%request.setAttribute("name", null);%><!-- out 标签 --><z:out val="${name}" defaultVal="你输入了null关键字"/><z:if test="${100 == 100}">测试if(100 == 100)</z:if><z:if test="${100 != 200}">测试if(100 != 200)</z:if>
</body>
</html>
总结🍊
今天的分享就到此为止啦,精彩下期继续哦!












![出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]](https://img-blog.csdnimg.cn/5f89ade4ae604d9083fedac37d1d3155.png)



