我想显示两个选择日期之间的日期列表(用户选择 dateA 和 dateB ,当他点击SUBMIT时,另一个jsp向开放用表格显示表格中的日期 第一个ROW 用于标签:日期,司机,汽车,加油站 第二行用于信息; 到目前为止我已经这样做了:
index.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="datepickeer" action="showdates.jsp" method="POST">
<table>
<tr><td>Date début :</td> <td><input type = "date" name = "dateA"></td><tr>
<tr><td>Date Fin :</td> <td><input type = "date" name = "dateB"></td><tr>
<tr><td><input type = "submit" name = "submit" value = "submit"></td></tr>
</table>
</form>
</body>
</html>
showdates.jsp:
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Calendar"%>
<%@ page import = "java.util.Date,java.text.SimpleDateFormat,java.text.ParseException"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table>
<tr>
<td> Dates </td>
<td> Driver</td>
<td> Car </td>
<td> Gas station </td>
</tr>
<tr>
<td>
<%
String dateA =request.getParameter("dateA");
String dateB =request.getParameter("dateB");
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
Date dateStart = null;
Date dateEnd = null;
dateStart = formater.parse(dateA);
dateEnd = formater.parse(dateB);
Calendar c = GregorianCalendar.getInstance();
c.setTime(dateStart);
List<Date> dates = new ArrayList<Date>();
while (!c.getTime().after(dateEnd)) {
dates.add(c.getTime());
c.add(Calendar.DAY_OF_YEAR, 1);
}
for (Date element: dates)
{
SimpleDateFormat newFormater = new SimpleDateFormat("dd-MM-yyyy");
out.println(newFormater.format( element));
} %> </td>
<td> Mr alaoui </td>
<td> 235647-A-23 </td>
<td> Marrakech,MOROCCO </td>
</tr>
</table>
</html>
请帮帮我
答案 0 :(得分:0)
我希望在单元格中有一个日期,这意味着每行中有一个日期
您的代码会在单个<td>
中写下所有日期,因为您的for循环位于<td>
内,以获得您在评论中所述的输出:
<table>
<tr>
<th> Dates </th>
<th> Driver</th>
<th> Car </th>
<th> Gas station </th>
</tr>
start loop through your dates
<tr>
<td>print date</td>
<td>print Driver</td>
<td>print Car</td>
<td>print Gas station</td>
</tr>
end loop
</table>
<小时/> 但是,更好地创建一个自定义的jsp标记函数来轻松完成它并将所有java代码保存在其中而不是像jsp一样:
在dateDiff.tld
内创建${project_root}/(WebContent OR webapp)/WEB-INF/
,如:
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<short-name>DateDifference TLD</short-name>
<jsp-version>2.1</jsp-version>
<tag>
<name>GetDatesStartEnd</name>
<tag-class>com.jsp.tag.dateArrayListTagHandler.DateArrayListTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>start</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.Date</type>
</attribute>
<attribute>
<name>end</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.Date</type>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
和DateArrayListTag
类如:
package com.jsp.tag.dateArrayListTagHandler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class DateArrayListTag extends SimpleTagSupport {
private Date start;
private Date end;
private String var;
public void setVar(String var) {
this.var = var;
}
private List<Date> listDates;
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
this.end = end;
}
public List<Date> getListDates() {
return listDates;
}
public void setListDates(List<Date> listDates) {
this.listDates = listDates;
}
@Override
public void doTag() throws JspException, IOException {
System.out.println("startDate: "+start);
System.out.println("endDate: "+end);
Calendar c = GregorianCalendar.getInstance();
listDates = new ArrayList<Date>();
c.setTime(start);
while (!c.getTime().after(end)) {
listDates.add(c.getTime());
c.add(Calendar.DAY_OF_YEAR, 1);
}
getJspContext().setAttribute(var, listDates);
}
}
然后,你的showdates.jsp将会像:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/dateDiff.tld" prefix="dateDiff" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style type="text/css">
th,td
{
padding:15px;
}
</style>
</head>
<body>
<fmt:parseDate value="${param.dateA}" var="parsedDateA" pattern="yyyy-MM-dd" />
Date A:<fmt:formatDate pattern="yyyy-MM-dd" type="DATE" value="${parsedDateA}" var="dateA" />${dateA}
<fmt:parseDate value="${param.dateB}" var="parsedDateB" pattern="yyyy-MM-dd" />
Date B:<fmt:formatDate pattern="yyyy-MM-dd" type="DATE" value="${parsedDateB}" var="dateB" />${dateB}
<dateDiff:GetDatesStartEnd end="${parsedDateB}" start="${parsedDateA}" var="resultDates"/>
<table>
<tr>
<th> Dates </th>
<th> Driver</th>
<th> Car </th>
<th> Gas station </th>
</tr>
<c:forEach items="${resultDates}" var="curDt">
<tr>
<td>
<fmt:formatDate pattern="yyyy-MM-dd" type="DATE" value="${curDt}" />
</td>
<td>
Mr alaoui
</td>
<td>
235647-A-23
</td>
<td>
Marrakech,MOROCCO
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
jstl.x.x.jar
和jsp-api.jar
文件应该在您的项目构建路径中可用。