"Custom Label Development" 09-label case-developing a foreach tag

Source: Internet
Author: User
Tags tag name tld

We then develop a label for the iteration set. We know that there is a <s:iterator> tag in struts that iterates over the set, and we then imitate it to write a custom label.

The effect of the front desk is as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><% @taglib uri= "/example" prefix= "Z"% ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

First create the Label processor class:


Then write the logic in it:
Package Org.zyg.web.exampletag;import Java.io.ioexception;import Java.util.iterator;import java.util.List;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.simpletagsupport;public class ForeachTag extends Simpletagsupport {    //incoming array, MAP, list is possible, so use object to receive parameters    private object items;    Private String var;        public void Setitems (Object items) {        this.items = items;    }    public void SetVar (String var) {        This.var = var;    }    @Override public    void Dotag () throws Jspexception, IOException {        //iterating the collection for        list list= (list) items;        Iterator it=list.iterator ();        while (It.hasnext ()) {            Object value=it.next ();            Place the value of the iteration into the Var value in PageContext            this.getjspcontext (). SetAttribute (Var, value);            Displays the label body on the page            this.getjspbody (). Invoke (null);}}    

Then register the tag in the Z.TLD configuration file:
<tag>    <name>foreach</name><!--Tag name--    <tag-class> Org.zyg.web.exampletag.foreachtag</tag-class>    <body-content>scriptless</body-content> <!--labels (single label or paired label)-    <attribute>        <name>var</name>        <required>true </required>        <rtexprvalue>true</rtexprvalue>    </attribute>        <attribute>        <name>items</name>        <required>true</required>        <rtexprvalue>true </rtexprvalue>    </attribute></tag>

To restart the Web App test:


Discover that we have successfully iterated the list collection.

Now our iteration tags can only iterate through the list collection, and we can't iterate over other types of collection data such as map and arrays, and we'll make improvements below.

Our expected effect for the previous paragraph is:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><% @taglib uri= "/example" prefix= "Z"% ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
We modified our previous tag processor class:
Package Org.zyg.web.exampletag;import Java.io.ioexception;import Java.util.arrays;import java.util.Collection; Import Java.util.iterator;import java.util.list;import Java.util.map;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.simpletagsupport;public class Foreachtag extends Simpletagsupport {//incoming array, MAP, list is possible,    So use object to receive the parameter private Object items;    Private String var;        Private Collection Collection;        The type is converted when the items parameter is set, regardless of what is converted to a single-column collection public void Setitems (Object items) {this.items = items; First determine whether the data passed in is not a Collection if (items instanceof Collection) {//list set (single column) Collection = (Collection) it        Ems            } if (items instanceof map) {map map= (map) items; Collection= Map.entryset ();//Double column is changed to single-row set} if (Items instanceof object[]) {object[] obj= (            Object[]) items; Collection=arrays.aslist (obj);//Convert an array to a single list}} public void SetVar (String var) {this.var = var; } @Override public void Dotag () throws Jspexception, IOException {//Iterate over collection Iterator it=collection.it        Erator ();            while (It.hasnext ()) {Object value=it.next ();            Place the value of the iteration into the Var value in PageContext this.getjspcontext (). SetAttribute (Var, value);        Displays the label body on the page this.getjspbody (). Invoke (null); }    }}

The label was previously registered in Z.TLD and is no longer registered here.

Then restart the web app to see the effect:

We have found 3 types of data to iterate over.

So can we iterate over 8 basic data types? Let's try it:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><% @taglib uri= "/example" prefix= "Z"% ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Refresh the page and find the error:


A "null pointer exception" error was reported. Let's take a look at what our Foreachtag code 45 line is:


A description of our collection.iterator (), NULL, that is, an array of basic data types is not determined by any of the judging conditions of the items type in Setitems (Object items), So we're going to add a judgment condition that determines the array of basic data types:
Note: Here is the use of reflection technology, if you do not know this piece, it is recommended to learn first.
Package Org.zyg.web.exampletag;import Java.io.ioexception;import Java.lang.reflect.array;import Java.util.arraylist;import Java.util.arrays;import Java.util.collection;import Java.util.Iterator;import Java.util.map;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.simpletagsupport;public Class    Foreachtag extends Simpletagsupport {//incoming array, MAP, list is possible, so use object to receive parameters private object items;    Private String var;        Private Collection Collection;        The type is converted when the items parameter is set, regardless of what is converted to a single-column collection public void Setitems (Object items) {this.items = items; First determine whether the data passed in is not a Collection if (items instanceof Collection) {//list set (single column) Collection = (Collection) it        Ems            } if (items instanceof map) {map map= (map) items; Collection= Map.entryset ();//double-row is changed to a single-column set} if (Items.getclass (). IsArray ()) {//judgment is not an array of this            . Collection=new ArrayList (); int length=array.geTlength (items);//Gets the array length for (int i = 0; i < length; i++) {Object value=array.get (items, i);            This.collection.add (value);            }}/*if (Items instanceof object[]) {object[] obj= (object[]) items; Collection=arrays.aslist (obj);//Convert the array to a single-column list}*/} public void SetVar (String var) {this.    var = var; } @Override public void Dotag () throws Jspexception, IOException {//Iterate over collection Iterator it=collection.it        Erator ();            while (It.hasnext ()) {Object value=it.next ();            Place the value of the iteration into the Var value in PageContext this.getjspcontext (). SetAttribute (Var, value);        Displays the label body on the page this.getjspbody (). Invoke (null); }    }}
The above code, you can iterate over any type of array, of course, you can use the same method in our own project after understanding.

Our final results:


At this point, our complete foreach iteration label has been developed.
Reprint Please specify Source: http://blog.csdn.net/acmman/article/details/51258357

"Custom Label Development" 09-label case-developing a foreach tag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.