当前页面: 开发资料首页 → Java 专题 → 一个用标记做的月历
一个用标记做的月历
摘要: 一个用标记做的月历
</td>
</tr>
<tr>
<td width="531" height="35" valign="top" class="ArticleTeitle">
这是我学jDlog 1.0 做的一个练习。先看dlog.tld文件:
<?xml version="1.0" encoding="GB2312"?>
taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
1.0
1.1
dlog
http://www.dlogcn.com
calendar
jdlog.tags.CalendarTag
<bodycontent>empty</body>
year
false
true
month
false
true
再看CalendarTag.java,年和月可以从属性或请求参数中获取
/*
* Created on 2004-2-6
*/
package jdlog.tags;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* @author Liudong
* 日历标签库
*/
public class CalendarTag extends TagSupport {
protected int year = 2005;
protected int month = 2;
private final static SimpleDateFormat sdf =
new SimpleDateFormat("MMMMM yyyy",Locale.ENGLISH);
public int doStartTag() throws JspException {
Calendar cal = Calendar.getInstance();
JspWriter out=pageContext.getOut();
try{
if(pageContext.getRequest().getParameter("paramYear")!=null){
year = Integer.parseInt(pageContext.getRequest().getParameter("paramYear")); }
if(year>0)
cal.set(Calendar.YEAR,year);
}catch(Exception e){ System.out.println(e.toString());}
try{
if(pageContext.getRequest().getParameter("paramMonth")!=null){
month =
Integer.parseInt(pageContext.getRequest().getParameter("paramMonth")); }
if(month>0&&month<13)
cal.set(Calendar.MONTH,month-1);
else if(month==0){
cal.set(Calendar.MONTH,11);
cal.add(Calendar.YEAR,-1);
}
else if(month==13){
cal.set(Calendar.MONTH,0);
cal.add(Calendar.YEAR,1);
}
}catch(Exception e){}
int[][] datas = buildCalendar(cal);
try{
out.print("<table border=Ɔ' width=颼%' align='center' cellspacing=Ƈ'
cellpadding=Ƈ'><tr><td colspan=ƍ'>
"+year+"年"+month+"月</td></tr><tr><td
class='week'>Su</td><td class='week'>M</td><td class='week'>Tu</td>
<td class='week'>W</td><td
class='week'>Th</td> <td class='week'>F</td><td class='week'>Sa</td></tr>");
for(int i=0;i
out.print("<tr>");
for(int j=0;j
out.print("<td>"+datas[i][j]+"</td>");
out.print("</tr>");
}
out.print("</table>");
}
catch(Exception e){
System.out.println("Error");
}
//pageContext.setAttribute(datasId,datas);
pageContext.setAttribute("year",new Integer(cal.get(Calendar.YEAR)));
pageContext.setAttribute("month",new Integer(cal.get(Calendar.MONTH)+1));
//pageContext.setAttribute(titleId,sdf.format(cal.getTime()));
return SKIP_BODY;
}
/**
* 构造日历
* @param year
* @param month
* @return
*/
protected static int[][] buildCalendar(Calendar cal){
cal.set(Calendar.DAY_OF_MONTH,1);
int firstDateInWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
int dateOfMonth = getMonthDateCount(cal);
int base = dateOfMonth + firstDateInWeek;
int row = base / 7;
row += ((base%7)>0)?1:0;
int[][] cals = new int[row][7];
int iCol=firstDateInWeek,iRow=0;
for(int i=1;i<=dateOfMonth;i++){
cals[iRow][iCol] = i;
if(iCol==6){
iCol = 0;
iRow++;
}
else
iCol++;
}
return cals;
}
/**
* 得到指定月份的天数
* @param cal
* @return
*/
protected static int getMonthDateCount(Calendar cal){
Calendar cal2 = (Calendar)cal.clone();
cal2.add(Calendar.MONTH,1);
cal2.set(Calendar.DAY_OF_MONTH,1);
cal2.add(Calendar.DAY_OF_MONTH,-1);
return cal2.get(Calendar.DAY_OF_MONTH);
}
public static void main(String[] args) throws Exception{
int[][] days = buildCalendar(Calendar.getInstance());
for(int i=0;i
for(int j=0;j
System.out.print(days[i][j]+" ");
System.out.println();
}
}
/**
* @return
*/
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
public void setMonth(int month) {
this.month = month;
}
public void setYear(int year) {
this.year = year;
}
}
三、jsp调用
<%@ page language="java" contentType="text/html;charset=GBK" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib uri="/WEB-INF/dlog.tld" prefix="dlog" %>
<table><tr><td width="200">
</td></tr></table>
<%
Integer year=(Integer)pageContext.getAttribute("year");
Integer month=(Integer)pageContext.getAttribute("month");
int nextYear = year.intValue();
int lastYear = year.intValue();
int next_m = month.intValue()+1;
if(next_m>12){
next_m = 1;
nextYear++;
}
int last_m = month.intValue()-1;
if(last_m<1){
last_m = 12;
lastYear--;
}
%>
¶mMonth=<%=last_m %>>上一月
¶mMonth=<%=next_m %>>下一月
¶mMonth=
<%=month.intValue() %>>上一年
¶mMonth=
<%=month.intValue() %>>下一年
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
<td width="176" valign="top" class="ArticleTeitle">
</td>
</tr>
<tr>
<td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑返回目录
前一篇: Meta标签详解
后一篇: 如何避免重复提交