UserForm f = (UserForm)form 这句为什么会出错?

NeverFear 2004-09-27 03:10:18
刚开始学习struts,于是照着书上写程序,(UserForm)form 应该是个强制类型转换吧,
为什么这儿Jbulider会报警呢!详细代码在下面,大家帮我看看呀!谢谢了!

package cn.com.bigbang.user;

import cn.com.bigbang.user.*;
import org.apache.struts.action.*;
import javax.servlet.http.*;

/**
*
* <p>Title: UserAction</p>
* <p>Description: 业务控制</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 西安BB</p>
* @author ChengJun
* @version 1.0
*/

public class UserAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
UserForm f = (UserForm)form;
try
{
UserBean bean = new UserBean();//User连接数据库,执行业务逻辑
bean.addUser(f.getUser()); //添加用户
}
catch(Exception e)
{
e.printStackTrace();
}
request.setAttribute("User",f.getUser());
return(mapping.findForward("userCreated"));
}
}

UserForm 如下:
package cn.com.bigbang.user;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

/**
*
* <p>Title: UserForm.java</p>
* <p>Description: 用户表单类</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 西安BB</p>
* @author ChengJun
* @version 1.0
*/

public class UserForm extends ActionForm{
private User user = new User();
public void setUser(User user)
{
this.user=user;
}
public User getUser()
{
return this.user;
}

//重新设置Form
public void reset(ActionMapping apping,HttpServletRequest request)
{
this.user = new User();
}

//Form有效性验证
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
if(user.getName() == null)
{
errors.add("name",new ActionError("error.user.name"));
}
return errors;
}
}

...全文
48 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weipj007 2004-09-27
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse)

强制转换的对象应该是上述方法中ActionForm定义的变量。
回复
newmember 2004-09-27
:) 搂主用的开发工具?怎么编译通过的?
回复
yeshucheng 2004-09-27
UserForm f = (UserForm)form 这个要变成:UserForm f = (UserForm)actionForm就可以
回复
xunyiren 2004-09-27
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
UserForm f = (UserForm)form;
注意上面这两句,把UserForm f = (UserForm)form;改成UserForm f = (UserForm)actionForm;

回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告