我有一个Calendar要传递给JSP并使用JSTL格式化为我自己的自定义格式的对象:
Calendar
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:forEach items="${employees}" var="employee" varStatus="loop"> <fmt:parseDate value="${employee.startDate}" var="parsedStartDate" pattern="HH:mm dd/MM/yyyy z" /> <fmt:parseDate value="${employee.endDate}" var="parsedEndDate" pattern="HH:mm dd/MM/yyyy z" /> var contentString = '<div id="content">' + '<h1 id="firstHeading" class="firstHeading" style="font-size: 11px;"><p style="color: green">${parsedStartDate}</p> to <p style="color: orange">${parsedEndDate}</p></h1>' + '<div id="bodyContent">' + '<p style="font-size: 14px;">${employee.details}</p>' + '</div>' + '</div>'; </c:forEach>
该employee对象是标准Calendar.getInstance();对象。
employee
Calendar.getInstance();
编辑,完全例外:
java.text.ParseException: Unparseable date: "java.util.GregorianCalendar[time=1417392000000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2014,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=335,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]" at java.text.DateFormat.parse(DateFormat.java:366) at org.apache.taglibs.standard.tag.common.fmt.ParseDateSupport.doEndTag(ParseDateSupport.java:187) at org.apache.jsp.index_jsp._jspx_meth_fmt_005fparseDate_005f0(index_jsp.java:459) at org.apache.jsp.index_jsp._jspx_meth_c_005fforEach_005f0(index_jsp.java:405) at org.apache.jsp.index_jsp._jspService(index_jsp.java:142) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302) at com.example.controller.EmployeeController.doGet(EmployeeController.java:48) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:745)
at com.example.controller.EmployeeController.doGet(EmployeeController.java:48) 是: request.getRequestDispatcher("/index.jsp").forward(request, response);
at com.example.controller.EmployeeController.doGet(EmployeeController.java:48)
request.getRequestDispatcher("/index.jsp").forward(request, response);
parseDate用于解析字符串输入并转换为Date对象。您不应该使用parseDate。而是使用formatDate
<c:forEach items="${employees}" var="employee" varStatus="loop"> var contentString = '<div id="content">' + '<h1 id="firstHeading" class="firstHeading" style="font-size: 11px;"><p style="color: green"> <fmt:formatDate type="time" value="${employee.startDate.time}" pattern="HH:mm dd/MM/yyyy z"/></p> to <p style="color: orange"> <fmt:formatDate type="time" value="${employee.endDate.time}" pattern="HH:mm dd/MM/yyyy z" /></p></h1>' + '<div id="bodyContent">' + '<p style="font-size: 14px;">${employee.details}</p>' + '</div>' + '</div>'; </c:forEach>