JSP custom tag - replacing the bean with "scripting programming variable" to traverse

xiaoxiao2021-03-06  15

Tag13.java

Package tag13;

import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; import java.io.IOException; import java.util.List; import java.util.Iterator;

public class Tag13 extends BodyTagSupport {private List lists; private Iterator iters; public void setLists (List lists) {this.lists = lists;} public int doStartTag () throws JspException {return EVAL_BODY_BUFFERED;} public void doInitBody () throws JspException {if (Lists.size () <= 0) return; item = lists.iterator (); page.id, iters.next ();} public int dressboDy () throws jspexception {ix (ney.hasnext )) {pageContext.setAttribute (this.id, iters.next ()); return EVAL_BODY_AGAIN;} else {try {bodyContent.writeOut (this.getPreviousOut ());} catch (IOException ioe) {throw new JspException (ioe. GetMessage ());}} Return Skip_body;}}

INFO13.JAVA

Package tag13;

Import javax.servlet.jsp.tagext.tagdata; import javax.servlet.jsp.tagext.tagextrainfo; import javax.servlet.jsp.tagext.variableinfo;

public class Info13 extends TagExtraInfo {public VariableInfo [] getVariableInfo (TagData data) {return new VariableInfo [] {new VariableInfo (data.getId (), "java.lang.Object", true, VariableInfo.NESTED)};}}

Tag13.tld

1.2 1.2 Aaaaaaaaaaaaaaaa aaaaaaaaa tag13 tag13.tag13 tag13.info13 JSP id true TRUE lists True TRUE

Tag13.jsp

<% @ Page ContentType = "Text / HTML; Charset = GBK"%> <% @ Taglib prefix = "TAG" URI = "tag13.tld"%> <% @ page import = "java.util.list, Java. Util.arrayList "%>

<% List mylists = new ArrayList (); mylists.add ( "aaaaaaaaaaaaaaa"); mylists.add ( "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); mylists.add ( "ccccccccccccccccccccccccccccccccccc"); mylists.add ( "dddddddddddddddddddddddddddddddddddddddddddd" );%>

<% = iTer%> < / TD> Result:

aaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddddddddddddddd

analysis:

1. When calculating the body in the tag class, the BEAN is saved in the key to the key in the ID, and then directly accesses this bean directly in the JSP page.

2. Since the ID attribute of the tag class is maintained by the Tagsupport class and the BodytagSupport class, the programmer does not need to set an ID attribute in its own customized tag class.

转载请注明原文地址:https://www.9cbs.com/read-46594.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.031, SQL: 9