日期:2014-05-17  浏览次数:20645 次

请教一下Struts1.2中的小问题
关于这个代码中有几句不太明白,请给我解释一下吧,谢谢。
Java code
package action;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import actionform.*;
public class UpdateNewsAction extends Action   {
    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
    {
        NewsForm updateNewsForm=(NewsForm)form; // 这句什么意思,写具体点啦,谢谢。
        try
        {   
            UpdateNews news= new UpdateNews(updateNewsForm); // 这句什么意思
            news.update();
            request.setAttribute("update","修改成功!");
        }
        catch(Exception e)
        {
            request.setAttribute("update",e.getMessage());
        }
        return mapping.findForward("update");
    }
}





------解决方案--------------------
NewsForm updateNewsForm=(NewsForm)form; 
页面的提交过来的表单 转换成 NewsForm 表单 这样就可以通过 NewsForm里的get方法取得对应的值

UpdateNews news= new UpdateNews(updateNewsForm); 
new UpdateNews 这是一个构造方法 参数是NewsForm
你可以点进去看看源码 应该是 把表单里值 赋值给 一个新UpdateNews 这个对象
------解决方案--------------------
NewsForm updateNewsForm=(NewsForm)form; 获取你提交的表单,将你提交的表单转换为你定义的NewsForm 

UpdateNews news= new UpdateNews(updateNewsForm); 这个要看你的UpdateNews的构造函数是怎么写的了



------解决方案--------------------
NewsForm updateNewsForm=(NewsForm)form; 
是将前台提交的表单中的数据取出来set到你NewsForm中对应的属性当中,
UpdateNews news= new UpdateNews(updateNewsForm); 
是调用UpdateNews的public UpdateNews(NewsForm newsForm){}的构造函数,
------解决方案--------------------
strus1 中有个ActionForm的概念,每个页面的一个<form action="" method=""/></form>都对应的有个ActionForm,就是用来装页面的表单中的值的,
 NewsForm updateNewsForm=(NewsForm)form;这句就是得到你的ActionForm
为了让代码不具有侵入性,所以自己又写了一个javabean,也就是这个UpdateNews类,通过构造方法,把ActionForm中的值set到UpdateNews的对象中,用来跟业务层传递,希望对你有帮助!
------解决方案--------------------
探讨

引用:
NewsForm updateNewsForm=(NewsForm)form;
是将前台提交的表单中的数据取出来set到你NewsForm中对应的属性当中,
UpdateNews news= new UpdateNews(updateNewsForm);
是调用UpdateNews的public UpdateNews(NewsForm newsF……