当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

常用工作方法总结(7S、SWOT分析、PDCA循环、SMART原则、6W2H、时间管理、WBS、二八原则)

不租服务器,自建个人商业网站(3)

jz47 礼物的最大价值(动态规划思路)

IA课总结(2)
![[226] instructions for Wireshark parameters](/img/47/0d3fd221695920e02b1f690a2a21c1.png)
[226] instructions for Wireshark parameters

Heap overflow of kernel PWN basic tutorial

公网使用Microsoft Remote Desktop远程桌面,随时远程办公

【226】wireshark的参数使用说明

Hololens 2 development: development environment deployment

Leetcode sword finger offer JZ9 dual stack implementation queue
随机推荐
Unity2d horizontal game jump real-time response
Ia note 1
IP notes (7)
IP notes (6)
不租服务器,自建个人商业网站(如何购买域名)
IP notes (11)
【226】wireshark的参数使用说明
Using keras to realize multidimensional (multivariable) time series prediction of cnn+bilstm+attention
[219] what is the difference between app testing and web testing?
IP job (2) rip
IP class notes (5)
leetcode剑指offer JZ42 连续子数组的最大和
Hololens 2 Chinese development document MRTK V2
[251] common test tools
Hololens 2 development 101: create the first hololens 2 Application
Summary of common working methods (7S, SWOT analysis, PDCA cycle, smart principle, 6w2h, time management, WBS, 28 principles)
【251】常见的测试工具
How does the latest version of text (TMP) UI text of unity display in Chinese
ip作业(1)
Do not rent servers, build your own personal business website (3)