JSP标签介绍(02)

简介: JSP标签介绍(02)

1.JSP标签介绍

1.1 指令标签

指令标签用于通知JSP引擎如何处理JSP页面或应包含哪些资源。通常包含page、include、taglib等标签。

1.2 声明标签

声明标签用于定义全局变量或方法。

1.3 表达式标签

表达式标签用于在JSP页面中嵌入表达式。

<%= %>标签可用于输出表达式的值。

1.4 注释标签

注释标签用于对JSP页面的部分进行注释说明。
<%-- --%>标签可以用于多行注释。

1.5 动作标签

动作标签用于JSP页面中的业务处理。

  • jsp:forward标签用于将请求转发到其他页面。
  • jsp:include标签用于在当前页面中包含其他页面。
  • jsp:useBean标签用于在JSP页面中创建JavaBean对象。
  • jsp:setProperty和jsp:getProperty标签分别用于设置和获取JavaBean对象的属性。
  • jsp:scriptlet标签用于在JSP页面中嵌入Java代码。
  • jsp:expression标签用于在JSP页面中嵌入 Java 表达式。

除了以上标签,JSP还支持自定义标签,开发者可以基于Java类和API的扩展,创建自己的标签库。自定义标签的好处是可以达到更好的模块化和抽象化,提高代码重用性和可维护性。同时,JSP也支持标签文件,可以把标签的定义和标签处理逻辑进行分离和聚合。

2.JSP标签语言特点

JSP标签语言的特点如下:

2.1 形式:

<开始标签 属性=“属性值”>标签体</结束标签>

2.2 灵活性

JSP标签语言能够让开发者使用基本的HTML标签和表达式标签,同时也可以灵活地添加其它标签,以满足各种程序的需要。

2.3 代码可重用性

JSP标签语言支持自定义标签,可以将复杂的业务逻辑封装到标签中,实现代码的重用性。

2.4 可维护性

JSP标签语言的模块化结构便于维护和扩展,标签的定义和标签处理逻辑进行分离和聚合,更容易复用和修改,提高了代码的可维护性。

2.5 交互性强

JSP标签语言内置动作标签,可以方便地与服务器进行交互,实现了服务器与客户端之间的数据交互。

动态性:JSP标签语言的表达式标签和动态脚本语言可以让开发者根据需要在页面中动态地生成html代码。这样可以根据页面的不同动态生成不同的HTML代码,给大家带来更好的用户体验。

3.foreach标签的底层使用

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
  <description>JSTL 1.1 core library</description>
  <display-name>JSTL core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>y</short-name>
  <uri>http://java.yuan2.tld</uri>
  <validator>
    <description>
        Provides core validation features for JSTL tags.
    </description>
    <validator-class>
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    </validator-class>
  </validator>
     <tag>
    <name>forEach</name>
    <tag-class>com.yuan.jsp2.ForeEachTag</tag-class>
    <body-content>JSP</body-content>
    <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>dept</name>
    <tag-class>com.yuan.jsp2.DeptTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
     <tag>
    <name>select</name>
    <tag-class>com.yuan.jsp2.SelectTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>optionValue</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>optionText</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>selected</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
</taglib>
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34
• 35
• 36
• 37
• 38
• 39
• 40
• 41
• 42
• 43
• 44
• 45
• 46
• 47
• 48
• 49
• 50
• 51
• 52
• 53
• 54
• 55
• 56
• 57
• 58
• 59
• 60
• 61
• 62
• 63
• 64
• 65
• 66
• 67
• 68
• 69
• 70
• 71
• 72
• 73
• 74
• 75
• 76
• 77
• 78
• 79
• 80
• 81
• 82

JSP页面

