1.配置文件:
将jstl.jar和standard.jar两jar包配置到WEB-INF下的lib目录或是根目录下的lib目录下。
2.配置tld文件
calendar.tld --->文件名任意
< ?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlib -version>1.0</tlib>
<jsp -version>2.0</jsp>
<short -name>jr</short>
<uri>calendar@huaiyu.com</uri>
<tag>
<name>calendar</name>
</tag><tag -class>
com.kettas.servlet.TagCalendar
</tag>
<body -content>empty</body>
</taglib>
name指定标签名
tag -class标签名于相应的类对应
3.类文件 :TagCalendar.java
package com.kettas.servlet;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspWriter;
public class TagCalendar extends TagSupport {
private static final long serialVersionUID = -7677534442361565504L;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
StringBuffer sCalendar = new StringBuffer();
GregorianCalendar gc = new GregorianCalendar();
int today = gc.get(Calendar.DAY_OF_MONTH);
int month = gc.get(Calendar.MONTH);
int year = gc.get(Calendar.YEAR);
String days[] = new String[42];
for (int i = 0; i < 42; i++) {
days[i] = "";
}
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.MONTH, month); // 设置当月
cal.set(Calendar.YEAR, year); // 设置当年
cal.setFirstDayOfWeek(Calendar.SUNDAY);// 设置星期天开始
cal.set(Calendar.DAY_OF_MONTH, 1); // 设置1号
int firstIndex = cal.get(Calendar.DAY_OF_WEEK) - 1; // 取得当月1号所在的星期数
int maxIndex = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 取得当月的最大天数
for (int i = 0; i < maxIndex; i++) {
days[firstIndex + i] = String.valueOf(i + 1);
}
try {
sCalendar.append("
<table border='0' width='168' height='81'>");
sCalendar.append("
<tr>
<td colspan='7' align='center'>"+year + "年" + (month + 1) + "月" + today+"日</td>
</tr>
");
sCalendar.append("
<tr>
<th width='25' height='16'><font color='red'>日</font></th>
");
sCalendar.append("
<th width='25' height='16'>一</th>
<th width='25' height='16'>二</th>
");
sCalendar.append("
<th width='25' height='16'>三</th>
<th width='25' height='16'>四</th>
");
sCalendar.append("
<th width='25' height='16'>五</th>
<th width='25' height='16'><font color='red'> 六</font></th>
</tr>
");
for(int i=0;i<6;i++){
sCalendar.append("
<tr>");
for(int j=i*7;j< (i+1)*7;j++){
sCalendar.append("
<td width='15%' height='16' valign='middle' align='center'>");
if((j-firstIndex+1)==today){
sCalendar.append("<font color='red'>"+days[j]+"</font>");
}else{
sCalendar.append(days[j]);
}
sCalendar.append("");
}
sCalendar.append("</tr>
");
}
sCalendar.append("");
out.print(sCalendar.toString());
} catch (IOException e) {
e.printStackTrace();
}
return TagSupport.SKIP_BODY;
}
}
4.jsp文件:calendar.jsp
< %@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
< %@ taglib prefix="jr" uri="calendar@huaiyu.com"%>
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</meta></head>
<body>
<jr :calendar/>
</body>
</html>
----->注意jsp文件中的uri与tld中的uri相一致,不然找不到tld文件