天天看點

java tag_java:tag 自定義标簽應用

一,tag類

1.1 TagMy标簽類,格式化目前日期并輸出

package com.dkt.tag;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.TagSupport;

public class TagMy extends TagSupport{

private String format;

public int doStartTag() {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

String nowdate = simpleDateFormat.format(new Date());

JspWriter out = pageContext.getOut();

try {

out.print("Hello 世界!!");

out.print(nowdate);

} catch (IOException e) {

e.printStackTrace();

}

return super.SKIP_BODY;

}

public int doAfterBody() throws JspException {

return super.doAfterBody();

}

public int doEndTag() throws JspException {

return super.doEndTag();

}

public String getFormat() {

return format;

}

public void setFormat(String format) {

this.format = format;

}

}

1.2 ForTag 标簽類,自定義标簽,循環輸出

package com.dkt.tag;

import javax.servlet.jsp.tagext.TagSupport;

public class FroTag extends TagSupport{

private int start;

private int end;

private String var;

public int doStartTag(){

System.out.println("doStartTag()"+start);

if(start<=end){

pageContext.setAttribute(var, start);

start++;

return super.EVAL_BODY_INCLUDE;

}else {

return super.SKIP_BODY;

}

//執行doAfterBody()

}

public int doAfterBody() {

if(start<=end){

pageContext.setAttribute(var, start);

start++;

System.out.println("doAfterBody()"+start);

return super.EVAL_BODY_AGAIN;

}else {

return super.SKIP_BODY;

}

}

public int doEndTag(){

System.out.println("doEndTag()"+start);

return super.EVAL_PAGE;

}

public int getStart() {

return start;

}

public void setStart(int start) {

this.start = start;

}

public int getEnd() {

return end;

}

public void setEnd(int end) {

this.end = end;

}

public String getVar() {

return var;

}

public void setVar(String var) {

this.var = var;

}

}

1.3 IteratorTag标簽類,疊代集合,全部循環輸出顯示在頁面

package com.dkt.tag;

import java.util.ArrayList;

import java.util.Iterator;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

public class IteratorTag extends TagSupport{

private String scope;

private String var;

private ArrayList list = null;

Iterator it = null;

public int doStartTag() {

it = list.iterator();

return super.EVAL_BODY_INCLUDE;

}

public int doAfterBody() {

if (it.hasNext()) {

Object next = it.next();

pageContext.setAttribute(var, next);

return super.EVAL_BODY_AGAIN;

}else {

return super.SKIP_BODY;

}

}

public int doEndTag() throws JspException {

return super.doEndTag();

}

public String getScope() {

return scope;

}

public void setScope(String scope) {

this.scope = scope;

}

public String getVar() {

return var;

}

public void setVar(String var) {

this.var = var;

}

public ArrayList getList() {

return list;

}

public void setList(ArrayList list) {

this.list = list;

}

}

1.4 QueryTag 标簽類,根據表名,查詢資料庫,并由表格輸出到頁面

package com.dkt.tag;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.TagSupport;

import com.dkt.util.DButil;

public class QueryTag extends TagSupport{

private String tableName;

public int doStartTag() {

DButil dButil = new DButil();

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

conn = dButil.createConn();

String sql = "select * from "+tableName;

try {

ps = conn.prepareStatement(sql);

ResultSetMetaData rsmd = ps.getMetaData();//得到表結構

rs = ps.executeQuery();

JspWriter out = pageContext.getOut();

out.print("

for (int i = 0; i < rsmd.getColumnCount(); i++) {//字段數量

out.print("

"+rsmd.getColumnName(i+1)+"");//字段名字

}

out.print("

");

while(rs.next()){

out.print("

");

for (int i = 0; i < rsmd.getColumnCount(); i++) {

out.print("

"+rs.getObject(i+1)+"");

}

out.print("

");

}

out.print("

");

} catch (SQLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return super.SKIP_BODY;

}

public int doAfterBody() throws JspException {

return super.doAfterBody();

}

public int doEndTag() throws JspException {

return super.doEndTag();

}

public String getTableName() {

return tableName;

}

public void setTableName(String tableName) {

this.tableName = tableName;

}

}

二,hellotag.tld

檔案 hellotag.tld 放在WEB-INF目錄下,與web.xml同級

/p>

"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

1.0

1.2

hello

hello

hello

com.dkt.tag.TagMy

empty

format

true

true

formy

com.dkt.tag.FroTag

jsp

start

true

true

end

true

true

var

true

true

iteratormy

com.dkt.tag.IteratorTag

jsp

list

true

true

scope

true

true

var

true

true

query

com.dkt.tag.QueryTag

empty

tableName

true

true

三,jsp

index.jsp  開啟伺服器,直接通路項目下的index.jsp頁面即可顯示自定義标簽

在jsp頁面導入自定義标簽

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'index.jsp' starting page

formy:

${item1 }----

iterator:

ArrayList list = new ArrayList();

list.add(new UserInfo(0,"小白","123456","",""));

list.add(new UserInfo(1,"小白111","353456","",""));

list.add(new UserInfo(2,"小白222","532456","",""));

list.add(new UserInfo(3,"小白333","532356","",""));

list.add(new UserInfo(4,"小白444","123536","",""));

request.setAttribute("list",list);

%>

${item2.name}+++${item2.password }

四,web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp