带Servlet的JSP - Bean无法从输入字段转换Date值

时间:2011-10-02 18:32:13

标签: java jsp javabeans

经过大量研究后,我一直无法找到解决方案。

我有一个由我正在设置为在Google App Engine上运行的servlet支持的JSP页面。我已经创建了一个bean(Client)来促进我在JSP和servlet之间传递表单字段。在大多数情况下,这一直很好。

作为我的servlet的一部分,我对用户输入的表单值进行了一些验证。如果存在验证错误,我使用RequestDispatcher将请求转发回JSP页面,以便显示错误消息。发生这种情况时,我收到以下错误:

  

org.apache.jasper.JasperException:org.apache.jasper.JasperException:无法将字符串“02-10-2011”转换为属性“appointmentDate”的类“java.util.Date”:属性编辑器未注册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 个答案:

答案 0 :(得分:2)

这是因为用户向您发送字符串而不是日期,这是您将此文本转换为日期的工作。

最快的修复方法是:

  • 将setter类型中的参数类型更改为String
  • 将此字符串中的字符串转换为日期。

示例:

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

此外,您应该以相同的方式更改getter或使用fmt:formatDate,如@duffymo所建议的那样。还要记住处理日期解析异常 - 永远不要信任用户输入

答案 1 :(得分:0)

在JSP中使用JSTL <fmt:formatDate>。你应该使用JSTL。

您需要使用DateFormat将该String解析为java.util.Date:

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