带有Servlet的JSP-Bean无法从输入字段转换Date值 [英] JSP with Servlet - Bean unable to convert Date value from input field
问题描述
经过大量研究,我一直找不到解决方法.
After a lot of research I've been unable to find a resolution.
我有一个由servlet支持的JSP页面,我将其设置为在Google App Engine上运行.我创建了一个bean(客户端),以方便在JSP和servlet之间传递表单字段.在大多数情况下,这种方法都可以正常工作.
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.
作为servlet的一部分,我对用户输入的表单值进行了一些验证.如果存在验证错误,我将使用RequestDispatcher
将请求转发回JSP页面,以便显示错误消息.发生这种情况时,出现以下错误:
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:无法将属性"appointmentDate"的字符串"02-10-2011"转换为类"java.util.Date":属性编辑器未注册PropertyEditorManager
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;
}
}
页面上的bean声明:
The bean declaration on the page:
<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);
}
推荐答案
这是因为用户向您发送的不是String,而是Date,这是您将文本转换为Date的工作.
This is because user sends you String not Date, and this is your job to convert this text into Date.
最快的解决方法是:
- 将设置类型中的参数类型更改为String
- 在此设置器中将字符串转换为Date.
示例:
public void setAppointmentDate(String appointmentDate) {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
this.appointmentDate = df.parse(appointmentDate);
}
此外,您应该以相同的方式更改getter或使用@@ duffymo建议的fmt:formatDate.还记得处理日期解析异常-从不信任用户输入
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
这篇关于带有Servlet的JSP-Bean无法从输入字段转换Date值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!