JDBC+Servlet+JSP整合开发之27.JSP自定义标签

�C简介
�C标签接口和类
�C标签的分类
�C标签实例
? HelloWorld 简单标签
?有属性的标签
?有标签体,有属性的标签
?嵌套标签
?循环标签
----------------------------------------------------------------------------
? 简介
�C标签在JSP页面中被调用
�C标签对应一个Java 处理类,来处理标签逻辑
�C好处是,是页面中尽量少写代码
? 标签接口和类
�CJSP所有标签都实现了javax.servlet.jsp.JspTag接口
?这个接口有两个之间接口:
? SimpleTag
�CJSP 2.0 中新增的接口
? Tag
�C是一个经典的必须实现的接口
�C它有一个直接接口是IterationTag
image
? 标签的分类
�C不带属性和标签体的简单标签
�C不带标签体,但是有属性的标签
�C带标签体,且有属性的标签
? 标签实例
�CHelloWorld 简单标签
�C有属性的标签
�C有标签体,有属性的标签
�C嵌套标签
�C循环标签
先来看下如何实现简单的标签哈~
MyTag.java
此类实现了Tag接口
image 
MyTag.tld
image
MyTag.jsp
image
测试:
image
下面我们继承TagSupport抽象类,这样更简单一些哈~
MyTag2.java
image
MyTag.tld
image
MyTag2.jsp
image
测试:
image
下面我们输出一个下拉列表哈~
MyTag3.java
image
MyTag.tld
image
MyTag.jsp
image
测试:
image 
下面从数据库取出下拉列表内容,并将其定义为标签。
  image
MyTag.tld
image
MyTag.jsp
image
MyTag4.java
image
PositionService.java
image
ConnectionUtil.java
package com.michael.tag;    

import java.sql.Connection;    
import java.sql.DriverManager;    
import java.util.Properties;    

public class ConnectionUtil {    

         /**    
         * @param args    
         */    
         public static void main(String[] args) {    
                ConnectionUtil cu = new ConnectionUtil();    
                System.out.println(cu.openConnection());    
        }    
         public Connection openConnection() {    
                String url = "";    
                String driver = "";    
                String user = "";    
                String password = "";    
                Properties prop = new Properties();    
                 try {    
                        prop.load( this.getClass().getClassLoader().getResourceAsStream( "DBConfig.properties"));    
                        driver = prop.getProperty( "driver");    
                        url = prop.getProperty( "url");    
                        user = prop.getProperty( "user");    
                        password = prop.getProperty( "password");    
                        Class.forName(driver);    
                        Connection conn = DriverManager.getConnection(    
                                        url, user, password);    
                         return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                 return null;    
        }    
         public Connection getConnection(String driver, String url, String user,    
                        String password) {    
                 // Class.forName()    
                 try {    
                        Class.forName(driver);    
                         // DriverManager get connection    
                        Connection conn = DriverManager.getConnection(url, user, password);    
                         return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                 return null;    
        }    

         public Connection getConnection() {    
                 // Class.forName()    
                 try {    
                        Class.forName( "com.mysql.jdbc.Driver");    
                         // DriverManager get connection    
                        Connection conn = DriverManager.getConnection(    
                                         "jdbc:mysql://localhost:3306/jsp_db", "root", "963963");    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    

}
Position.java
image
DBConfig.properties
image
测试:
image
数据库中有四条记录,我们看下能不能取出来哈~
image
下面我们在数据库中增加一条记录测试一下哈~
  image
  image
以上四个标签都是没有属性的,下面我们测试有属性的标签,传递一个参数,循环的输出哈~
MyTag5.java
image
MyTag.tld
image
MyTag.jsp
image
测试:
image
下面测试有标签体的标签,我们可以实现一个接口或者继承一个类哈~
先看实现接口的方法
MyTag6.java
package com.michael.tag;    

import java.io.IOException;    

import javax.servlet.jsp.JspException;    
import javax.servlet.jsp.PageContext;    
import javax.servlet.jsp.tagext.BodyContent;    
import javax.servlet.jsp.tagext.BodyTag;    
import javax.servlet.jsp.tagext.Tag;    

public class MyTag6 implements BodyTag{    
         private int count;    
         private BodyContent bodyContent;    
         public void setCount( int count) {    
                 this.count = count;    
        }    
         public void doInitBody() throws JspException {    
        }    

         public void setBodyContent(BodyContent bodyContent) {    
                 this.bodyContent = bodyContent;    
        }    

         public int doAfterBody() throws JspException {    
                 if(count>1){    
                        count--;    
                         return this.EVAL_BODY_AGAIN;    
                }    
                 return this.SKIP_BODY;    
        }    

         public int doEndTag() throws JspException {    
                 try {    
                         if(bodyContent!= null)    
                        bodyContent.print(bodyContent.getEnclosingWriter());    
                } catch (IOException e) {    
                        e.printStackTrace();    
                }    
                 return this.EVAL_PAGE;    
        }    

         public int doStartTag() throws JspException {    
                 if(count>0){    
                         return this.EVAL_BODY_INCLUDE;    
                }    
                 return this.SKIP_BODY;    
        }    

         public Tag getParent() {    
                 return null;    
        }    

         public void release() {    
        }    

         public void setPageContext(PageContext pageContext) {    
        }    

         public void setParent(Tag tag) {    
        }    
}
MyTag.tld
image
MyTag.jsp
image
测试:
image
下面我们实现抽象类来测试有标签体的标签哈~
MyTag7.java
image
MyTag.tld
image
MyTag.jsp
image
测试:
image
在JSP2.0中还有一个新特征,具有SimpleTag接口,还有针对这个接口的实现类,如果使用这个接口就更方法了哈~
MyTag8.java
image
MyTag.tld
image
MyTag.jsp
image
测试:
image
下面我们实现针对这个接口的抽象类
MyTag9.java
image
MyTag.tld
image
MyTag.jsp
image
测试:
  image
 
------------------------------------END-----------------------------------

你可能感兴趣的:(jsp,servlet,开发,jdbc,定义)