如何使用Apache Commons BeanUtils设置日期类型值, [英] How to set date type values using Apache Commons BeanUtils,

查看:554
本文介绍了如何使用Apache Commons BeanUtils设置日期类型值,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 enctype =multipart / form-data的Html表单。我有一个 dto 类它有所有 setter getters 。由于我提交表单作为多部分, getParameter()方法将不起作用,以处理我使用Apache Commons BeanUtils的html表单字段。我的servlet如下:

I have an Html form with enctype="multipart/form-data" . I have an dto class it has all setter and getters. Since I am submitting form as multipart, getParameter() method will not work,to process html form fields I used Apache Commons BeanUtils. My servlet is as follow,

List<FileItem> items = (List<FileItem>) new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
System.out.println(fieldname);
System.out.println(fieldvalue);
// ... (do your job here)
//getters and setters
try {if((!fieldname.equals("dob"))&&(!fieldname.equals("doj"))){
             BeanUtils.setProperty(teacherInfo, fieldname, fieldvalue);}
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

} else {

        //Code for file upload
 }

我的问题是我无法处理日期类型变量,这就是为什么我忽略在上面的代码中设置两个日期值和上面的代码中的一些html字段,值不是由Bean setProperty()方法设置的。任何人都可以告诉我我错了吗? 。

My problem is I am unable to process the date type variables , thats why I m ignoring to set two date values in above code and in above code for some html fields, value are not set by Beans setProperty() method . Can any one tell me where I am wrong . .

推荐答案

DateConverter converter = new DateConverter( null );
converter.setPattern("dd/mm/yyyy");
ConvertUtils.register(converter, Date.class);
BeanUtils.setProperty(obj, "date", "07/04/2014");

这篇关于如何使用Apache Commons BeanUtils设置日期类型值,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