4. Compile tags with tags
To return tagsupport. eval_body_include, you can enter the label body;
Attributetag. Java
package org.tagext;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;public class AttributeTag extends TagSupport{private String name;public String getName(){return name;}public void setName(String name){this.name = name;}public int doStartTag()throws JspException{Object value = null;value = super.pageContext.getAttribute(name,PageContext.PAGE_SCOPE);if(value==null){return TagSupport.SKIP_BODY;}else{return TagSupport.EVAL_BODY_INCLUDE;}}}
Xiazdong. TLD
<tag><name>attribute</name><tag-class>org.tagext.AttributeTag</tag-class><body-content>JSP</body-content><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute> </tag>
Attributetag. jsp
<%@ page contentType="text/html" pageEncoding="GBK"%><%@ taglib prefix="xiazdong" uri="xiazdong"%>
5. Iteration labels
The iterative tag is defined as the repeated execution of the TAG body, which is often used in the output set; it is often used in MVC;
Code example:
I will not write some files that have not changed much;
Iteratetag. Java
package org.tagext;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;import java.util.*;public class IterateTag extends TagSupport{private String name;private String id;private Iterator<String> iter;public String getName(){return name;}public void setName(String name){this.name = name;}public String getId(){return id;}public void setId(String id){this.id = id;}public int doStartTag()throws JspException{Object value = null;value = super.pageContext.getAttribute(name,PageContext.PAGE_SCOPE);if(value==null||!(value instanceof List<?>)){return TagSupport.SKIP_BODY;}else{iter = ((List<String>)value).iterator();if(iter.hasNext()){super.pageContext.setAttribute(id,iter.next());return TagSupport.EVAL_BODY_INCLUDE;}else{return TagSupport.SKIP_BODY;}}}public int doAfterBody()throws JspException{if(iter.hasNext()){super.pageContext.setAttribute(id,iter.next());return TagSupport.EVAL_BODY_AGAIN;}else{return TagSupport.SKIP_BODY;}}}
Iteratetag. jsp
<%@ page contentType="text/html" pageEncoding="GBK" import="java.util.*"%><%@ taglib prefix="xiazdong" uri="xiazdong"%>
Vi. bodytagsupport class
Bodytagsupport stores all output content in bodycontent at a time;
According to the code output by the previous iteration, the code can be changed slightly. The main change is that extends bodytagsupport is required in iteratetag. Java;
Common Methods for bodytagsupport:
(1) bodytagsupport.Eval_body_buffered;
(2)Getpreviousout();
// Obtain the output stream from the output to the webpage
(3) bodycontent object storage data;
Package Org. tagext; import javax. servlet. JSP. tagext. *; import javax. servlet. JSP. *; import Java. util. *; public class iteratetag extends bodytagsupport {private string name; private string ID; private iterator <string> ITER; Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String GETID () {return ID;} public void setid (string ID) {This. id = ID;} public int dostarttag () Throws jspexception {object value = NULL; value = super. pagecontext. getattribute (name, pagecontext. page_scope); If (value = NULL |! (Value instanceof list <?> )) {Return tagsupport. skip_body;} else {iter = (list <string>) value ). iterator (); If (ITER. hasnext () {super. pagecontext. setattribute (ID, ITER. next (); Return tagsupport. eval_body_buffered; // exists in bodycontent} else {return tagsupport. skip_body ;}} public int doafterbody () throws jspexception {If (ITER. hasnext () {super. pagecontext. setattribute (ID, ITER. next (); Return tagsupport. eval_body_again;} else {return tagsupport. s Kip_body ;}} public int doendtag () throws jspexception {If (Super. bodycontent! = NULL) {try {super. bodycontent. writeout (Super. getpreviousout (); // output to the page} catch (exception e) {}} return bodytagsupport. eval_page ;}}
VII. Use of tagextrainfo and variableinfo
In <JSP: usebean id = "">, the ID attribute represents the Object Name and can be used to call the method. To achieve this effect, you must inherit the tagextrainfo class;
We need to create another file to indicate that the property object can be used in the script;
Myextrainfo. Java
package org.tag;import javax.servlet.jsp.tagext.*;public class MyTagExtraInfo extends TagExtraInfo{public VariableInfo[] getVariableInfo(TagData data){return new VariableInfo[]{new VariableInfo(data.getId(),"java.lang.String",true,VariableInfo.NESTED)};}}
Add the following content to xiazdong. TLD:
<Tei-class> // note org. Tag. mytagextrainfo </Tei-class>
Finally, in xiazdong. TLD:
<Tag> <Name> iterate </Name> <tag-class> Org. tagext. iteratetag </Tag-class> <body-content> JSP </body-content> <Tei-class> // pay attention to Org. tag. mytagextrainfo </Tei-class> <attribute> <Name> name </Name> <required> true </required> <rtexprvalue> true </rtexprvalue> </attribute> <attribute> <Name> id </Name> <required> true </required> <rtexprvalue> true </rtexprvalue> </attribute> </Tag>
In this way, it can be used in scriptlet in JSP;
<% @ Page contenttype = "text/html" pageencoding = "GBK" Import = "Java. util. * "%> <% @ taglib prefix =" xiazdong "uri =" xiazdong "%> <HTML>