论坛首页 Java版 Struts

关于ActionForm初始化问题!

浏览 4557 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-03-08
按道理struct的actionform,scope定义request,每次调用都会产生新的actionform实列.然后actionform的也会自动初始化actionForm原来的初始值.不会保留原来的,或者上次调用的初始值.现在现在还有保留上次调用的初始值.

定义如下:

(1)定义一个ActionForm
public UserForm extends ActionForm{

    private List values = new AutoArrayList<UserVO.class>();
    public List getValues(){
         return values;
    }
     public void setValues(List values){
         this.values = values;
    }
}

(2)定义一个VO: UserVO
public UserVO implements Serializable{

     private  String name;
      public String getName(){
         return values;
     }
     public void setValues(String  name){
         this.name= name;
    }
}

问题当我访问带二个参数Action.http://localhost:8080/bs/user.do?values[0].name=a&values[1].name=b
发现内存是:values[0].name=a和values[1].name=b
我当再访问此Action.带一个参数http://localhost:8080/bs/user.do?values[0].name=a.
发现内存是:values[0].name=a和values[1].name=b还是两个值.
说明每次访问一个初始values的值.只有覆盖了.

当我在ActionForm加上reset(request,mapping)方法,就会初始化.
public ActionForm reset(request,mapping){
     this.values = new AutoArrayList<UserVO.class>()
}



我不知道ActionForm的reset.参数怎么配置,才能初始化ActionForm.
   
最后更新时间:2007-03-10
以前看过Struts书籍上有讲过Action的处理流程:
调用Action 的execute()方法前,先调用Action对应ActionForm的reset()方法,再将Html Form的内容设置到对应的ActionForm中,再执行Action的,楼主可能在UserForm中没有重写ActionForm类的reset()方法,所以没有清空以前的脏数据,而是直接把html form的数据又重新set到你的UserForm中.
当你加入:

public ActionForm reset(request,mapping){   
     this.values = new AutoArrayList<UserVO.class>()   
}  


([ code]code [ /code]怎么会重复3次,是不是javaEye写贴子的BUG?!编辑一次就OK了.)
说明在执行Action前已经过滤了以前的脏数据.

ps:新手,以上纯属个人的分析,书本上那段原文也不太记得了,但好象大致是这个意思,不知道分析得对不对,请各位批评指正.
   
0 请登录后投票
最后更新时间:2007-03-10
action mapping中设定scope="request"
   
0 请登录后投票
最后更新时间:2007-03-11
我STRUTS学到一半,就你的问题我的观点是:首先ActionForm是表单form(html:form)struts的FORM,提交时自动进行封装数据的,而你却是以这种传值的方法,感觉不是很对。
再者:二次是一样是不是缓存的问题??
在每次提交完是应该把 request范围中的FORM给删掉
还有就是RESET方法直接写  this.values=null,this.name=null;

能想到的就那么多,你再试试......
   
0 请登录后投票
最后更新时间:2007-03-26
没有碰见过这样的问题
   
0 请登录后投票
最后更新时间:2007-04-29
正常情况下,这种情况不会出现,能否把你的Action也贴出来啊。
   
0 请登录后投票
最后更新时间:2007-06-06
这个问题之前在项目中遇到过,查阅资料后发现这个是struts的一个bug,不要使用form,建议使用javabean来代替,这样就很好的解决这个问题。
   
0 请登录后投票
最后更新时间:2007-06-06
我想问一下
http://localhost:8080/bs/user.do?values[0].name=a&values[1].name=b

这个是做什么用的??
   
0 请登录后投票
最后更新时间:2007-06-13

首先回顾下Request的生命周期:Request是每个客户端发出的一次请求, 当服务器端推出一个页面到客户端时,即当前请求-响应结束时,意味着这个Request的结束,就会销毁Request实例,那么一个新的请求,将是一个新的Request实例


在执行action的execute()方法前,org.apache.struts.action.RequestProcessor会执行一系列的processXXX方法,其中一个是processActionForm方法,请看代码(struts1.2.7)

