java中“StudentForm studentForm = (StudentForm)form”

来源:百度知道 编辑:UC知道 时间:2024/05/31 09:37:28
请问这个话和
StudentForm studentForm = new StudentForm ();
有什么区别?

前者是通过Struts机制去做映射的
只要你的JSP页面配到相映的Action中(通过页面路径和config.xml对应上)
当你StudentForm studentForm = (StudentForm)form
走完之后 你的studentForm就有从JSP页面传过来的值(前提是你的formBean和页面中所要接的属性名字要对应)
而后者你可以用DEBUG查看下就回发现studentForm里面什么都没有 就是普通的创建一个对象 对象里面是没有值的 需要你去设置。

第一个是用父类转换对象
form是父类 强制转换成子类StudentForm

第二个就是单纯创建一个StudentForm 对象以及引用studentForm

StudentForm studentForm = (StudentForm)form

这里面应该是struts常用的转型,也就是说你StudentForm 继承了这个form
可以转换

不一样!

StudentForm studentForm = new StudentForm();
仅仅实例化一个对象

StudentForm studentForm = (StudentForm)form;
是将form强制类型转换后赋给StudentForm对象
不出意外的话form应该是ActionForm对象.

区别很明显:前者是把一个已经存在的对象强转成StudentForm
后者是新创建一个StudentForm对象