`
liuxinglanyue
  • 浏览: 557181 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

struts2生命周期

阅读更多

有很多人问Struts2.0中的对象既然都是线程安全的,都不是单例模式,那么它究竟何时创建,何时销毁呢? 

这个和struts2.0中的配置有关,我们来看struts.properties 

### if specified, the default object factory can be overridden here 
### Note: short-hand notation is supported in some cases, such as "spring" 
###       Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here 
struts.objectFactory = spring 
如果我们使用的是com.opensymphony.xwork2.ObjectFactory ,老实说,我也没有研究过,xwork有一套像spring一样的IOC机制,小巧而简洁,有兴趣的朋友可以去研究下。struts2.0中的 Action默认就是使用这种工厂模式的,我们来看 

    <action name="index" class="hdu.management.action.IndexAction"> 
        <result name="success">/input.jsp</result> 
        <result name="testFTL" type="freemarker">/ftl/test.jsp</result> 
    </action> 
class属性必须写类的全名,通过这种方式配置后,action对象的生命周期到底怎么样,你就认命吧,反正你就记住xwork有一个对象池,它会自己分配的,应对每次客户端的请求,它都会创建一个新的实例,至于这个实例何时销毁,由XWORK来控制。 


接下来,我们用spring来控制action的生命周期,关于action和spring的集成,我这里就不累述了。 

    <action name="index" class="index"> 
        <result name="success">/input.jsp</result> 
        <result name="testFTL" type="freemarker">/ftl/test.jsp</result> 
    </action> 
这里的class是spring配置文件中bean的id 

我们来看看spring文档中关于生命周期这个章节 


Table 3.4. Bean scopes 


Scope Description 
singleton 
Scopes a single bean definition to a single object instance per Spring IoC container. 

prototype 
Scopes a single bean definition to any number of object instances. 

request 
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext. 

session 
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. 

global session 
Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext. 


是不是一目了然? 

当然我们要使用request、session等,必须在web.xml中配置 

<web-app> 
... 
<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
... 
</web-app> 
准备好这些之后,我们来对request这个scope进行下测试 

<beans> 
   
    <bean id="index" class="hdu.management.action.IndexAction" 
        scope="request" 
        destroy-method="destroy" 
        init-method="init"/> 
   
</beans> 
配置好后,发现每次刷新页面,都会建立一个新的实例,运行完后就销毁这个实例,这个效果和默认的是一样的,只是我们这个运行完后会立即销毁,而默认的不是立即销毁,由xwork优化销毁 

如果设置为session,其实相当于ejb中的状态bean,对于每个session来说用的都是同一个实例,当然,一旦把浏览器关闭,或者超时,对象就会销亡。

分享到:
评论

相关推荐

    struts2完全学习手册源码

    7. **Struts2生命周期**: - 一个典型的Struts2请求处理包括:解析请求、查找Action、调用Action、执行拦截器、处理结果等步骤。理解这个生命周期有助于调试和优化应用性能。 8. **插件和扩展性**: - Struts2...

    struts2例子

    11. **Struts2的生命周期**:从用户发送请求开始,经过URL映射、Action选择、拦截器执行、Action方法调用、结果处理,最后返回响应给客户端,这就是一个完整的Struts2生命周期。 12. **单元测试与整合测试**:...

    struts2jar.zip

    6. **Struts2的生命周期**:包括初始化、请求处理、响应生成等阶段。 7. **Action的注解配置**:Struts2支持通过注解来简化配置,如@Action、@Result等。 8. **.struts2-convention-plugin**:这个插件允许开发者...

    struts1和struts2的区别

    - **Struts1**: 在Struts1架构中,Action的生命周期是由容器管理的。每次HTTP请求都需要重新创建Action实例,并调用它的`execute`方法。这种方式导致了较高的资源消耗。 - **Struts2**: 每个Action实例都只服务于...

    struts2测试工具

    为了充分利用这个工具,你需要了解Struts2的生命周期、配置方式以及如何编写测试用例。测试通常包括验证Action类的行为、结果的跳转、Interceptor的执行顺序、以及视图层的正确渲染。同时,考虑到Struts2的安全性...

    struts2ajax项目

    此外,为了更好地展示数据,可能还需要在Struts2中使用Interceptor(拦截器),例如,`params`拦截器用于将请求参数绑定到Action,`validation`拦截器负责验证表单数据,`workflow`拦截器管理Action的生命周期。...

    struts2讲义

    - **Struts1** 主要是基于Servlet API进行操作,使用ActionForm来封装请求参数,并且依赖于JSP页面的生命周期;而 **Struts2** 则更加灵活,支持多种视图技术如JSP、FreeMarker等,同时也支持更丰富的插件和拦截器...

    struts2经典实例

    通过这个实例,你可以学习到Action的生命周期、结果映射以及如何处理业务逻辑。 7. **Struts2 HelloWorld** `Struts2_HelloWorld` 是经典的“Hello, World!”示例,它是最基本的入门教程。这个例子简单地演示了...

    struts2 与 struts1的区别

    - **Struts2** 则引入了拦截器栈(Interceptor Stacks)的概念,允许针对每个Action定义不同的执行路径和生命周期管理方式。这种灵活性提高了应用程序的可配置性和可扩展性。 综上所述,Struts2相比Struts1在架构...

    struts1和struts2区别

    - **Struts2**:支持拦截器堆栈,允许为每个Action创建独特的生命周期,堆栈可以根据需要定制。 总的来说,Struts2在设计上更加灵活、可测试和可扩展,而Struts1则相对更注重基础功能和简单的实现。在实际项目中,...

    Struts2SpringUnitDemo单元测试

    Spring的IoC(Inverse of Control,控制反转)容器通过依赖注入管理对象的生命周期和相互依赖关系。在单元测试中,Spring的Mockito库可以用来模拟协作对象,以便隔离被测试的代码。 3. **单元测试**:单元测试是...

    Struts2实战(Struts2 In Action中文版)

    4. **Action与结果**:理解Action类的生命周期,以及如何配置结果类型,如Redirect、RedirectAction、Stream等。 5. **表单验证**:Struts2内置的验证框架,包括基于XML和注解的验证方式,以及如何处理验证失败的...

    struts2-spring-plugin-2.3.4.jar

    4. **Spring 的Bean 定义**:在Spring 配置文件中,我们可以为Struts 2 的Action 定义bean,包括其属性、依赖关系以及生命周期方法。这使得Action 的配置更加集中,易于管理和扩展。 5. **Action 实例的生命周期**...

    struts2,spring

    3. **整合Struts2与Spring**:在Struts2配置文件中声明Spring的Action bean,并指定Spring管理其生命周期。 4. **编写Action类**:Action类通常会注入需要的服务或DAO,实现业务逻辑。 5. **创建视图**:使用JSP或...

    struts2基本类库

    7. **Struts2的生命周期**: 一个请求到达时,Struts2会经历Action实例化、拦截器链执行、Action方法调用、结果渲染等阶段。了解这个生命周期有助于调试和优化应用。 8. **类型转换**: Struts2自动将请求参数...

    struts2 最新jar包

    比如,`com.opensymphony.xwork2`包下的ActionSupport类是所有自定义Action类的基础,`org.apache.struts2`包下的StrutsPrepareAndExecuteFilter是Struts2的过滤器,负责拦截HTTP请求并执行Struts2的生命周期。...

    struts2-tags-API,struts2标签api

    如果遇到问题,可以查看标签的错误信息,或者使用调试工具查看标签的生命周期和执行流程,以便定位和解决问题。 总结,`struts2-tags-API`是学习和使用Struts2框架标签的重要资源。通过深入理解和应用这些标签,...

    struts2简单案例

    - 学习Struts2的生命周期和工作原理。 - 理解Action、Result、Interceptor的职责。 - 掌握JSP基本语法和EL、JSTL的使用。 - 实践创建Action类,编写`struts.xml`配置,以及与JSP交互。 6. **注意事项** - 使用...

    struts2所有jar包

    1. **核心库**:`struts2-core.jar` 是框架的核心,包含了Action、Interceptor、Result等主要组件,以及配置解析和生命周期管理等功能。 2. **插件**:Struts2支持许多插件,如`struts2-convention-plugin.jar` ...

Global site tag (gtag.js) - Google Analytics