TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
这是我学jDlog 1.0 做的一个练习。先看dlog.tld文件:
<?xml version="1.0" encoding="GB2312"?>
<!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>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>dlog</shortname>
<uri>http://www.dlogcn.com</uri>
<tag><!-- 用于calendar.jsp -->
<name>calendar</name>
<tagclass>jdlog.tags.CalendarTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>year</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>month</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
再看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=ƍ"><b>"+year+"年"+month+"月</b></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<datas.length;i++){
out.print("<tr>");
for(int j=0;j<datas.length;j++)
out.print("<td>"+datas[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<days.length;i++){
for(int j=0;j<days.length;j++)
System.out.print(days[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">
<dlog:calendar year="2001" month="5"/></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--;
}
%>
<a href=tag.jsp?paramYear=<%=lastYear %>¶mMonth=<%=last_m %>>上一月</a>
<a href=tag.jsp?paramYear=<%=nextYear %>¶mMonth=<%=next_m %>>下一月</a>
<a href=tag.jsp?paramYear=<%=(year.intValue()-1) %>¶mMonth=
<%=month.intValue() %>>上一年</a>
<a href=tag.jsp?paramYear=<%=(year.intValue()+1) %>¶mMonth=
<%=month.intValue() %>>下一年</a>
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");
}
源码下载:http://file.javaxxz.com/2014/10/1/045908250.zip |
|