spring整合web

来源:互联网 发布:change my mac中文版 编辑:程序博客网 时间:2024/06/01 10:33
1. Spring整合WEB需要引入spring-web-4.2.4.RELEASE.jar包


2. 在web.xml中配置监听器并且读取配置文件:
         <!-- 配置Spring的核心监听器: -->
         <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
         <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
         </context-param>


3. 修改Action的代码
ServletContext servletContext = ServletActionContext.getServletContext();
        // 需要使用WEB的工厂的方式
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
context.getBean("id名");
调用目标对象的方法;