struts 2中的bean属性和modelDriven [英] beans properties and modelDriven in struts 2
问题描述
我是 struts 2 的新手.我创建了一个动作类,它使用 ModelDriven
接口将数据从 JSP 页面插入到 bean.
I am new to struts 2. I created an action class that insert data from JSP page to a bean using ModelDriven
interface.
问题是我在 bean 中有一些非 'String 属性,例如
Long、
Date` ...等问题是:当我在 jsp 页面中按下提交按钮时,我收到一条错误消息,指出它没有找到该特定属性的字符串设置器.例如,如果我的 bean 中有<代码>
The problem is that i have some non 'Stringproperties in the bean like
Long,
Date` ... etc
The problem is:
when i press submit button in the jsp page i get an error saying that it did not find the a string setter for that particular property.
for example if i have in my bean
package com.hsms.aseza.enteringApproval
Class EnteringApproval
private Date approvalDate
Date getApprovalDate()
{
return employeeId;
}
void setApprovalDate(Date employeeId)
{
this.employeeId = employeeId;
}
当实现模型驱动的动作类被执行时,它会触发一个运行时异常
when the action class that implement the model driven is executed, it fires a run time exception
java.lang.NoSuchMethodException: com.hsms.aseza.enteringApproval.EnteringApproval.setApprovalDate([Ljava.lang.String;)
有没有办法在不为我的 Date
属性编写字符串设置器的情况下解决这个问题?
Is there anyway to solve this problem without writing a String setter for my Date
property?
推荐答案
我认为您的问题是转换,即从 String 转换为您的 java.util.Date
对象.这个类扩展 this 负责从 String 转换为 Long 等其他类型, Double 等.如果您查看 DefaultTypeConverter 的源代码,您不会看到 java.util.Date
或 java.sql.Date
的任何转换.所以我认为你应该做的是为 approvalDate
编写一个转换器.我的上一篇文章 将指导您完成该过程,您所需要的只是编辑代码以满足您的需要.
I think that your problem is conversion i.e. conversion from String to your java.util.Date
Object. This class extends this which is responsible for converting from String to other types like Long, Double etc. If you check the source code for DefaultTypeConverter, you won't see any conversion for either java.util.Date
or java.sql.Date
. So i think what you should do is to write a converter for approvalDate
. My previous post on this will guide you on the procedure, all you will need is to edit the code to suit your needs.
在您的 jsp 上,按照 this datepicker 示例使用与 SimpleDateFormat 在转换器的 convertFromString
方法中进行转换并返回转换后的 java.util.Date
或 java.sql.Date
.
On your jsp, follow this datepicker example
Use the same format i.e date format used to display your date in your jsp with SimpleDateFormat to do your conversion in convertFromString
method of your converter and return the converted java.util.Date
or java.sql.Date
.
如果您在执行此操作时遇到问题,请告诉我.
Let me know if you have issues implementing this.
这篇关于struts 2中的bean属性和modelDriven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!