2011-10-02 59 views
2

經過大量研究,我一直無法找到解決方案。JSP with Servlet - Bean無法將日期值轉換爲輸入字段

我有一個JSP頁面,由我設置在Google App Engine上運行的servlet支持。我創建了一個bean(客戶端),以方便在JSP和servlet之間傳遞我的表單字段。這在大多數情況下一直工作正常。

作爲我的servlet的一部分,我對用戶輸入的表單值進行了一些驗證。如果存在驗證錯誤,我使用RequestDispatcher將請求轉發回JSP頁面,以便顯示錯誤消息。發生這種情況時,我得到以下錯誤:

org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to convert string "02-10-2011" to class "java.util.Date" for attribute "appointmentDate": Property Editor not registered with the PropertyEditorManager

這裏是我的代碼段可能會感興趣:

public class Client { 
    public Client() {} 
    private long clientId; 
    private String name; 
    private String address; 
    private String homePhone; 
    private String cellPhone; 
    private String workPhone; 
    private String fax; 
    private String city; 
    private String postalCode; 
    private String emailAddress; 
    private String directions; 
    private String style; 
    private String decoratingThemes; 
    private String comments; 
    private String referralSource; 
    private boolean emailList; 
    private Date appointmentDate; 
    public long getClientId() { 
     return clientId; 
    } 
    public void setClientId(long clientId) { 
     this.clientId = clientId; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getAddress() { 
     return address; 
    } 
    public void setAddress(String address) { 
     this.address = address; 
    } 
    public String getHomePhone() { 
     return homePhone; 
    } 
    public void setHomePhone(String homePhone) { 
     this.homePhone = homePhone; 
    } 
    public String getCellPhone() { 
     return cellPhone; 
    } 
    public void setCellPhone(String cellPhone) { 
     this.cellPhone = cellPhone; 
    } 
    public String getWorkPhone() { 
     return workPhone; 
    } 
    public void setWorkPhone(String workPhone) { 
     this.workPhone = workPhone; 
    } 
    public String getFax() { 
     return fax; 
    } 
    public void setFax(String fax) { 
     this.fax = fax; 
    } 
    public String getCity() { 
     return city; 
    } 
    public void setCity(String city) { 
     this.city = city; 
    } 
    public String getPostalCode() { 
     return postalCode; 
    } 
    public void setPostalCode(String postalCode) { 
     this.postalCode = postalCode; 
    } 
    public String getEmailAddress() { 
     return emailAddress; 
    } 
    public void setEmailAddress(String emailAddress) { 
     this.emailAddress = emailAddress; 
    } 
    public String getDirections() { 
     return directions; 
    } 
    public void setDirections(String directions) { 
     this.directions = directions; 
    } 
    public String getStyle() { 
     return style; 
    } 
    public void setStyle(String style) { 
     this.style = style; 
    } 
    public String getDecoratingThemes() { 
     return decoratingThemes; 
    } 
    public void setDecoratingThemes(String decoratingThemes) { 
     this.decoratingThemes = decoratingThemes; 
    } 
    public String getComments() { 
     return comments; 
    } 
    public void setComments(String comments) { 
     this.comments = comments; 
    } 
    public String getReferralSource() { 
     return referralSource; 
    } 
    public void setReferralSource(String referralSource) { 
     this.referralSource = referralSource; 
    } 
    public boolean isEmailList() { 
     return emailList; 
    } 
    public void setEmailList(boolean emailList) { 
     this.emailList = emailList; 
    } 
    public Date getAppointmentDate() { 
     return appointmentDate; 
    } 
    public void setAppointmentDate(Date appointmentDate) { 
     this.appointmentDate = appointmentDate; 
    } 
} 

頁面上的Bean聲明:

<jsp:useBean id="Client" class="com.HC.RaveDesigns.Entity.Client" scope="session"/> 

轉發請求的方法。

private void dispatchError(String error, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{ 
    req.setAttribute("error",error); 

    RequestDispatcher rd = req.getRequestDispatcher("ManageClient.jsp"); 
    rd.forward(req,resp); 
} 

回答

2

這是因爲用戶發送您的字符串不是日期,這是你的工作,這個文本轉換成日期。

最快的修復將是:

  • 改變此setter裏面的參數類型二傳手類型爲String
  • 字符串轉換爲日期。

例子:

public void setAppointmentDate(String appointmentDate) { 
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); 
    this.appointmentDate = df.parse(appointmentDate); 
} 

此外,您應該以同樣的方式改變吸氣或使用FMT:formatDate像@duffymo已建議。還記得辦理日期解析異常 - 決不相信用戶輸入

+0

這幫助。原來的例外現在消失了。不過,我還必須將getter改爲鍵入String,否則setter似乎根本不會被調用。我大概可以完成這項工作,但它確實會將我的bean與我的Google App Engine對象(其預約日期類型爲Date)進行競爭。有沒有另一種方法可以解決這個問題?感謝你的幫助。 – Harley

0

使用JSTL <fmt:formatDate>在你的JSP。你應該使用JSTL。

您需要使用日期格式到字符串解析成java.util.Date:

DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 
formatter.setLenient(false); 
Date d = formatter.parse("02-10-2011"); 
+0

我在我的jsp中使用''。這似乎工作正常的初始職位,但不是當轉發請求時。 – Harley