java 代码
  1. protected ActionForm processActionForm(HttpServletRequest request,   
  2.                                            HttpServletResponse response,   
  3.                                            ActionMapping mapping) {   
  4.   
  5.         // Create (if necessary) a form bean to use   
  6.         //(如果有必要)创建一个新的formbean, 现在请看下面的RequestUtils.createActionForm源码    
  7.         ActionForm instance = RequestUtils.createActionForm   
  8.             (request, mapping, moduleConfig, servlet);   
  9.         if (instance == null) {   
  10.             return (null);   
  11.         }   
  12.   
  13.         // Store the new instance in the appropriate scope   
  14.     //把新的实例存储在合适的范围内(根据action配置的时指定的scope的值)   
  15.         if (log.isDebugEnabled()) {   
  16.             log.debug(" Storing ActionForm bean instance in scope '" +   
  17.                 mapping.getScope() + "' under attribute key '" +   
  18.                 mapping.getAttribute() + "'");   
  19.         }   
  20.         //   
  21.         if ("request".equals(mapping.getScope())) {   
  22.             request.setAttribute(mapping.getAttribute(), instance);   
  23.         } else {   
  24.             HttpSession session = request.getSession();   
  25.             session.setAttribute(mapping.getAttribute(), instance);   
  26.         }   
  27.         return (instance);   
  28.   
  29.     }  

org.apache.struts.util.RequestUtils的部分方法:

java 代码
  1. public static ActionForm createActionForm(   
  2.             HttpServletRequest request,   
  3.             ActionMapping mapping,   
  4.             ModuleConfig moduleConfig,   
  5.             ActionServlet servlet) {   
  6.   
  7.         // Is there a form bean associated with this mapping?   
  8.         String attribute = mapping.getAttribute();   
  9.         if (attribute == null) {   
  10.             return (null);   
  11.         }   
  12.   
  13.         // Look up the form bean configuration information to use   
  14.         String name = mapping.getName();   
  15.         FormBeanConfig config = moduleConfig.findFormBeanConfig(name);   
  16.         if (config == null) {   
  17.             log.warn("No FormBeanConfig found under '" + name + "'");   
  18.             return (null);   
  19.         }   
  20.         //根据 action 配置从request或session中获得form bean 实例,如果指定scope="request"    
  21.     //那么每次请求都是新的Request实例,那么也就不会有form bean 实例,该方法将返回null   
  22.         ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());   
  23.   
  24.         // Can we recycle the existing form bean instance (if there is one)?   
  25.     //可以重复使用一个已经存在的form bean 实例么   
  26.         try {   
  27.         //instance != null 且 instance 是 type 是 DynaActionForm 或 ActionForm 时返回form bean实例   
  28.         //这里应该是scope="session"的情况吧,formbean保持上次请求的状态   
  29.             if (instance != null && canReuseActionForm(instance, config)) {   
  30.                 return (instance);   
  31.             }   
  32.         } catch(ClassNotFoundException e) {   
  33.             log.error(servlet.getInternal().getMessage("formBean", config.getType()), e);   
  34.             return (null);   
  35.         }   
  36.   
  37.         //返回一个新创建的form bean 实例()   
  38.         return createActionForm(config, servlet);   
  39.     }   
  40.        
  41.     private static ActionForm lookupActionForm(HttpServletRequest request, String attribute, String scope)   
  42.     {   
  43.         // Look up any existing form bean instance   
  44.         if (log.isDebugEnabled()) {   
  45.             log.debug(   
  46.                     " Looking for ActionForm bean instance in scope '"  
  47.                     + scope   
  48.                     + "' under attribute key '"  
  49.                     + attribute   
  50.                     + "'");   
  51.         }   
  52.         ActionForm instance = null;   
  53.         HttpSession session = null;   
  54.         if ("request".equals(scope)) {   
  55.             instance = (ActionForm) request.getAttribute(attribute);   
  56.         } else {   
  57.             session = request.getSession();   
  58.             instance = (ActionForm) session.getAttribute(attribute);   
  59.         }   
  60.   
  61.         return (instance);   
  62.     }   

从源码可以看出, 在配置 action 时设定 scope="request"  应该就可以解决楼主的问题了,需要提一下的是scope的默认值是session,我想问题就在这里了,如果楼主设置的scope="request" ,那可能真是一个bug了,我还没有遇到过这种情况

   
0 请登录后投票
最后更新时间:2007-06-13
是struts 1吧  这个问题好早以前就说过了
   
0 请登录后投票
论坛首页 Java版 Struts

跳转论坛:
JavaEye推荐