Meaningless Drivel is fun!*
The moose likes Struts and the fly likes Help Please Cannot cast from ActionForm to StudentForm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Help Please Cannot cast from ActionForm to StudentForm" Watch "Help Please Cannot cast from ActionForm to StudentForm" New topic
Author

Help Please Cannot cast from ActionForm to StudentForm

student study
Greenhorn

Joined: Jan 15, 2005
Posts: 1
Hi All,
I am getting this Eroor


Cannot cast from ActionForm to StudentForm


public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
StudentService service = new StudentService();
StudentForm studentForm = (StudentForm) form; //Error
StudentDTO studentDTO = new StudentDTO();
BeanUtils.copyProperties( studentDTO, studentForm );
service.insertStudent( studentDTO );
request.setAttribute("employee",studentDTO);
return (mapping.findForward("success"));
}

I have extended Action in StudentForm class.


Any help would be greatly appreciated.

Thanks
AS
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by student study:
I have extended Action in StudentForm class.

You'll have to extend ActionForm, not Action.

By the way, since "student study" is not in compliance with our naming policy, I'll have to ask you to change your display name into something valid. The display name must be your real name or a fake name that looks real and isn't otherwise obviously fake.
Thanks.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
please post your struts-config.xml file.


AND


please check form type of this action in config file.

may be incorrect...


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
nash avin
Ranch Hand

Joined: Nov 18, 2004
Posts: 45
Hi Guys,

Thanks a lot for the responses.I will surely work that out.Must be a trivial issue.

Thanks again
AS
venkat penmatsa
Greenhorn

Joined: Jul 23, 2011
Posts: 17
Hi , I m getting Cannot cast from ActionForm to BankController error even though i imported the action class , please help me ASAP.

public class BankController extends Action{

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

LoginForm l=(LoginForm)form;
BankController b=(BankController)form;

return null;
}
}

this is my struts.config,xml



<action
attribute="Balence"
input="Banklogin.jsp" name="Balence" path="/BankController" scope="request"
type="edu.action.BankController">
<set-property property="cancellable" value="true" />
<forward name="failure" path="/failure.jsp" />
<forward name="success" path="/welcome.jsp" />
</action>









Mohan rvscas
Ranch Hand

Joined: Jul 03, 2010
Posts: 39
Hi venkat penmatsa,

I think your form-bean tag only mapped with LoginForm class.

and also for a request should have only one form-bean attribute that means either LoginForm bean or BankController bean

but you are casting the form object for two classes as you posted like this.


LoginForm l=(LoginForm)form;
BankController b=(BankController)form;


just put any one statement. Relevant what you mapped with the form-bean attribute.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Please Cannot cast from ActionForm to StudentForm