由于涉及到项目隐私的问题,所以只把关键的部分贴出来,大家可以好好看看: 这是portlet种生成日历的核心代码: public class CalendarPortlet extends PortletAdapter implements ActionListener { private String selectUrl = ""; private String urlStr = ""; private String dateLink = ""; String strSelectDa = ""; public void actionPerformed(ActionEvent event) throws PortletException { selectUrl = event.getActionString(); } public void doView(PortletRequest request, PortletResponse response) throws PortletException, IOException { response.setContentType("text/html;charset=GBK"); try{ String CalendarHtml=this.createCalendar(request,response); request.setAttribute("grid", CalendarHtml); }catch(Exception e){ StringWriter sw=new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String grid=sw.toString(); request.setAttribute("grid", grid); }finally{ this.getPortletConfig().getContext().include("/jsp/Calendar.jsp", request, response); } } private String createCalendar(PortletRequest request, PortletResponse response) { StringBuffer calendarHtml=new StringBuffer(); SimpleDateFormat swf = new SimpleDateFormat("yyyy-MM-dd"); List weeks = new ArrayList(); weeks.add("日"); weeks.add("一"); weeks.add("二"); weeks.add("三"); weeks.add("四"); weeks.add("五"); weeks.add("六"); Calendar firstDayofMonth = Calendar.getInstance(); firstDayofMonth.add(Calendar.DAY_OF_MONTH, (Calendar.getInstance().get( Calendar.DAY_OF_MONTH) - 1) * -1); if (request.getParameter("_YEAR") != null && request.getParameter("_YEAR").trim() != "" && request.getParameter("_MONTH") != null && request.getParameter("_MONTH").trim()!= "") { String SelectTime=request.getParameter("_YEAR")+"-"+request.getParameter("_MONTH")+"-1"; java.util.Date selectDate = null; try { selectDate = swf.parse(SelectTime); } catch (ParseException e) { e.printStackTrace(); } firstDayofMonth.setTime(selectDate); firstDayofMonth.add(Calendar.DAY_OF_MONTH, (firstDayofMonth.get( Calendar.DAY_OF_MONTH) - 1)* -1); } Calendar CurrentDay = firstDayofMonth; int days = GetDaysOfcurrentMonth(CurrentDay);//获取当月的天数 int daily=0; int blancDay = firstDayofMonth.get(Calendar.DAY_OF_WEEK) - 1;//当月第一天偏移量 calendarHtml.append("<table border=/"0/" cellpadding=/"0/" cellspacing=/"1/" class=/"Calendar/" id=/"caltable/">"); calendarHtml.append("<tr align=/"center/" valign=/"middle/">"+ "<td colspan=/"1/" class=/"CTitle/">" +"<a href="/" mce_href="/""javaScript:subMonth();/" title=/"上一月/" Class=/"DayButton/">3</a></td>" + "<td colspan=/"5/" class=/"CTitle/" >" + "<span id=/"year/">"+ firstDayofMonth.get(Calendar.YEAR) +"</span>"+ "年" + "<span id=/"month/">"+(firstDayofMonth.get(Calendar.MONTH) + 1) +"</span>"+ "月</td>"+ "<td colspan=/"1/" class=/"CTitle/">"+ " <a href="/" mce_href="/""JavaScript:addMonth();/" title=/"下一月/" Class=/"DayButton/">4</a>" + "</td>" + "</tr>"); calendarHtml.append("<tr align=/"center/" valign=/"middle/" ><td class=DaySunTitle id=diary>"+weeks.get(0).toString()+"</td>"); for (int i = 1; i < weeks.size()-1; i++) calendarHtml.append("<td class=DayTitle id=diary>" + weeks.get(i).toString() + "</td>"); calendarHtml.append("<td class=DaySatTitle id=diary>" + weeks.get(6).toString() + "</td>"); calendarHtml.append("</tr>"); for(int intWeeks = 0; intWeeks < 6; intWeeks++) { calendarHtml.append("<tr style="/" mce_style="/""cursor:hand/">"); for(int intDays = 0; intDays < weeks.size();intDays++) { if((intDays==blancDay && (0 == daily)) || ((daily > 0) && (daily <= days)) ) { if((intDays==blancDay) && (0 == daily)) { daily=1;//当intDay是起始日,daily作为标志位表示找到起始日了,并作为游标 } DataView dv=DataHelper.findCalendar(CurrentDay.get(Calendar.YEAR)+"-"+(CurrentDay.get(Calendar.MONTH)+1)+"-"+CurrentDay.get(Calendar.DAY_OF_MONTH)); int Xtype=0; if(dv!=null && dv.size()>0) { DataRowView dr=(DataRowView)dv.get(0); Xtype=dr.getInt("xType"); } String type=""; switch(Xtype) { case 0 :type="正常上班"; break; case 1 :type="双休日"; break; case 2: type="国家法定假日"; break; default:type="正常上班"; break; } if (CurrentDay.get(Calendar.DAY_OF_MONTH) == Calendar.getInstance().get(Calendar.DAY_OF_MONTH) && CurrentDay.get(Calendar.MONTH) == Calendar.getInstance() .get(Calendar.MONTH)&& CurrentDay.get(Calendar.YEAR) == Calendar.getInstance() .get(Calendar.YEAR)){ calendarHtml.append("<td class=DayNow title=/"" + type + "/" >"); } else if(Xtype==1) { calendarHtml.append("<td class=DaySat title=/"" + type + "/" >"); } else if(Xtype==2) { calendarHtml.append("<td class=DaySat title=/"" + type + "/" >"); } else { calendarHtml.append("<td class=Day title=/"" + type + "/" >"); } calendarHtml.append(daily + ""); daily++; CurrentDay.add(Calendar.DAY_OF_MONTH, 1); calendarHtml.append("</td>"); } else { calendarHtml.append("<td align=/"center/" " + " /" class=Day onMouseover=/"buttonOver();/" onMouseOut=/"buttonOut();/"></td>"); } } calendarHtml.append("</tr>"); } calendarHtml.append("</table>"); return calendarHtml.toString(); } private int GetDaysOfcurrentMonth(Calendar DeSelectDate) { Calendar DeNow = DeSelectDate; int DaysOfcurrentMonth; switch (DeNow.get(Calendar.MONTH) + 1) { case 4: case 6: case 9: case 11: DaysOfcurrentMonth = 30; break; case 2: if (IsLeapYear(DeNow.get(Calendar.YEAR))) { DaysOfcurrentMonth = 29; } else { DaysOfcurrentMonth = 28; } break; default: DaysOfcurrentMonth = 31; break; } /* out.getWriter().println("<hr>"); out.getWriter().println("月份"+DeNow.get(Calendar.MONTH)); out.getWriter().println("天数"+DaysOfcurrentMonth); out.getWriter().println("<hr>");*/ return DaysOfcurrentMonth; } private boolean IsLeapYear(int year) { if (year % 4 == 0 || (year % 4 == 0 && year % 100 != 0)) { return true; } else { return false; } } private String DayOfWeek(int week) { String retStr = ""; switch (week) { case 0: return "星期日"; case 1: return "星期一"; case 2: return "星期二"; case 3: return "星期三"; case 4: return "星期四"; case 5: return "星期五"; case 6: return "星期六"; default: break; } return retStr; } } 这是前台的JSP界面的核心代码: <mce:style><!-- a{ text-decoration:none; } a:active { font-size: 12px; text-decoration: none; color: #012065; } a:hover { font-size: 12px; text-decoration: none; color: #012065; } a:link { font-size: 12px; text-decoration: none; color: #012065; } a:visited { font-size: 12px; text-decoration: none; color: #012065; } .Calendar {font-family: verdana;text-decoration: none;width: 240;background-color: #DFE5EC;font-size: 9pt;border:0px dotted #1C6FA5;} .CalendarTD {font-family: verdana;font-size: 7pt;color: #000000;background-color:#F4F8FB;height: 21px;width:11%;text-align: center;} .CTitle {font-weight: bold; font-family: verdana;text-decoration: none;width: 240;background-color: #F4F8FB;font-size: 9pt;border:0px dotted #1C6FA5;} .Day {font-family: verdana;font-size: 7pt;color:#243F65;background-color: #ffffff;height: 21px;width:12%;text-align: center;} .DaySat {font-family: verdana;font-size: 7pt;color:#000000;text-decoration: none;background-color:#EFEEF6;text-align: center;height: 21px;width: 12%;} .DaySun {font-family: verdana;font-size: 7pt;color: #000000;text-decoration: none;background-color:#EFEEF6;text-align: center;height: 21px;width: 12%;} .DayNow {font-family: verdana;font-size: 8pt;font-weight: bold; color: #000000;background-color: #C3BFFA;height: 21px;text-align: center;} .DayHoliday{font-family: verdana;font-size: 9pt; color: #000000;background-color: #F1EECF;height: 21px;text-align: center;} .DayTitle {font-family: verdana;font-size: 9pt;color: #000000;background-color: #F4F8FB;height: 21px;width:12%;text-align: center;} .DaySatTitle {font-family: verdana;font-size: 9pt;color:#FF0000;text-decoration: none;background-color:#F4F8FB;text-align: center;height: 21px;width: 12%;} .DaySunTitle {font-family: verdana;font-size: 9pt;color: #FF0000;text-decoration: none;background-color: #F4F8FB;text-align: center;height: 21px;width: 12%;} .DayButton {font-family: Webdings;font-size: 9pt;font-weight: bold;color: #243F65;cursor:hand;text-decoration: none;} --></mce:style><style mce_bogus="1">a{ text-decoration:none; } a:active { font-size: 12px; text-decoration: none; color: #012065; } a:hover { font-size: 12px; text-decoration: none; color: #012065; } a:link { font-size: 12px; text-decoration: none; color: #012065; } a:visited { font-size: 12px; text-decoration: none; color: #012065; } .Calendar {font-family: verdana;text-decoration: none;width: 240;background-color: #DFE5EC;font-size: 9pt;border:0px dotted #1C6FA5;} .CalendarTD {font-family: verdana;font-size: 7pt;color: #000000;background-color:#F4F8FB;height: 21px;width:11%;text-align: center;} .CTitle {font-weight: bold; font-family: verdana;text-decoration: none;width: 240;background-color: #F4F8FB;font-size: 9pt;border:0px dotted #1C6FA5;} .Day {font-family: verdana;font-size: 7pt;color:#243F65;background-color: #ffffff;height: 21px;width:12%;text-align: center;} .DaySat {font-family: verdana;font-size: 7pt;color:#000000;text-decoration: none;background-color:#EFEEF6;text-align: center;height: 21px;width: 12%;} .DaySun {font-family: verdana;font-size: 7pt;color: #000000;text-decoration: none;background-color:#EFEEF6;text-align: center;height: 21px;width: 12%;} .DayNow {font-family: verdana;font-size: 8pt;font-weight: bold; color: #000000;background-color: #C3BFFA;height: 21px;text-align: center;} .DayHoliday{font-family: verdana;font-size: 9pt; color: #000000;background-color: #F1EECF;height: 21px;text-align: center;} .DayTitle {font-family: verdana;font-size: 9pt;color: #000000;background-color: #F4F8FB;height: 21px;width:12%;text-align: center;} .DaySatTitle {font-family: verdana;font-size: 9pt;color:#FF0000;text-decoration: none;background-color:#F4F8FB;text-align: center;height: 21px;width: 12%;} .DaySunTitle {font-family: verdana;font-size: 9pt;color: #FF0000;text-decoration: none;background-color: #F4F8FB;text-align: center;height: 21px;width: 12%;} .DayButton {font-family: Webdings;font-size: 9pt;font-weight: bold;color: #243F65;cursor:hand;text-decoration: none;} </style> <Script LANGUAGE="JavaScript"> function subMonth() { var month=Math.ceil(document.getElementById('month').innerText) var year=Math.ceil(document.getElementById('year').innerText); if ((month-1)<1) { month=12; year=year-1; } else { month=month-1; } addElement('_year', year); addElement('_month', month); document.getElementById('form1').submit(); } function addMonth() { var month=Math.ceil(document.getElementById('month').innerText) var year=Math.ceil(document.getElementById('year').innerText); if((month+1)>12) { month=1; year=year+1; } else { month=month+1; } addElement('_year', year); addElement('_month', month); document.getElementById('form1').submit(); } function buttonOver() { var obj = window.event.srcElement; obj.runtimeStyle.cssText = "background-color:#FFFFFF"; // obj.className="Hover"; } function buttonOut() { var obj = window.event.srcElement; window.setTimeout(function(){obj.runtimeStyle.cssText = "";},300); } </Script> <form method="POST" id="form1" action="<portletAPI:createURI></portletAPI:createURI>" style="margin:0px"> <% out.print(request.getAttribute("grid")); %> </form>