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

发布于 2024-09-30 11:12:40 字数 4534 浏览 1 评论 0原文

我有一个 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>

在这个 bean 中,我还存储了月份名称数组,以便我可以在我的视图中执行此操作。

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

编辑:这是我根据接受的答案所做的,

我将其更改

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

为:

<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 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]}" />">* </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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵性 2024-10-07 11:12:40

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

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文