–简介
–标签接口和类
–标签的分类
–标签实例
? HelloWorld 简单标签
?有属性的标签
?有标签体,有属性的标签
?嵌套标签
?循环标签
–标签接口和类
–标签的分类
–标签实例
? HelloWorld 简单标签
?有属性的标签
?有标签体,有属性的标签
?嵌套标签
?循环标签
----------------------------------------------------------------------------
? 简介
–标签在JSP页面中被调用
–标签对应一个Java 处理类,来处理标签逻辑
–好处是,是页面中尽量少写代码
–标签在JSP页面中被调用
–标签对应一个Java 处理类,来处理标签逻辑
–好处是,是页面中尽量少写代码
? 标签接口和类
–JSP所有标签都实现了javax.servlet.jsp.JspTag接口
?这个接口有两个之间接口:
? SimpleTag
–JSP 2.0 中新增的接口
? Tag
–是一个经典的必须实现的接口
–它有一个直接接口是IterationTag
–JSP所有标签都实现了javax.servlet.jsp.JspTag接口
?这个接口有两个之间接口:
? SimpleTag
–JSP 2.0 中新增的接口
? Tag
–是一个经典的必须实现的接口
–它有一个直接接口是IterationTag
? 标签的分类
–不带属性和标签体的简单标签
–不带标签体,但是有属性的标签
–带标签体,且有属性的标签
–不带属性和标签体的简单标签
–不带标签体,但是有属性的标签
–带标签体,且有属性的标签
? 标签实例
–HelloWorld 简单标签
–有属性的标签
–有标签体,有属性的标签
–嵌套标签
–循环标签
–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;
}
}
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) {
}
}
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
测试:
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/305932
,如需转载请自行联系原作者