JSF无法将给定对象格式化为日期

时间:2011-11-16 12:28:31

标签: java jsf date

我写了一个简单的表单和托管bean来存储提交给SQL服务器的数据。下载了sqljdbc并安装了它。发生以下情况:

javax.servlet.ServletException: Cannot format given Object as a Date

不确定到底发生了什么......

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.prime.com.tr/ui"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Reservation Request</title>
</h:head>
<body>
<h:form>
    <h2>Event Details:</h2>
    <table cellspacing="10">
        <tr>
            <td>
                Event Title:
            </td>
            <td>
        <p:inputText id="eventTitle" value="#{formData.eventTitle}"/>
            </td>
        </tr>
    </table>
    <table>
        <tr>
            <td>
                <p:tab title="startDate">
                     <h:panelGrid columns="2" cellpadding="10">
                      <label>Start Date</label>
                      <p:calendar value="#{formData.startDate}}" required="true" />
                      <h:message for="startDate" />
                     </h:panelGrid>
                    </p:tab>
            </td>
            <td>
                 <p:tab title="endDate">
                     <h:panelGrid columns="2" cellpadding="10">
                      <label>End Date</label>
                      <p:calendar value="#{formData.endDate}" required="true" />
                      <h:message for="endDate" />
                     </h:panelGrid>
                    </p:tab>
            </td>
        </tr>
        <tr>
            <td>
                Request Type/Purpose:
            </td>
        </tr>
        <tr>
            <td>

                <h:selectOneMenu id="purposeOption"
                required="true"
                value="#{formData.requestType}">
                <f:selectItem
                    itemValue="#{formData.projectOption}"
                    itemLabel="Project"/>
                <f:selectItem
                    itemValue="#{formData.trainingOption}"
                    itemLabel="Training"/>
                </h:selectOneMenu>
            </td>
        </tr>
    </table>
    <table cellspacing="10">
        <tr>
            <td>
                Workstations Required:
            </td>
            <td>
        <p:inputText value="#{formData.terminalsRequired}" id="terminals"/>
            </td>
        </tr>
    </table>
    <br></br>
    <h2>Requester Information:</h2>
    <table cellspacing="10">
        <tr>
            <td>
                Last Name:
            </td>
            <td>
                <p:inputText value="#{formData.lastName}" id="lastName"/>
            </td>
             <td>
                First Name:
            </td>
            <td>
                <p:inputText value="#{formData.firstName}" id="firstName"/>
            </td>
             <td>
                M.I:
            </td>
            <td>
            <p:inputText value="#{formData.middleInitials}" id="middleInitials"/>
            </td>
        </tr>
        <tr>
            <td>
                Badge:
            </td>
            <td>
        <p:inputText value="#{formData.badgeNo}" id="badgeNo"/>
            </td>
            <td>
                Network ID:
            </td>
            <td>
            <p:inputText value="#{formData.networkID}" id="networkID"/>
            </td>
            <td>
                Telephone:
            </td>
            <td>
            <p:inputText value="#{formData.telephoneNo}" id="telephoneNo"/>
            </td>
        </tr>
        <tr>
            <td>
                Org Code:
            </td>
            <td>
        <p:inputText value="#{formData.orgCode}" id="orgCode"/>
            </td>
            <td>
                Org Name:
            </td>
            <td>
               <p:inputText value="#{formData.orgName}" id="orgname"/>
            </td>
        </tr>
    </table>
    <br></br>
    <table>
        <tr>
            <td>
                <b>Justification:</b>
            </td>
        </tr>
         <tr>
            <td>
         <p:inputTextarea value="#{formData.justification}" id="justification" cols="80" rows="10" />
            </td>
        </tr>
        <tr>
            <td>
        <h:commandButton value="Submit Data!" action="#{formData.storeTheData()}"/>
            </td>
        </tr>
    </table>
</h:form>
</body>
</html>

并且支持bean是:

package core.smd.classes;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.util.Date;
import java.sql.*;
/**
*
* @author 
*/
@ManagedBean
@SessionScoped
public class FormData {
//global variables
String eventTitle;
Date startDate;
Date endDate;
String requestType;
int terminalsRequired;
String lastName;
String firstName;
String middleInitials;
int badgeNo;
String networkID;
String telephoneNo;
String orgCode;
String orgName;
String trainingOption;
String projectOption;
String justification;
/*End of variables declaration*/

/** Creates a new instance of FormData */
public FormData() {
}

public int getBadgeNo() {
    return badgeNo;
}

public String getJustification() {
    return justification;
}

public void setJustification(String justification) {
    this.justification = justification;
}


public void setBadgeNo(int badgeNo) {
    this.badgeNo = badgeNo;
}

public String getProjectOption() {
    return projectOption;
}

public void setProjectOption(String projectOption) {
    this.projectOption = projectOption;
}

public String getTrainingOption() {
    return trainingOption;
}

public void setTrainingOption(String trainingOption) {
    this.trainingOption = trainingOption;
}



public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public String getEventTitle() {
    return eventTitle;
}

public void setEventTitle(String eventTitle) {
    this.eventTitle = eventTitle;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getMiddleInitials() {
    return middleInitials;
}

public void setMiddleInitials(String middleInitials) {
    this.middleInitials = middleInitials;
}

public String getNetworkID() {
    return networkID;
}

public void setNetworkID(String networkID) {
    this.networkID = networkID;
}

public String getOrgCode() {
    return orgCode;
}

public void setOrgCode(String orgCode) {
    this.orgCode = orgCode;
}

public String getOrgName() {
    return orgName;
}

public void setOrgName(String orgName) {
    this.orgName = orgName;
}

public String getRequestType() {
    return requestType;
}

public void setRequestType(String requestType) {
    this.requestType = requestType;
}

public Date getStartDate() {
    return startDate;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

public String getTelephoneNo() {
    return telephoneNo;
}

public void setTelephoneNo(String telephoneNo) {
    this.telephoneNo = telephoneNo;
}

public int getTerminalsRequired() {
    return terminalsRequired;
}

public void setTerminalsRequired(int terminalsRequired) {
    this.terminalsRequired = terminalsRequired;
}

public void storeTheData(){
    //prepared statenebts
    PreparedStatement preStmt = null; 


    try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
        "xxxx;user=sa;password=xxxxx;";
    Connection con = DriverManager.getConnection(connectionUrl);
    String sqlQuery = "INSERT INTO Pending_Requests(Request_No, Request_Details, Network_ID, Request_Status) VALUES(?, ?, ?, ?)";
    preStmt = con.prepareStatement(sqlQuery);
    preStmt.setString(1, telephoneNo);
    preStmt.setString(2, eventTitle);
    preStmt.setString(3, networkID);
    preStmt.setString(4, eventTitle);
    preStmt.executeUpdate();
    con.commit();

    } catch (SQLException e) {
        System.out.println("SQL Exception: "+ e.toString());
    } catch (ClassNotFoundException cE) {
        System.out.println("Class Not Found Exception: "+ cE.toString());
    }
}
}

我知道提交的数据实际上并未完全存储,但我只测试SQL qorks和preparedStatement ...因此只存储部分提交的数据。 谢谢,

1 个答案:

答案 0 :(得分:1)

尝试使用日期时间转换器。

<f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="GMT+5" />

添加你自己的时区,这是一个工作片段

<p:calendar id="end" value="#{message.endDate}" mode="popup" pattern="dd/MM/yyyy HH:mm" size="17">
                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="GMT+5" />
                                </p:calendar>