Hi Everybody!!, Im facing a problem with the BeanUtils .populate method, I have a Form Bean SampleForm.java having 3 attributes name,lastName,phone(All Strings). The Corresponding action class is SampleAction.java , here I issue a BeanUtils.describe((SampleForm)form), I get a Map in which I get all the Values for the 3 attributes. I have a Value object SampleVO , this is a java class having the same 3 attributes as the ones in the form Bean i.e.(SampleForm.java) i.e. name,lastName,phone(All Strings), I have getter and setter methods for the same Now In my action class when I try to populate this class with the Map created from the form bean describe , I get an EXCEPTION " java.lang.reflect.InvocationTargetException: Cannot set class " Can you guide me How to go about doing this , do I need to issue explicit convert commands to convert to String? if yes can you send me some example code as to how to implement the same.
I've also included the code for my classes below. thanks in advance, regards, Sudhir ---------------------------------------------------------------------------------------------------------------------------- SampleForm.java import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import java.util.Vector; import java.util.Hashtable; import java.util.Enumeration; import java.sql.Timestamp; public final class SampleForm extends ActionForm{ private String name = ""; private String lastName = ""; private String phone = ""; public void setPhone(String phone){ this.phone = phone; } public String getPhone(){ return this.phone; } public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public void setLastName(String lastName){ this.lastName = lastName; } public String getLastName(){ return this.lastName; } /** * Reset all properties to their default values. * * @param mapping The mapping used to select this instance * @param request The servlet request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { System.out.println("reset is called"); this.name = null; this.lastName = null; this.phone = null; } /* *Validate the properties that have set from XML file and HTTPS, *and return an <code>ActionErrors</code> object that encapsulates *any validation errors that have been found.If no errors are found, *return <code>null</code> or an <code>ActionErrors</code> object with no *recorded error messages. *@param mapping The mapping used to select this instance *@param request The servlet request we are processing */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){ System.out.println("Validate method called"); ActionErrors errors = new ActionErrors(); System.out.println("name = "+name); System.out.println("lastName = "+lastName); System.out.println("phone = "+phone); return errors; }// ends validate method }//ends Class -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SampleVO.java public class SampleVO { private String name = null; private String lastName = null; private String phone = null; public void setPhone(String phone){ this.phone = phone; } public String getPhone(){ return this.phone; } public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public void setLastName(String lastName){ this.lastName = lastName; } public String getLastName(){ return this.lastName; } } ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ SampleAction.java package com.inveniotech.docset; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionServlet; import org.apache.commons.beanutils.BeanUtils; import org.apache.struts.util.MessageResources; import org.apache.commons.collections.FastHashMap; /** * @author Sudhir S. Shetty * @version $Revision: $ $Date: 03/07/2002 */ public final class SampleAction extends Action { // --------------------------------------------------------- Public Methods /** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed. * * @param mapping The ActionMapping used to select this instance * @param actionForm The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet exception occurs */ public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Extract attributes we will need System.out.println("Mapping Bean Name = "+ mapping.getAttribute()); System.out.println("Mapping Bean Name = "+ mapping.getName()); SampleVO sVO = new SampleVO(); try{ Map lmap = BeanUtils.describe((SampleForm)form); System.out.println("lmap name = "+lmap.get("name")); System.out.println("lmap lastName = "+lmap.get("lastName")); System.out.println("lmap phone = "+lmap.get("phone")); BeanUtils.populate(sVO,lmap); }catch(NoSuchMethodException ne){ System.out.println("An NoSuchMethodException During populate = "+ne.getMessage()); }catch(InvocationTargetException te){ System.out.println("An InvocationTargetException During populate = "+te.getMessage()+te.toString()); }catch(IllegalAccessException ie){ System.out.println("An IllegalAccessException During populate = "+ie.getMessage()); }catch(Exception e){ System.out.println("An Exception During populate = "+e.getMessage()); } //set New Name return (mapping.findForward("Index")); } }