代码之家  ›  专栏  ›  技术社区  ›  jeff

我在哪里为保存元数据以格式化月历的Java bean指定默认值

  •  0
  • jeff  · 技术社区  · 15 年前

    我有一个Java bean,它存储我所称的元数据(周开始日,任何假日),JSP视图将使用它来显示一个日历月。我使用的是JSTL,而不是EL,因为公司只有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>
    

    <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>
    

    我的问题是我是在javabean中设置月列表还是为日历生成元数据的方法。下面是返回元数据bean的方法(为了清晰起见,去掉了逻辑)。我应该在这个方法、这个类或Java bean中设置它。如果它应该放在Java bean中,我不知道该怎么做。

    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;
    }
    

    编辑:以下是我根据接受的答案所做的

    我改了这个:

    <<option value=“<c:out value=”${status.index}“/>”lt;c:if test=”${month==calendar.monthName}“>选定</c:if><c:out value=”${month}“/></option>
    

    对此:

    <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>
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   Amir Raminfar Hadi Rasouli    15 年前

    我认为这里的问题是,您应该将默认值与视图或模型放在一起。我更喜欢在模型中使用默认值,因为您可能有一个不同的jsp,将来会有不同的默认值。然后您必须返回并更改模型中的默认值。我在JSP中投了赞成票。:)

    推荐文章