Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 298|回复: 0

[jsp学习]一个用标记做的月历

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 04:59:09 | 显示全部楼层 |阅读模式
    这是我学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=&#390;" width=&#65533;%" align="center" cellspacing=&#391;"  

    cellpadding=&#391;"><tr><td colspan=&#397;"><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 %>&paramMonth=<%=last_m %>>上一月</a>
    <a href=tag.jsp?paramYear=<%=nextYear %>&paramMonth=<%=next_m %>>下一月</a>
    <a href=tag.jsp?paramYear=<%=(year.intValue()-1) %>&paramMonth=
          <%=month.intValue() %>>上一年</a>
    <a href=tag.jsp?paramYear=<%=(year.intValue()+1) %>&paramMonth=
          <%=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
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-2 00:23 , Processed in 0.464264 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表