开发web程序的时候,我没有用struts和spring集成 ,只是单独的用了struts1.2,现在遇到一个问题,就是action是单例的,会有线程安全问题,
现在我在struts创建action 的时候,将这行代码改为actions.put(className, null);这样每次在map缓存中都没有实例化好的action,就是每次都会创建一个action。 这样修改不知道能不能解决struts1.2的action单实例问题, 希望高手多多指教,不胜感激
 protected Action processActionCreate(HttpServletRequest request,
                                         HttpServletResponse response,
                                         ActionMapping mapping)
        throws IOException {        // Acquire the Action instance we will be using (if there is one)
        String className = mapping.getType();
        if (log.isDebugEnabled()) {
            log.debug(" Looking for Action instance for class " + className);
        }        // :TODO: If there were a mapping property indicating whether
        // an Action were a singleton or not ([true]),
        // could we just instantiate and return a new instance here?        Action instance = null;
        synchronized (actions) {            // Return any existing Action instance of this class
            instance = (Action) actions.get(className);
            if (instance != null) {
                if (log.isTraceEnabled()) {
                    log.trace("  Returning existing Action instance");
                }
                return (instance);
            }            // Create and return a new Action instance
            if (log.isTraceEnabled()) {
                log.trace("  Creating new Action instance");
            }
            
            try {
                instance = (Action) RequestUtils.applicationInstance(className);
                // :TODO: Maybe we should propagate this exception
                // instead of returning null.
            } catch (Exception e) {
                log.error(
                    getInternal().getMessage("actionCreate", mapping.getPath()),
                    e);
                    
                response.sendError(
                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    getInternal().getMessage("actionCreate", mapping.getPath()));
                    
                return (null);
            }
            
            instance.setServlet(this.servlet);
            actions.put(className, null);
        }        return (instance);    }

解决方案 »

  1.   

    action 本身就是每次请求的时候重新创建一个实例
      

  2.   

    那是struts2, struts1是单实例的action
      

  3.   

    Struts1.X中Action从页面获得数据是从FormBean获得的,它不需要定义成员变量获取页面数据,所有它是无状态的,采用的是单实例模式,而且线程安全的
      

  4.   

    恩,楼主说的没错,我是采用struts1.2 + ActionBean 来开发的,这样就把jsp传过来的变量都定义在action中了,和struts2很相象,只不过action走的还是struts1的流程, 我上面那样修改之后不知道会不会引发什么其他情况?