facebook

BeanUtils.copyProperties() to populate a value object

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #246832 Reply

    johnjoreilly
    Member

    I’m trying to use org.apache.commons.beanutils.BeanUtils.copyProperties() to populate a transfer/value object from a formBean. The problem is that one of the fields, a date, is not getting copied. There is no exception thrown, the result is simply null. The other fields, which are Strings and Longs, copy just fine.

    By reading what documentation I can find, it seems that this conversion, from String to java.sql.Date, is supposed to work out of the box. I wasn’t sure, though, so I registered a converter at startup using a ServletContextListener. This had no effect I could see.

    Any ideas?
    //////////////////////////////////////////////////////////////////////
    public class FindAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {

    FindForm findForm = (FindForm) form;
    Worktypet worktypet = new Worktypet();

    List list = null;
    try {
    BeanUtils.copyProperties(worktypet,findForm);
    } catch (IllegalAccessException e1) {
    e1.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }

    try {
    Context ctx = new InitialContext();
    WorktypeHome worktypeHome =
    (WorktypeHome) ctx.lookup(“ejb/Worktype”);
    Worktype worktype = worktypeHome.create();
    list = worktype.find(worktypet);

    } catch (RemoteException e) {
    e.printStackTrace();
    } catch (NamingException e) {
    e.printStackTrace();
    } catch (CreateException e) {
    e.printStackTrace();
    }
    request.setAttribute(“list”,list);
    return mapping.findForward(“success”);

    }

    }
    //////////////////////////////////////////////////////
    <html>
    <head>
    <title>JSP for findForm form</title>
    </head>
    <body>
    <html:form action=”/find”>
    worktypeid : <html:text value=”17″ property=”worktypeid” />
    <html:errors property=”worktypeid” />
    <br />
    description : <html:text value=”description” property=”description” />
    <html:errors property=”description” />
    <br />
    dictsystname : <html:text value=”dictsystname” property=”dictsystname” />
    <html:errors property=”dictsystname” />
    <br />
    flags : <html:text value=”4″ property=”flags” />
    <html:errors property=”flags” />
    <br />
    lastchange : <html:text value=”2005-04-14″ property=”lastchange” />
    <html:errors property=”lastchange” />
    <br />
    status : <html:select value=”1″ property=”status” >
    <html:option value=”1″/>1
    <html:option value=”2″/>2
    </html:select>
    <html:errors property=”status” />
    <br />
    <html:submit />
    <html:cancel />
    </html:form>
    </body>
    </html>

    ///////////////////////////////////////////////////////////
    public class Worktypet
    implements Serializable
    { …
    private java.lang.String worktypeid;
    private java.lang.Long status;
    private java.lang.String dictsystname;
    private java.lang.String description;
    private java.sql.Date lastchange;
    private java.lang.Long flags;

    //setter and getters….
    }
    //////////////////////////////////////////////////////////
    public class FindForm extends ActionForm {

    private String worktypeid;
    private String dictsystname;
    private String description;
    private String flags;
    private String lastchange;
    private String status;

    //setter and getters….
    }

    #246835 Reply

    Riyad Kalla
    Member
    #246838 Reply

    johnjoreilly
    Member

    I figured out the problem. The signature of the getter for the date in the value object was incorrect (util.Date instead of sql.Date) , so copyProperties() just ignored it. Of course you could not have seen that , since I didn’t include all the source… 🙁

    But thanks, and I’ll add that to my list of links on this general topic. I agree with the author that the subject is poorly documented.

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: BeanUtils.copyProperties() to populate a value object

You must be logged in to post in the forum log in