UserForm f = (UserForm)form 这句为什么会出错?
刚开始学习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;
}
}