当前位置: 代码迷 >> J2EE >> Cannot cast from ActionForm to XXForm解决方案
  详细解决方案

Cannot cast from ActionForm to XXForm解决方案

热度:283   发布时间:2016-04-22 01:55:07.0
Cannot cast from ActionForm to XXForm
package com.hgq.student_information.web.struts.action;

import javax.servlet.http.HttpServletRequest;
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 com.hgq.student_information.dao.DAOFactory;
import com.hgq.student_information.dao.TeacherDAO;
import com.hgq.student_information.domain.Student;
import com.hgq.student_information.domain.Teacher;
import com.hgq.student_information.web.struts.form.StudentForm;
import com.hgq.student_information.web.struts.form.TeacherForm;

public class EditTeacher extends Action{
private TeacherDAO teaDao;

public TeacherDAO getTeacherDAO() {
return DAOFactory.getInstance().createTeacherDAO();
}

/**
* Method execute

* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

String id = (String)request.getParameter("teacher_id");

/*
* 声明一个StudentForm类型的对象. form为页面中提交的表单信息,将它
* 强制转换为StudentForm类型,并赋值给stuForm对象
*/
TeacherForm teaForm = (TeacherForm) form ; // 错误代码

teaDao = this.getTeacherDAO();
// 根据ID获取学生信息
Teacher tea = teaDao.getTeacherByID(id);

// 设置相应的更新属性
tea.setName( teaForm.getName());
tea.setPassword( teaForm.getPassword());
tea.setXuebu( teaForm.getXuebu());
tea.setSex( teaForm.getSex() );
tea.setTitle(teaForm.getTitle());
tea.setTel( teaForm.getTel()) ;
tea.setEmail( teaForm.getEmail()) ;

if(teaDao.updateTeacher(tea)){
// 更新成功,跳转页面
return (mapping.findForward("success"));
}else {

return (mapping.getInputForward());
}

}
}


代码我已经继承Action类了..为什么还会出现这个错误...
最奇怪的是..之前有个类也是试用此代码..都没有错误.
就这个类的代码出现这个错误...

求高手解答..

------解决方案--------------------
struts-config.xml也贴出来啊。
估计是<action name="这个地方填错了。。">
------解决方案--------------------
关键是你的TeacherForm 怎么写的,是集成了ActionForm吗?奇怪了,TeacherForm 和你的Action有啥关系
------解决方案--------------------
问题是你的form没有继承actionForm
  相关解决方案