<%@ page import="java.util.List" %>
<%@ page import="com.yuan.jsp2.Dept" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%@ taglib uri="http://java.yuan2.tld" prefix="y" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<% 
List<Dept> list = new ArrayList();
list.add(new Dept(1,"张三"));
list.add(new Dept(2,"李四"));
list.add(new Dept(3,"王五"));
request.setAttribute("list", list);
%>
<body>
<y:forEach items="${list }" var="lis">
${lis.id }:${lis.name }
</y:forEach> 
</body>
</html>
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
package com.yuan.jsp2;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ForeEachTag extends BodyTagSupport {
  private List<Object> items = new ArrayList<Object>();
  private String var;
  public ForeEachTag() {
    // TODO Auto-generated constructor stub
  }
  public ForeEachTag(List<Object> items, String var) {
    super();
    this.items = items;
    this.var = var;
  }
  public List<Object> getItems() {
    return items;
  }
  public void setItems(List<Object> items) {
    this.items = items;
  }
  public String getVar() {
    return var;
  }
  public void setVar(String var) {
    this.var = var;
  }
  @Override
  public int doStartTag() throws JspException {
    if (items == null || items.size() == 0)
      return SKIP_BODY;
    // 遍历第一次
    Iterator<Object> it = items.iterator();
    pageContext.setAttribute(var, it.next());
    pageContext.setAttribute("it", it);
    return EVAL_BODY_INCLUDE;
  }
  @Override
  public int doAfterBody() throws JspException {
    Iterator<Object> it = (Iterator<Object>) pageContext.getAttribute("it");
    if (it.hasNext()) {
      // 取得的二条 以及第二条之后的
      pageContext.setAttribute(var, it.next());
      pageContext.setAttribute("it", it);
      return EVAL_BODY_AGAIN;
    }
    return SKIP_BODY;
  }
}
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34
• 35
• 36
• 37
• 38
• 39
• 40
• 41
• 42
• 43
• 44
• 45
• 46
• 47
• 48
• 49
• 50
• 51
• 52
• 53
• 54
• 55
• 56
• 57
• 58
• 59
• 60
• 61
• 62
• 63
• 64
• 65
• 66
• 67

网页输出:

4.select标签的底层使用

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
  <description>JSTL 1.1 core library</description>
  <display-name>JSTL core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>y</short-name>
  <uri>http://java.yuan2.tld</uri>
  <validator>
    <description>
        Provides core validation features for JSTL tags.
    </description>
    <validator-class>
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    </validator-class>
  </validator>
     <tag>
    <name>forEach</name>
    <tag-class>com.yuan.jsp2.ForeEachTag</tag-class>
    <body-content>JSP</body-content>
    <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>dept</name>
    <tag-class>com.yuan.jsp2.DeptTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
     <tag>
    <name>select</name>
    <tag-class>com.yuan.jsp2.SelectTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>optionValue</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>optionText</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>selected</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
