开发者

JSP with Servlet - Bean unable to convert Date value from input field

开发者 https://www.devze.com 2023-04-09 19:40 出处:网络
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

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) {
        thi开发者_如何学Cs.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);
}


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


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号