简体   繁体   中英

Where do I specify default values for a Java bean that holds meta data to format a Month Calendar

I have a Java bean which stores what I am calling meta data (week start day, any holidays) that my JSP view will use to display a Calendar Month. I am using JSTL, not EL, since company only has jsp 1.2

  <table align="center" border="1" cellpadding="3" cellspacing="0" width="100%" class="tableInternalBorder">
     <tbody>
        <tr>
           <th width="180px" class="optionYellow">Sun</th>
           <th width="180px">Mon</th>
           <th width="180px">Tue</th>
           <th width="180px">Wed</th>
           <th width="180px">Thu</th>
           <th width="180px">Fri</th>
           <th width="180px" class="optionYellow">Sat</th>
        </tr>

        <c:forEach var="week" begin="1" end="${calendar.totalWeeks}" varStatus="status">
           <tr>
           <c:forEach var="cell" begin="${1+7*(week-1)}" end="${7+7*(week-1)}" step="1" varStatus="status"><c:set var="dayNo" value="${cell-calendar.weekStartDay+1}" />
               <c:choose><c:when test="${calendar.weekStartDay>cell || (cell-calendar.weekStartDay+1)>calendar.totalDays}">
               <td height="50" class="<c:out value="${calendar.cellColor[cell]}" />">*&nbsp;</td>
               </c:when>
               <c:otherwise>
               <td valign="Top" height="75px" class="<c:out value="${calendar.cellColor[dayNo]}" />"><span class="calDayNo"><c:out value="${dayNo}" /></span><span class="calDayName"> <c:out value="${calendar.holidayName[dayNo]}" /></span><br>
               <c:forEach var="dayEvent" items="${eventMap.byDay[dayNo]}" varStatus="status"><div class="eventContent" ><c:out value="${status.count}" />) <c:out value="${dayEvent.event_type_name}" />: <c:out value="${dayEvent.eventUser.lastName}" /></div></c:forEach></td>
               </c:otherwise>
               </c:choose>
           </c:forEach>
           </tr>
        </c:forEach>
     </tbody>
  </table>

In this bean I also storing an array of month names so that I can do this in my view.

<c:forEach var="month" items="${calendar.monthList}" varStatus="status">
    <option value="<c:out value="${status.index}" />" <c:if test="${month == calendar.monthName}">selected</c:if>><c:out value="${month}" /></option>
</c:forEach>

My question is do I set the month list in my javabean or the method which generates the meta data for the calendar. Here is the method that returns to meta data bean (with logic removed for clarity). Should I set it in this method, this class, or in the Java bean. If it should go in the Java bean, I am not sure how to do that.

public EBCalendar getCalendarMeta (HttpServletRequest request) {
    //Get any request parameters
    int iYear = STKStringUtils.nullIntconv(request.getParameter("iYear"));
    int iMonth = STKStringUtils.nullIntconv(request.getParameter("iMonth"));
    EBCalendar ebCal = new EBCalendar();

// Get the current month and year
    Calendar ca = new GregorianCalendar();
    int curYear = ca.get(Calendar.YEAR);
    int curMonth = ca.get(Calendar.MONTH);

// If request parameters are null use todays calendar
    if (iYear == 0) {
        iYear = curYear;
        iMonth = curMonth;
    }
    ebCal.setCurYear(curYear);
    ebCal.setCurMonth(curMonth);
    ebCal.setSelYear(iYear);
    ebCal.setSelMonth(iMonth);

    ebCal.setTotalWeeks(iTotalweeks);
    ebCal.setTotalDays(totalDays);
    ebCal.setWeekStartDay(weekStartDay);
    ebCal.setAbvMonthName(abvMonthName);
    ebCal.setMonthName(monthName);
    ebCal.setMonthList(monthList);
    ebCal.setHolidayName(getEBHolidayNameInMonth(iYear, iMonth));
    ebCal.setCellColor(getWeekendHolidayColorMap(iYear, iMonth));

    return ebCal;
}

EDIT: Here is what I did based on the accepted answer

I changed this:

<c:forEach var="month" items="${calendar.monthList}" varStatus="status">
    <option value="<c:out value="${status.index}" />" <c:if test="${month == calendar.monthName}">selected</c:if>><c:out value="${month}" /></option>
</c:forEach>

to this:

<c:set var="months">January,February,March,April,May,June,July,August,September,October,November,December</c:set>
<c:forTokens var="month" items="${months}" delims="," varStatus="status">
  <option value="<c:out value="${status.index}" />" <c:if test="${month == calendar.monthName}">selected</c:if>><c:out value="${month}" /></option>
</c:forTokens>

I think the question here is should you put the default with the view or the model. I am a bigger fan of putting the default with the model because you may have a different jsp that will have different defaults in the future. Then you have to go back and change the default in your model. I vote for in JSP. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM