当前位置:网站首页>JSP tag
JSP tag
2022-07-24 06:27:00 【zsm030616】
Catalog
One , Language characteristics of labels
(1) form :< The start tag attribute =“ Property value ”> Tagging body </ End tag >
(2) classification
Empty label for example :br,hr
ui label for example :input,table
Control tags for example :if.foreach
Data labels for example :out label
Two , The development and use of custom labels
(1) step : Helper ( Inherit BodyTagSupport)
package com.zsm.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Helper Must inherit bodytagSupport
* @author zjjt
*/
public class DemoTag1 extends BodyTagSupport {
@Override
public int doStartTag() throws JspException {
System.out.println("=================================doStartTag=======================");
// skip
//return SKIP_BODY;
//return super.doStartTag();
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
System.out.println("=================================doAfterBody=======================");
return super.doAfterBody();
// loop
//return EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
System.out.println("=================================doEndTag=======================");
return super.doEndTag();
// Only read the previous content
//return SKIP_PAGE;
}
}
(2) Label library description file (tld)
Be careful :tld The file must be kept in WEB-INF Directory or its subdirectories
(3)jsp adopt taglib Instruction import label Library
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
(4) example : take c:if Label to z:if
1. The custom tag library is related to tld Document related , Also with tag Element is related to the corresponding helper class
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<z:if test="true">true</z:if>
<z:if test="false">false</z:if>
<z:set value="zsm" var="name"></z:set>
<z:out value="${name}"></z:out>
</body>
</html>
. 2. Tags in tag library and tld Medium Tag The elements are related to ( When there is no if Number of tags )
error 
3、 ... and , Tag life cycle

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<z:dome1>xxxx</z:dome1>
<z:if test="true">true</z:if>
<z:if test="false">false</z:if>
<z:set var="name" value="laoliu"></z:set>
<z:out value="${name }"></z:out>
</body>
</html>
package com.zsm.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Helper Must inherit bodytagSupport
* @author zjjt
*/
public class DemoTag1 extends BodyTagSupport {
@Override
public int doStartTag() throws JspException {
System.out.println("=================================doStartTag=======================");
// skip
//return SKIP_BODY;
//return super.doStartTag();
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
System.out.println("=================================doAfterBody=======================");
return super.doAfterBody();
// loop
//return EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
System.out.println("=================================doEndTag=======================");
return super.doEndTag();
// Only read the previous content
//return SKIP_PAGE;
}
}
Read only the front part
@Override
public int doEndTag() throws JspException {
System.out.println("=================================doEndTag=======================");
return super.doEndTag();
// Only read the previous content
//return SKIP_PAGE;
}
design sketch 
1. With label body , By default, the helper class will be called dostarttag.doafterbody,doendtag Method
@Override
public int doStartTag() throws JspException {
System.out.println("=================================doStartTag=======================");
return super.doStartTag();
}
2. If you will dostarttag The return value of is changed to skip_body, that doafterbody There will be no call to execute ( Line one )
Popular point :( Change the return value of the method to skip , In the case of a label body, it is also worth calling two methods )
@Override
public int doStartTag() throws JspException {
System.out.println("=================================doStartTag=======================");
// skip
return SKIP_BODY;
}
3. If you will dostarttag The return value of is changed to EVAL_BODY_INCLUDE, that doafterbody Will call to execute ( line 2)
@Override
public int doStartTag() throws JspException {
System.out.println("=================================doStartTag=======================");
return EVAL_BODY_INCLUDE;
}
4. If you will doafterbody The return value of is changed to EVAL_BODY_again, Then call consistently doafterbody, Into the loop ( Route 3)
@Override
public int doAfterBody() throws JspException {
System.out.println("=================================doAfterBody=======================");
return super.doAfterBody();
// loop
//return EVAL_BODY_AGAIN;
}
Four ,z:if label
Case study
package com.zsm.tag;
/**
* if label
* If the conditions are met , Just print the label body ---》dostatag The return value of eval_body_include
* If you don't do that , The label body is not output ---》dostarttag The return value of skip_body
* You need to obtain the value of whether the conditions are met , Then the attribute value of the tag boolean
*
* @author zjjt
*/
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ifTag extends BodyTagSupport {
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
// If the conditions are met , Just print the label body ---》dostatag The return value of eval_body_include
return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
}
}
5、 ... and ,z:set label z:out label
1.z:set Label helper class
Case study
package com.zsm.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Data labels : Store the data
*
* To store data, store it in the form of key value pairs : The tag has two properties
* @author zjjt
*/
public class SetTag extends BodyTagSupport {
private String var;
private Object value;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
// To store data, store it in the form of key value pairs : The tag has two properties
pageContext.setAttribute(var, value);
return super.doStartTag();
}
}
2.z:out Label helper class
servlet Background code foreground output content :out.print
Send the data to the front desk : First get the output stream
Case study
package com.zsm.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* servlet Background code foreground output content :out.print
* Send the data to the front desk : First get the output stream
* @author zjjt
*/
public class Outtag extends BodyTagSupport {
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
边栏推荐
- Solutions to the failure of wechat TBS online kernel installation
- Top 10 vulnerability assessment and penetration testing tools
- Machine learning & deep learning introduction information sharing summary
- 【226】wireshark的参数使用说明
- Understanding of Flink parallelism
- 【测试工具】
- Ia note 1
- Flink checkpoint configuration details
- Public access intranet IIS website server [no public IP required]
- 公网使用Microsoft Remote Desktop远程桌面,随时远程办公
猜你喜欢

Using keras to realize LSTM time series prediction based on attention mechanism

Do not rent servers, build your own personal business website (4)

IA课总结(2)

简单三步快速实现内网穿透

IP lesson summary (3)

Do not rent servers, build your own personal business website (3)

Openpose unity plug-in deployment tutorial
![[218] what are the advantages and disadvantages of CS architecture and BS architecture and data on the server and client?](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[218] what are the advantages and disadvantages of CS architecture and BS architecture and data on the server and client?

异地远程连接在家里的群晖NAS【无公网IP,免费内网穿透】

Mysql database - SQL summary (remember to pay attention to me! Come on in China!)
随机推荐
Unity2d horizontal game jump real-time response
Unity (III) three dimensional mathematics and coordinate system
IP notes (9)
UE4 reload system 1. basic principle of reload system
微信TBS在线安装内核失败的解决方法
IP job (1)
Leetcode sword finger offer JZ9 dual stack implementation queue
Leetcode sword finger offer jz42 maximum sum of continuous subarrays
Data set and pre training model
Hololens 2 development: development environment deployment
Leetcode refers to the duplicate number in the offer jz3 array
IP job (2) rip
jz47 礼物的最大价值(动态规划思路)
Do not rent servers, build your own personal business website (2)
利用内网穿透,实现公网访问内网
不租服务器,自建个人商业网站(1)
力扣986.区间列表的交集
Flink function (1): rich function
Metersphere one stop open source continuous testing platform
IP course (OSPF) comprehensive experiment