The ifelse of JSP custom tag and the example of traversing custom tag

Time:2020-10-19

The first example:
Simple JSP custom tag to get content:
First create a JSP instance class, and then inherit the simpletagsupport class
Then implement the dotag () method of the parent class
In this method, we get the content of the tag body this.getJspBody ();
Return the jspfragment class, and call invoke according to this class object( this.getJspContext ().getOut());
This method can also be empty, and the meaning expressed is also output to the browser;

Copy codeThe code is as follows:
public class SimpleDmeo1 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspFragment js =this.getJspBody();
js.invoke(null);
}
}

Then write TLD file, tag library description file, and JSP file, which are relatively simple
Throw an exception if you don’t want to execute something
Throw new skippageexception(); the sum face content will not be displayed
Next is a JSP custom tag file with attributes

Copy codeThe code is as follows:
public class SimpleDmeo1 extends SimpleTagSupport {
private int counts;
public void setCounts(int counts) {
this.counts = counts;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment js =this.getJspBody();
For (int i = 0; I < counts; I + +) {/ / loop fetch
js.invoke(null);
}
}

}
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
< short name > C < / short name > prefix name
<uri>http://www.csdn.com</uri>
<tag>
<name>demo</name>
<tag-class>com.csdn.simple.SimpleDmeo1</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>counts</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

Then write the content in JSP file;

Copy codeThe code is as follows:
< hbsi:demo counts= “3”>aaaaaaa<br/></ hbsi:demo >/ / output three parts
JspFragment js = this.getJspBody();
StringWriter jw = new StringWriter();
js.invoke(jw);
String s = jw.toString().toUpperCase();
JspWriter out =this.getJspContext().getOut();
for(int i=0;i<counts2;i++){
out.print(s);
}
}

This is the code converted to uppercase, the others are consistent;
There are too many codes about if else. I put them in the resources. If necessary, you can download them for reference only.

Recommended Today

Explain module, import and export in JavaScript

Author: Tania rascia Crazy technology house Original text:https://www.taniarascia.com/j… In the era of the Internet, websites are mainly developed with HTML and CSS. If you load JavaScript into a page, it usually provides effects and interactions in the form of small fragments. Generally, all JavaScript code is written in a file and loaded into a filescriptTag. […]