JSP series: (6) JSP advanced-imitate JSTL core Tag Library

Source: Internet
Author: User
Tags tld

1. If label

Iftag.java file

Package Com.rk.tag;import Java.io.ioexception;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.jspfragment;import Javax.servlet.jsp.tagext.simpletagsupport;public class IfTag extends Simpletagsupport{private boolean test;public void Settest (Boolean test) {this.test = test;} @Overridepublic void Dotag () throws Jspexception, ioexception{//determines whether to output the label body content if (test = = True), based on the return value of the test {//jspfragment Jspbody = Getjspbody ();//jspbody.invoke (null); This.getjspbody (). Invoke (null);}}


. tld File Configuration:

<tag><name>if</name><tag-class>com.rk.tag.IfTag</tag-class><body-content> Scriptless</body-content><attribute><name>test</name><required>true</required ><rtexprvalue>true</rtexprvalue><type>boolean</type></attribute></tag>


JSP file

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >


2, choose, when, otherwise label

Choosetag.java file

Package Com.rk.tag;import Java.io.ioexception;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.simpletagsupport;public class Choosetag extends simpletagsupport{//is not a property but a temporary variable that is used to pass in the middle of a child label "State" private Boolean executed = False;public Boolean isexecuted () {return executed;} public void setexecuted (Boolean executed) {this.executed = executed;} @Overridepublic void Dotag () throws Jspexception, Ioexception{this.getjspbody (). Invoke (null);}}


. tld File Configuration:

<tag><name>choose</name><tag-class>com.rk.tag.ChooseTag</tag-class>< Body-content>scriptless</body-content></tag>


Whentag.java file

Package Com.rk.tag;import Java.io.ioexception;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.jsptag;import Javax.servlet.jsp.tagext.simpletagsupport;public class WhenTag extends Simpletagsupport{private boolean test;public void Settest (Boolean test) {this.test = test;} @Overridepublic void Dotag () throws Jspexception, ioexception{//gets the parent tag choosetag parent = (Choosetag) this.getparent ();// If the contents of the parent tag have not been executed and the current judging condition (test) is true, then the output if (!parent.isexecuted () && test==true) {this.getjspbody () is executed. Invoke (NULL);p arent.setexecuted (True);}}}





. tld File Configuration:

<tag><name>when</name><tag-class>com.rk.tag.whentag</tag-class><body-content >scriptless</body-content><attribute><name>test</name><required>true</ required><rtexprvalue>true</rtexprvalue><type>boolean</type></attribute></ Tag>




Otherwisetag.java file

Package Com.rk.tag;import Java.io.ioexception;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.jsptag;import Javax.servlet.jsp.tagext.simpletagsupport;public class OtherWiseTag extends simpletagsupport{@Overridepublic void Dotag () throws Jspexception, ioexception{//get parent tag Choosetag father = (Choosetag) This.getparent (); if (!parent.isexecuted ()) {this.getjspbody (). Invoke (null);}}


. tld File Configuration:

<tag><name>otherwise</name><tag-class>com.rk.tag.OtherWiseTag</tag-class>< Body-content>scriptless</body-content></tag>


jsp file

<%@ page language= "java"  import= "java.util.*"  pageencoding= "Utf-8"%><% @taglib  uri= "Http://www.lsieun.com/rk"  prefix= "RK"  %><! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >



3. Foreach Label

Foreachtag.java file

package com.rk.tag;import java.io.ioexception;import java.util.collection;import  java.util.list;import java.util.map;import javax.servlet.jsp.jspexception;import  javax.servlet.jsp.pagecontext;import javax.servlet.jsp.tagext.simpletagsupport;public class  foreachtag extends simpletagsupport{private object items;private string var; Public void setitems (object items) {this.items = items;} Public void setvar (String var) {This.var = var;} @Overridepublic  void dotag ()  throws JspException, IOException{PageContext  pagecontext =  (PageContext) this.getjspcontext (); Collection colls = null;if (items instanceof list) {colls =  (List) items;} if (items instanceof map) {map map =  (MAP) Items;colls = map.entryset ();}      for (object object:colls) {//Put eachThe object is placed in the domain object (pagecontext) Pagecontext.setattribute (var, object);//Displays the contents of the tag body this.getjspbody (). Invoke (null);}} 


. tld File Configuration:

<tag><name>forEach</name><tag-class>com.rk.tag.ForEachTag</tag-class>< Body-content>scriptless</body-content><attribute><name>items</name><required> true</required><rtexprvalue>true</rtexprvalue><type>java.lang.object</type></ Attribute><attribute><name>var</name><required>true</required><rtexprvalue >false</rtexprvalue><type>java.lang.String</type></attribute></tag>


JSP file

<%@ page language= "java"  import= "Java.util.*,com.rk.entity.student"  pageEncoding= "Utf-8 "%><% @taglib  uri=" Http://www.lsieun.com/rk " prefix=" RK " %><! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >



JSP series: (6) JSP advanced-imitate JSTL core Tag Library

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.