带 Servlet 的 JSP - Bean 无法转换输入字段中的日期值

发布于 2024-12-07 16:11:47 字数 4225 浏览 0 评论 0原文

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

我有一个由 servlet 支持的 JSP 页面,我将其设置为在 Google App Engine 上运行。我创建了一个 bean(客户端)来促进 JSP 和 servlet 之间表单字段的传输。这在大多数情况下都运行良好。

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

org.apache.jasper.JasperException:org.apache.jasper.JasperException:无法将属性“appointmentDate”的字符串“02-10-2011”转换为类“java.util.Date”:属性编辑器未注册PropertyEditorManager

在 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);
}

After a lot of research I've been unable to find a resolution.

I have a JSP page backed by a servlet that I'm setting up to run on the Google App Engine. I've created a bean (Client) to facilitate the transfer of my form fields between the JSP and the servlet. This has been working fine in most cases.

As a part of my servlet, I do some validation on the user-entered form values. If there is validation error I use the RequestDispatcher to forward the request back to the JSP page so that an error message can be shown. When this happens, I get the following error:

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

Here are the segments of my code that may be of interest:

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;
    }
}

The bean declaration on the page:

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

The method forwarding the request.

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吝吻 2024-12-14 16:11:47

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

最快的修复方法是:

  • 将 setter 类型中的参数类型更改为 String,
  • 在此 setter 内将字符串转换为 Date。

示例:

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

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

This is because user sends you String not Date, and this is your job to convert this text into Date.

The fastest fix will be:

  • change the parameter type in setter type to String
  • convert string to Date inside this setter.

Example:

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

Additionally, you should change getter in the same way or use fmt:formatDate like @duffymo has suggested. Also remember to handle date parse exception - Never trust user input

︶葆Ⅱㄣ 2024-12-14 16:11:47

在 JSP 中使用 JSTL 。您应该使用 JSTL。

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

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

Use JSTL <fmt:formatDate> in your JSPs. You should be using JSTL.

You need to use DateFormat to parse that String into a java.util.Date:

DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
formatter.setLenient(false);
Date d = formatter.parse("02-10-2011");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文