–簡介
–標籤接口和類
–標籤的分類
–標籤實例
? HelloWorld 簡單標籤
?有屬性的標籤
?有標籤體,有屬性的標籤
?嵌套標籤
?循環標籤
----------------------------------------------------------------------------
? 簡介
–標籤在JSP頁面中被調用
–標籤對應一個Java 處理類,來處理標籤邏輯
–好處是,是頁面中儘可能少寫代碼
? 標籤接口和類
–JSP全部標籤都實現了javax.servlet.jsp.JspTag接口
?這個接口有兩個之間接口:
? SimpleTag
–JSP 2.0 中新增的接口
? Tag
–是一個經典的必須實現的接口
–它有一個直接接口是IterationTag
? 標籤的分類
–不帶屬性和標籤體的簡單標籤
–不帶標籤體,可是有屬性的標籤
–帶標籤體,且有屬性的標籤
? 標籤實例
–HelloWorld 簡單標籤
–有屬性的標籤
–有標籤體,有屬性的標籤
–嵌套標籤
–循環標籤
先來看下如何實現簡單的標籤哈~
MyTag.java
此類實現了Tag接口
MyTag.tld
MyTag.jsp
測試:
下面咱們繼承TagSupport抽象類,這樣更簡單一些哈~
MyTag2.java
MyTag.tld
MyTag2.jsp
測試:
下面咱們輸出一個下拉列表哈~
MyTag3.java
MyTag.tld
MyTag.jsp
測試:
下面從數據庫取出下拉列表內容,並將其定義爲標籤。
MyTag.tld
MyTag.jsp
MyTag4.java
PositionService.java
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
DBConfig.properties
測試:
數據庫中有四條記錄,咱們看下能不能取出來哈~
下面咱們在數據庫中增長一條記錄測試一下哈~
以上四個標籤都是沒有屬性的,下面咱們測試有屬性的標籤,傳遞一個參數,循環的輸出哈~
MyTag5.java
MyTag.tld
MyTag.jsp
測試:
下面測試有標籤體的標籤,咱們能夠實現一個接口或者繼承一個類哈~
先看實現接口的方法
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
MyTag.jsp
測試:
下面咱們實現抽象類來測試有標籤體的標籤哈~
MyTag7.java
MyTag.tld
MyTag.jsp
測試:
在JSP2.0中還有一個新特徵,具備SimpleTag接口,還有針對這個接口的實現類,若是使用這個接口就更方法了哈~
MyTag8.java
MyTag.tld
MyTag.jsp
測試:
下面咱們實現針對這個接口的抽象類
MyTag9.java
MyTag.tld
MyTag.jsp
測試:
------------------------------------END-----------------------------------