1

作为一名 Java 学生,我正在从事一个小型个人项目,我被要求创建一个显示 Mysql 数据库的简单网页。在 mySql 中,我以 DATE 类型声明了我的日期。请参阅下面的屏幕截图。

在此处输入图像描述

下面的 java 代码显示了我如何从我的数据库中检索数据。

private Destination resultSetRowToCursist(ResultSet resultSet)
            throws SQLException {
        return new Destination (resultSet.getInt("CountryID"),
                resultSet.getString("Destination"),
                resultSet.getDate("DepartureDate"),
                resultSet.getDate("ReturnDate"), 
                resultSet.getInt("Price"),
                resultSet.getInt("AvailableSeats"));
    }

下面是我网页上输出的截图。我网页上的 DepartureDate 和 ReturnDate 格式应反映与数据库中相同的格式

在此处输入图像描述

这是我的 JSP 代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="styles/default.css">

    <title>Travels</title>
    </head>
    <body>

    <table border=1>

            <tr>

                <th>CountryId</th>
                <th>Country</th>
                <th>DepartureDate</th>

                <th>ReturnDate</th>

                <th>Price</th>
                <th>AvailableSeats</th>

    </tr>

            <c:forEach var="destinationArrayListItem" items="${DestinationArrayList}">

                <tr>

                    <td>${destinationArrayListItem.countryID}</td>
                    <td>${destinationArrayListItem.destination}</td>
                    <td>${destinationArrayListItem.departureDate}</td>
                    <td>${destinationArrayListItem.returnDate}</td>
                    <td>${destinationArrayListItem.price}</td>
                    <td>${destinationArrayListItem.availableSeats}</td>


                </tr>

            </c:forEach>

        </table>



        <br />

        <c:url var="index" value="/IndexServlet" />

        <a class="HPbutton" href="${index}">Home Page</a>


    </body>
    </html>

我的目的地类与构造函数和吸气剂

import java.io.Serializable;
import java.sql.Date;

public class Destination implements Serializable {

    private static final long serialVersionUID = 1L;

    private int countryID;

    private String destination;

    private  Date departureDate;
    private  Date returnDate;
    private  int price;
    private  int availableSeats;


    public Destination () {
          this.countryID=0;;

          this.destination="geen";

          this.departureDate= null;
          this.returnDate= null;
          this.price=0;
          this.availableSeats=0;


    }

    public Destination(int countryID, String destination, Date departureDate,
            Date returnDate, int price, int availableSeats) {
        this.countryID = countryID;
        this.destination = destination;
        this.departureDate = departureDate;
        this.returnDate = returnDate;
        this.price = price;
        this.availableSeats = availableSeats;


    }

    public int getCountryID() {
        return countryID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public Date getDepartureDate() {
        return departureDate;
    }

    public void setDepartureDate(Date departureDate) {
        this.departureDate = departureDate;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public Date getReturnDate() {
        return returnDate;
    }

    public void setReturnDate(Date returnDate) {
        this.returnDate = returnDate;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getAvailableSeats() {
        return availableSeats;
    }

    public void setAvailableSeats(int availableSeats) {
        this.availableSeats = availableSeats;
    }


}
4

1 回答 1

1

您使用的是 javax.sql.Date 还是 java.util.Date。记住 EL 标签使用 toString() 方法,可能值得格式化日期,如下面的线程所示:

将 java.util.Date 转换为字符串

另一种方法是按以下方式使用 JSTL 标记:

<td><fmt:formatDate value="${destinationArrayListItem.departureDate}" pattern="MM/yyyy" /></td>
<td><fmt:formatDate value="${destinationArrayListItem.returnDate}" pattern="MM/yyyy" /></td>

为此,您必须将 JSTL 库添加到您的 JavaEE 应用程序,如下所示:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

请注意,这与 JSTL 核心不同。

于 2013-03-29T10:48:31.183 回答