</taglib>
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34
• 35
• 36
• 37
• 38
• 39
• 40
• 41
• 42
• 43
• 44
• 45
• 46
• 47
• 48
• 49
• 50
• 51
• 52
• 53
• 54
• 55
• 56
• 57
• 58
• 59
• 60
• 61
• 62
• 63
• 64
• 65
• 66
• 67
• 68
• 69
• 70
• 71
• 72
• 73
• 74
• 75
• 76
• 77
• 78
• 79
• 80
• 81
• 82
<%@ page import="java.util.List" %>
<%@ page import="com.yuan.jsp2.Dept" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%@ taglib uri="http://java.yuan2.tld" prefix="y" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<% 
List<Dept> list = new ArrayList();
list.add(new Dept(1,"张三"));
list.add(new Dept(2,"李四"));
list.add(new Dept(3,"王五"));
request.setAttribute("list", list);
%>
<body>
<y:select items="${list }" optionText="name" optionValue="id" selected="2"></y:select>
</body>
</html>
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
package com.yuan.jsp2;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SelectTag extends BodyTagSupport{
private List<Object> items = new ArrayList();
private String optionValue;
private String optionText;
private String selected;
public SelectTag() {
  // TODO Auto-generated constructor stub
}
public List<Object> getitems() {
  return items;
}
public void setitems(List<Object> items) {
  this.items = items;
}
public String getoptionValue() {
  return optionValue;
}
public void setoptionValue(String optionValue) {
  this.optionValue = optionValue;
}
public String getOptionText() {
  return optionText;
}
public void setOptionText(String optionText) {
  this.optionText = optionText;
}
public String getSelected() {
  return selected;
}
public void setSelected(String selected) {
  this.selected = selected;
}
@Override
public String toString() {
  return "SelectTag [items=" + items + ", optionValue=" + optionValue + ", optionText=" + optionText + ", selected="
      + selected + "]";
}
@Override
  public int doStartTag() throws JspException {
  JspWriter out = pageContext.getOut();
  try {
    out.print(toHTML());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
  }
/**
 * 字符串拼接
 * @return
 * @throws Exception 
 */
private String toHTML() throws Exception {
  StringBuffer sb = new StringBuffer("<select>");
  for (Object object : items) {
    String val = getObjAttrValue(object,optionValue);
    String text = getObjAttrValue(object,optionText);
    sb.append("<option "+(val.equals(selected)?"selected":"")+" value = '"+val+"'>"+text+"</option>");
  }
  sb.append("</select>");
  return sb.toString();
}
/**
 * 获得某个对象的某个属性值
 * @param object
 * @param optionValue2
 * @return
 * @throws Exception 
 * @throws NoSuchFieldException 
 */
private String getObjAttrValue(Object object, String attr) throws Exception {
  Class class1 = object.getClass();
  Field field = class1.getDeclaredField(attr);
  field.setAccessible(true);
  String string = field.get(object).toString(); 
  return string;
}
}
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34
• 35
• 36
• 37
• 38
• 39
• 40
• 41
• 42
• 43
• 44
• 45
• 46
• 47
• 48
• 49
• 50
• 51
• 52
• 53
• 54
• 55
• 56
• 57
• 58
• 59
• 60
• 61
• 62
• 63
• 64
• 65
• 66
• 67
• 68
• 69
• 70
• 71
• 72
• 73
• 74
• 75
• 76
• 77
• 78
• 79
• 80
• 81
• 82
• 83
• 84
• 85
• 86
• 87
• 88
• 89
• 90
• 91
• 92
• 93
• 94
• 95
• 96
• 97
• 98
• 99
• 100
• 101
• 102
• 103
• 104
• 105
• 106
• 107
• 108
• 109
• 110
• 111
• 112
• 113
• 114
• 115
• 116

5.总结

JSP标签是JSP中的一种扩展功能,可以帮助用户将动态数据插入到JSP页面中。JSP标签分为内置标签和自定义标签两种类型。

内置标签是JSP提供的标准标签库,包括核心标签库、格式化标签库、SQL标签库、XML标签库等。它们提供了丰富的功能,如控制页面流程、格式化数据、操作数据库和生成XML文档等。

自定义标签是用户自己定义的标签,可以根据实际需要实现特定的功能。自定义标签分为标签文件和标签类两种形式。标签文件是一种基于XML的标记语言,类似于HTML,可以通过标签文件定义标签的属性、主体内容和处理逻辑。标签类则是一种Java类,实现了标签处理器的接口,并提供了标签的执行逻辑,可以通过Java代码编写标签的执行过程。

JSP标签可以帮助用户解决复杂的业务需求,并提高Web应用程序的灵活性和可维护性。使用内置标签可以方便地完成通用的操作,而自定义标签可以将业务逻辑集成到标签中,实现更高层次的抽象和封装。


相关文章
|
6月前
J2EE&JSP标签02&Foreach标签&select
J2EE&JSP标签02&Foreach标签&select
|
2月前
|
Java
jsp页面中使用jstl标签报错:javax.servlet.jsp.JspTagException
jsp页面中使用jstl标签报错:javax.servlet.jsp.JspTagException
13 0
|
4月前
|
Java
jsp标签下
jsp标签下
27 0
|
4月前
|
XML Java 数据格式
jsp标签上
jsp标签上
26 0
|
4月前
|
Java
JSP标签(2) -----自定义foreach ,select标签,全网最详细,最完整易懂
JSP标签(2) -----自定义foreach ,select标签,全网最详细,最完整易懂
|
4月前
|
Java API
JSP标签 01 完整详细
JSP标签 01 完整详细
|
4月前
|
Java
JAVA2EE-----jsp标签(02)
JAVA2EE-----jsp标签(02)
26 0
|
4月前
|
Java
JSP标签(01)
JSP标签(01)
28 0
|
4月前
|
Java 数据安全/隐私保护
Shiro - JSP页面标签应用
Shiro - JSP页面标签应用
29 0
|
5月前
|
XML Java 程序员
自定义JSP标签
自定义JSP标签

相关课程

更多