RE: Please Help - ClassCastException
I've been responding to your questions here and at JavaRanch on this subject. It seems like you are missing the point of DynaActionForms. Please read 4.3 and 4.4 of the user guide. http://jakarta.apache.org/struts/userGuide/building_controller.html Lots of additional comments below. Comments below look for * -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Sunday, January 04, 2004 10:18 PM To: Struts Users Mailing List Subject: RE: Please Help - ClassCastException Allow me to ask three more questions: 1. If I want to reset some specific text fields when the JSP is re-displayed, do I code the PostForm.java like this (please confirm): code: - import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.action.ActionMapping; import javax.servlet.http.HttpServletRequest; public class PostForm extends DynaValidatorForm { public void reset(ActionMapping mapping, HttpServletRequest request) { super.reset( mapping, request ); set( receiver, new String() ); set( sender, new String() ); } public PostForm () {} } *** Why not just use the intial attribute of the form-property element? --- 2. if I want to reset all the text fields when the JSP is re-displayed, do I code like this (please confirm): code: --- import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.action.ActionMapping; import javax.servlet.http.HttpServletRequest; public class PostForm extends DynaValidatorForm { public void reset(ActionMapping mapping, HttpServletRequest request) { initialize( mapping ); } public PostForm () {} } -- *** Why not just use the intial attribute of the form-property element? 3. What is the difference between code: --- form-bean name=postForm type=package.package.package.PostForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ /form-bean --- AND code: -- form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ /form-bean --- Thank you very much. *** The second one makes sense. Why do you need the PostForm class? see my answer to a similar question of yours at JavaRanch http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topicf=58t= 001820 Here is a snippet of some tutorial I am working on that might help. DynaActionForms are universally loved and hated. In teaching, consulting Struts and developing with Struts, I have found that DynaActionForms are either embraced or rejected. The idea behind DynaActionForms is that instead of creating an ActionForm per form, you instead configure an ActionForm per form. Advantages of DynaActionForms: Some folks feel creating an ActionForm class for each HTML form in your Struts application is time-consuming, causes maintenance problems, and is frustrating. With DynaActionForm classes you don't have to create an ActionForm subclass for each form and a bean property for each field. Instead you configure an DynaActionForm its properties, type, and defaults in the Struts configuration file. snip To configure a DynaActionForm in struts-config.xml, you use a form-bean element as with normal ActionForm. The type of the form bean must be org.apache.struts.action.DynaActionForm or a derived class. You then add form-property elements to declare the properties of the form. To use a DynaActionForm add the following to form-beans element (example): form-bean name=userRegistrationDynaForm type=org.apache.struts.action.DynaActionForm form-property name=userName type=java.lang.String / form-property name=email type=java.lang.String / form-property name=password type=java.lang.String / form-property name=passwordCheck type=java.lang.String / form-property name=firstName type=java.lang.String / form-property name=lastName type=java.lang.String / form-property name=phone type=java.lang.String initial=(520) / form-property name=fax type=java.lang.String / form-property name=page type
RE: Please Help - ClassCastException
I am still confused... I have a jsp that provides text fields for user to fill out information. All the information is passed via HttpRequest and to be validated. And all the properties in my PostForm are populated by the information retrieved from those text fields. And my PostForm is of type=org.apache.struts.validator.DynaValidatorForm Therefore, in my PostForm.java, I should do something like this? I just do not think the following code is correct. == import org.apache.struts.validator.DynaValidatorForm; public class PostForm extends DynaValidatorForm { private String receiver; private String sender; public void set(String receiver, String receiver); public void set(String sender, String sender); public String get(String receiver, String receiver); public String get(String sender, String sender); } == And you are saying that in my action class, I should ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostForm; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); ActionForm postForm = ( ActionForm )form; ThreadHandler thandler = new ThreadHandler(); ThreadBean threadBean = new ThreadBean(); BeanUtils.copyProperties( threadBean, postForm ); if (parentPostID == 0 ) // new topic { threadBean.setLastPostMemberName( memberName ); threadBean.setThreadCreationDate( now ); . . threadID = thandler.insertThread( threadBean ); } ... ... } } --- Joe Hertz [EMAIL PROTECTED] wrote: Well, first off: In your (dyna) form, you don't create setters and getters for the properties though. With DynaForms you would say set(myPropertyName, myString) instead of calling setMyPropertyName(myString). This is the Dyna part of DynaForms. It's much less tedious IMHO. In your action, you absolutely want to cast the form to PostForm. Otherwise the form variable has no way to know properties are associated with it. So yes, once you cast it to PostForm calls to BeanUtils.copyProperties() can and will work properly. Hope this helps, -Joe -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Sunday, January 04, 2004 1:42 AM To: Struts Users Mailing List Subject: RE: Please Help - ClassCastException I think that there are a lot more mistakes in my code than I originally thought. The root of the problem is that I do not know how to use DynaValidatorForm. If you could help me in learning how to code when I am working with DynaValidatorForm. 1. in my struts-config.xml, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ . . /form-bean 2. My PostForm.java is like: import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.action.ActionMapping; public class PostForm extends DynaValidatorForm { private String receiver; private String sender; .. public void setReceiver( String receiver ) { this.receiver = receiver; } public void setSender( String sender ) { this.sender = sender; } public String getReceiver() { return receiver; } public String getSender() { return sender; } . . } 3. in my action class (see the code below) 3.1. do I cast the form to DynaActionForm? or I should cast the form to DynaValidatorForm? 3.2. Can I use the copyProperties() method of the BeanUtils to convert the form to a bean? BeanUtils.copyProperties
RE: Please Help - ClassCastException
Not at all. First off, set(String, String) is predefined for you. You don't need to create any of that. In fact, what you will find in a DynaValidatorForm is that your form classes start to get very sparse. Here's really what it could look like. public class PostForm extends BaseDynaForm { public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) { // throw new UnsupportedOperationException(Method is not implemented); } public PostForm () { } } In your ActionForm itself, beanUtils would work something like this. All you need is to get the right formBean object. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { PostForm pForm = (PostForm) form; // other code you write here BeanUtils.copyProperties(yourBean, pForm); // etc Doing it yourself without beanUtils? Instead of calling getSender() and having a String returned, you call get (sender) and it will return an Object (cast it yourself to whatever type the sender form property really is). I made a base form class with a getString method that calls get() and does the String cast for me to save the tediom. Making sense now? -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Sunday, January 04, 2004 2:46 AM To: Struts Users Mailing List Subject: RE: Please Help - ClassCastException I am still confused... I have a jsp that provides text fields for user to fill out information. All the information is passed via HttpRequest and to be validated. And all the properties in my PostForm are populated by the information retrieved from those text fields. And my PostForm is of type=org.apache.struts.validator.DynaValidatorForm Therefore, in my PostForm.java, I should do something like this? I just do not think the following code is correct. == import org.apache.struts.validator.DynaValidatorForm; public class PostForm extends DynaValidatorForm { private String receiver; private String sender; public void set(String receiver, String receiver); public void set(String sender, String sender); public String get(String receiver, String receiver); public String get(String sender, String sender); } == And you are saying that in my action class, I should ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostForm; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); ActionForm postForm = ( ActionForm )form; ThreadHandler thandler = new ThreadHandler(); ThreadBean threadBean = new ThreadBean(); BeanUtils.copyProperties( threadBean, postForm ); if (parentPostID == 0 ) // new topic { threadBean.setLastPostMemberName( memberName ); threadBean.setThreadCreationDate( now ); . . threadID = thandler.insertThread( threadBean ); } ... ... } } --- Joe Hertz [EMAIL PROTECTED] wrote: Well, first off: In your (dyna) form, you don't create setters and getters for the properties though. With DynaForms you would say set(myPropertyName, myString) instead of calling setMyPropertyName(myString). This is the Dyna part of DynaForms. It's much less tedious IMHO. In your action, you absolutely want to cast the form to PostForm. Otherwise the form variable has no way to know properties are associated with it. So yes, once you cast it to PostForm calls to BeanUtils.copyProperties() can and will work properly. Hope this helps, -Joe -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Sunday, January 04, 2004 1:42 AM To: Struts Users Mailing List Subject: RE
Re: Please Help - ClassCastException
On 04/01/2004 05:04, Caroline Jen [EMAIL PROTECTED] wrote: Thank you for trying to help. I have added import org.apache.artimus.message.PostForm; to my action class. I do not fully follow what I should check in the struts-config.xml file. And should I use name=postForm with lowercase 'p' or uppercase 'P'? In my struts-config.xml file, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm Here is the problem. Replace org.apache.struts.validator.DynaValidatorForm for org.apache.artimus.message.PostForm That should work Pedro Salgado form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ form-property name=title type=java.lang.String/ form-property name=postTopic type=java.lang.String/ form-property name=postBody type=java.lang.String/ /form-bean and action roles=administrator,editor,contributor path=/message/NewTopic type=org.apache.artimus.message.StoreMessage name=postForm scope=request validate=true input=.message.Form forward name=success path=.article.View/ /action Do you see any problems? --- Pedro Salgado [EMAIL PROTECTED] wrote: On your struts config file check if the form bean for StoreMessage action is of type pkg.pkg.PostForm and if the action name is pointing to the correct form bean... It also seems to be missing the import of the PostForm on your action class. Pedro Salgado On 04/01/2004 03:22, Caroline Jen [EMAIL PROTECTED] wrote: The statement shown below encountered a ClassCastException: PostForm postForm = ( PostForm )form; I cannot figure out the reason. Please help. Allow me to show more code of the class where the exception occurred: ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; int postID; String postCreationIP; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); PostForm postForm = ( PostForm )form; } } __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Please Help - ClassCastException
This passage is straight from the struts javadoc The key passed into the validator is the action element's 'name' attribute from the struts-config.xml which should match the form element's name attribute in the validation.xml Take a peek at how to configure struts-config.xml at http://www.reumann.net/do/struts/lesson3/step4 which details the form bean name type (with the fully qualified package name) and the column form-property Keep me apprised, Martin - Original Message - From: Pedro Salgado [EMAIL PROTECTED] To: Struts Users Mailing List [EMAIL PROTECTED] Sent: Wednesday, December 31, 1969 7:05 PM Subject: Re: Please Help - ClassCastException On 04/01/2004 05:04, Caroline Jen [EMAIL PROTECTED] wrote: Thank you for trying to help. I have added import org.apache.artimus.message.PostForm; to my action class. I do not fully follow what I should check in the struts-config.xml file. And should I use name=postForm with lowercase 'p' or uppercase 'P'? In my struts-config.xml file, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm Here is the problem. Replace org.apache.struts.validator.DynaValidatorForm for org.apache.artimus.message.PostForm That should work Pedro Salgado form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ form-property name=title type=java.lang.String/ form-property name=postTopic type=java.lang.String/ form-property name=postBody type=java.lang.String/ /form-bean and action roles=administrator,editor,contributor path=/message/NewTopic type=org.apache.artimus.message.StoreMessage name=postForm scope=request validate=true input=.message.Form forward name=success path=.article.View/ /action Do you see any problems? --- Pedro Salgado [EMAIL PROTECTED] wrote: On your struts config file check if the form bean for StoreMessage action is of type pkg.pkg.PostForm and if the action name is pointing to the correct form bean... It also seems to be missing the import of the PostForm on your action class. Pedro Salgado On 04/01/2004 03:22, Caroline Jen [EMAIL PROTECTED] wrote: The statement shown below encountered a ClassCastException: PostForm postForm = ( PostForm )form; I cannot figure out the reason. Please help. Allow me to show more code of the class where the exception occurred: ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; int postID; String postCreationIP; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); PostForm postForm = ( PostForm )form; } } __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands
RE: Please Help - ClassCastException
Allow me to ask three more questions: 1. If I want to reset some specific text fields when the JSP is re-displayed, do I code the PostForm.java like this (please confirm): code: - import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.action.ActionMapping; import javax.servlet.http.HttpServletRequest; public class PostForm extends DynaValidatorForm { public void reset(ActionMapping mapping, HttpServletRequest request) { super.reset( mapping, request ); set( receiver, new String() ); set( sender, new String() ); } public PostForm () {} } --- 2. if I want to reset all the text fields when the JSP is re-displayed, do I code like this (please confirm): code: --- import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.action.ActionMapping; import javax.servlet.http.HttpServletRequest; public class PostForm extends DynaValidatorForm { public void reset(ActionMapping mapping, HttpServletRequest request) { initialize( mapping ); } public PostForm () {} } -- 3. What is the difference between code: --- form-bean name=postForm type=package.package.package.PostForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ /form-bean --- AND code: -- form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ /form-bean --- Thank you very much. --- Joe Hertz [EMAIL PROTECTED] wrote: Not at all. First off, set(String, String) is predefined for you. You don't need to create any of that. In fact, what you will find in a DynaValidatorForm is that your form classes start to get very sparse. Here's really what it could look like. public class PostForm extends BaseDynaForm { public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) { // throw new UnsupportedOperationException(Method is not implemented); } public PostForm () { } } In your ActionForm itself, beanUtils would work something like this. All you need is to get the right formBean object. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { PostForm pForm = (PostForm) form; // other code you write here BeanUtils.copyProperties(yourBean, pForm); // etc Doing it yourself without beanUtils? Instead of calling getSender() and having a String returned, you call get (sender) and it will return an Object (cast it yourself to whatever type the sender form property really is). I made a base form class with a getString method that calls get() and does the String cast for me to save the tediom. Making sense now? -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Sunday, January 04, 2004 2:46 AM To: Struts Users Mailing List Subject: RE: Please Help - ClassCastException I am still confused... I have a jsp that provides text fields for user to fill out information. All the information is passed via HttpRequest and to be validated. And all the properties in my PostForm are populated by the information retrieved from those text fields. And my PostForm is of type=org.apache.struts.validator.DynaValidatorForm Therefore, in my PostForm.java, I should do something like this? I just do not think the following code is correct. == import org.apache.struts.validator.DynaValidatorForm; public class PostForm extends DynaValidatorForm { private String receiver; private String sender; public void set(String receiver, String receiver); public void set(String sender, String sender); public String get(String receiver, String receiver); public String get(String sender, String sender); } == And you are saying that in my action class, I should ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import
Re: Please Help - ClassCastException
On your struts config file check if the form bean for StoreMessage action is of type pkg.pkg.PostForm and if the action name is pointing to the correct form bean... It also seems to be missing the import of the PostForm on your action class. Pedro Salgado On 04/01/2004 03:22, Caroline Jen [EMAIL PROTECTED] wrote: The statement shown below encountered a ClassCastException: PostForm postForm = ( PostForm )form; I cannot figure out the reason. Please help. Allow me to show more code of the class where the exception occurred: ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; int postID; String postCreationIP; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); PostForm postForm = ( PostForm )form; } } __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Please Help - ClassCastException
Thank you for trying to help. I have added import org.apache.artimus.message.PostForm; to my action class. I do not fully follow what I should check in the struts-config.xml file. And should I use name=postForm with lowercase 'p' or uppercase 'P'? In my struts-config.xml file, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ form-property name=title type=java.lang.String/ form-property name=postTopic type=java.lang.String/ form-property name=postBody type=java.lang.String/ /form-bean and action roles=administrator,editor,contributor path=/message/NewTopic type=org.apache.artimus.message.StoreMessage name=postForm scope=request validate=true input=.message.Form forward name=success path=.article.View/ /action Do you see any problems? --- Pedro Salgado [EMAIL PROTECTED] wrote: On your struts config file check if the form bean for StoreMessage action is of type pkg.pkg.PostForm and if the action name is pointing to the correct form bean... It also seems to be missing the import of the PostForm on your action class. Pedro Salgado On 04/01/2004 03:22, Caroline Jen [EMAIL PROTECTED] wrote: The statement shown below encountered a ClassCastException: PostForm postForm = ( PostForm )form; I cannot figure out the reason. Please help. Allow me to show more code of the class where the exception occurred: ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; int postID; String postCreationIP; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); PostForm postForm = ( PostForm )form; } } __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Please Help - ClassCastException
Here is what I see (opinions vary)... I see you are defining your form bean in your struts-config.xml as type 'org.apache.struts.validator.DynaValidatorForm'. So, why are you trying to cast it as this 'org.apache.artimus.message.PostForm' class. Does that class extend DynaValidatorForm? If it doesn't, you'll get a ClassCastException like you're getting now. Personally, I expected you to cast it as: DynaValidatorForm postForm = (DynaValidatorForm) form; Or us it as-initially defined (not casting like above) and use BeanUtils such as: String receiver = (String) PropertyUtils.getSimpleProperty(form, receiver); Regards, David -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Saturday, January 03, 2004 11:04 PM To: Struts Users Mailing List Subject: Re: Please Help - ClassCastException Thank you for trying to help. I have added import org.apache.artimus.message.PostForm; to my action class. I do not fully follow what I should check in the struts-config.xml file. And should I use name=postForm with lowercase 'p' or uppercase 'P'? In my struts-config.xml file, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ form-property name=title type=java.lang.String/ form-property name=postTopic type=java.lang.String/ form-property name=postBody type=java.lang.String/ /form-bean and action roles=administrator,editor,contributor path=/message/NewTopic type=org.apache.artimus.message.StoreMessage name=postForm scope=request validate=true input=.message.Form forward name=success path=.article.View/ /action Do you see any problems? --- Pedro Salgado [EMAIL PROTECTED] wrote: On your struts config file check if the form bean for StoreMessage action is of type pkg.pkg.PostForm and if the action name is pointing to the correct form bean... It also seems to be missing the import of the PostForm on your action class. Pedro Salgado On 04/01/2004 03:22, Caroline Jen [EMAIL PROTECTED] wrote: The statement shown below encountered a ClassCastException: PostForm postForm = ( PostForm )form; I cannot figure out the reason. Please help. Allow me to show more code of the class where the exception occurred: ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; int postID; String postCreationIP; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); PostForm postForm = ( PostForm )form; } } __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Please Help - ClassCastException
I think that there are a lot more mistakes in my code than I originally thought. The root of the problem is that I do not know how to use DynaValidatorForm. If you could help me in learning how to code when I am working with DynaValidatorForm. 1. in my struts-config.xml, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ . . /form-bean 2. My PostForm.java is like: import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.action.ActionMapping; public class PostForm extends DynaValidatorForm { private String receiver; private String sender; .. public void setReceiver( String receiver ) { this.receiver = receiver; } public void setSender( String sender ) { this.sender = sender; } public String getReceiver() { return receiver; } public String getSender() { return sender; } . . } 3. in my action class (see the code below) 3.1. do I cast the form to DynaActionForm? or I should cast the form to DynaValidatorForm? 3.2. Can I use the copyProperties() method of the BeanUtils to convert the form to a bean? BeanUtils.copyProperties( threadBean, postForm ); ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.struts.action.DynaActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostForm; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); DynaActionForm postForm = ( DynaActionForm )form; ThreadHandler thandler = new ThreadHandler(); ThreadBean threadBean = new ThreadBean(); BeanUtils.copyProperties( threadBean, postForm ); if (parentPostID == 0 ) // new topic { threadBean.setLastPostMemberName( memberName ); threadBean.setThreadCreationDate( now ); . . threadID = thandler.insertThread( threadBean ); } ... ... } } 4. the action mapping in my struts-config.xml is like: action roles=administrator,editor,contributor path=/message/NewTopic type=org.apache.artimus.message.StoreMessage name=postForm scope=request validate=true input=.message.Form forward name=success path=.article.View/ /action Thank you. --- David Friedman [EMAIL PROTECTED] wrote: Here is what I see (opinions vary)... I see you are defining your form bean in your struts-config.xml as type 'org.apache.struts.validator.DynaValidatorForm'. So, why are you trying to cast it as this 'org.apache.artimus.message.PostForm' class. Does that class extend DynaValidatorForm? If it doesn't, you'll get a ClassCastException like you're getting now. Personally, I expected you to cast it as: DynaValidatorForm postForm = (DynaValidatorForm) form; Or us it as-initially defined (not casting like above) and use BeanUtils such as: String receiver = (String) PropertyUtils.getSimpleProperty(form, receiver); Regards, David -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Saturday, January 03, 2004 11:04 PM To: Struts Users Mailing List Subject: Re: Please Help - ClassCastException Thank you for trying to help. I have added import org.apache.artimus.message.PostForm; to my action class. I do not fully follow what I should check in the struts-config.xml file. And should I use name=postForm with lowercase 'p' or uppercase 'P'? In my struts-config.xml file, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ form-property name
RE: Please Help - ClassCastException
Well, first off: In your (dyna) form, you don't create setters and getters for the properties though. With DynaForms you would say set(myPropertyName, myString) instead of calling setMyPropertyName(myString). This is the Dyna part of DynaForms. It's much less tedious IMHO. In your action, you absolutely want to cast the form to PostForm. Otherwise the form variable has no way to know properties are associated with it. So yes, once you cast it to PostForm calls to BeanUtils.copyProperties() can and will work properly. Hope this helps, -Joe -Original Message- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Sunday, January 04, 2004 1:42 AM To: Struts Users Mailing List Subject: RE: Please Help - ClassCastException I think that there are a lot more mistakes in my code than I originally thought. The root of the problem is that I do not know how to use DynaValidatorForm. If you could help me in learning how to code when I am working with DynaValidatorForm. 1. in my struts-config.xml, I have: form-bean name=postForm type=org.apache.struts.validator.DynaValidatorForm form-property name=receiver type=java.lang.String/ form-property name=sender type=java.lang.String/ . . /form-bean 2. My PostForm.java is like: import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.action.ActionMapping; public class PostForm extends DynaValidatorForm { private String receiver; private String sender; .. public void setReceiver( String receiver ) { this.receiver = receiver; } public void setSender( String sender ) { this.sender = sender; } public String getReceiver() { return receiver; } public String getSender() { return sender; } . . } 3. in my action class (see the code below) 3.1. do I cast the form to DynaActionForm? or I should cast the form to DynaValidatorForm? 3.2. Can I use the copyProperties() method of the BeanUtils to convert the form to a bean? BeanUtils.copyProperties( threadBean, postForm ); ... import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.struts.action.DynaActionForm; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.artimus.message.PostForm; import org.apache.artimus.message.PostBean; import org.apache.artimus.message.ThreadBean; import org.apache.artimus.message.utility.DateUtil; public final class StoreMessage extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int parentPostID; int threadID; String memberName = request.getRemoteUser(); Timestamp now = DateUtil.getCurrentGMTTimestamp(); parentPostID = Integer.parseInt( request.getParameter( parent ) ); DynaActionForm postForm = ( DynaActionForm )form; ThreadHandler thandler = new ThreadHandler(); ThreadBean threadBean = new ThreadBean(); BeanUtils.copyProperties( threadBean, postForm ); if (parentPostID == 0 ) // new topic { threadBean.setLastPostMemberName( memberName ); threadBean.setThreadCreationDate( now ); . . threadID = thandler.insertThread( threadBean ); } ... ... } } 4. the action mapping in my struts-config.xml is like: action roles=administrator,editor,contributor path=/message/NewTopic type=org.apache.artimus.message.StoreMessage name=postForm scope=request validate=true input=.message.Form forward name=success path=.article.View/ /action Thank you. --- David Friedman [EMAIL PROTECTED] wrote: Here is what I see (opinions vary)... I see you are defining your form bean in your struts-config.xml as type 'org.apache.struts.validator.DynaValidatorForm'. So, why are you trying to cast it as this 'org.apache.artimus.message.PostForm' class. Does that class extend DynaValidatorForm? If it doesn't, you'll get a ClassCastException like you're getting now. Personally, I expected you to cast it as: DynaValidatorForm postForm = (DynaValidatorForm) form; Or us it as-initially defined (not casting like above) and use BeanUtils