SpringMvc自定义监听器以及从Controller获取Application域对象

SpringMvc自定义监听器

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
@Component
public class sysInitListener implements ServletContextListener, ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
       //获取上下文
        ServletContext servletContext = sce.getServletContext();
        //通过applicationContext获取Spring的bean对象
        DicService dicService = applicationContext.getBean(DicService.class);
        Map<String, List<DicValue>> map = dicService.getAll();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            servletContext.setAttribute(key,map.get(key));
        }

    }
	@Override
    public void contextDestroyed(ServletContextEvent sce) {
        
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        sysInitListener.applicationContext =applicationContext;

    }
}
	实现一个用于自定义监听器 实现要实现**ServletContextListener**接口
由于我们要获取spring容器 所以我们还要实现**ApplicationContextAware**接口 并且实现对	应的方法。

需要注意的是我们的监听器配置代码的位置一定要在spring监听器的下面 因为我们的监	听器依赖于spring监听器
  <listener>
        <listener-class>cn.it.crm.web.listener.sysInitListener</listener-class>
    </listener>
application:全局作用范围,整个应用程序共享,

就是在部署文件中的同一个webApp共享

生命周期为:应用程序启动到停止
HttpServletRequest

request.getSession().getServeltContext(). 返回值就是application 当前对象	的appliaction的值

给处理器方法添加参数:  request.getSession().getServletContext().setAttribute("key","value"); 即可将数据存入application。

从Controller获取Application域对象

@RequestMapping("/getDic")
    public @ResponseBody
    Map<String, Object> getDic(HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<>();
        ServletContext servletContext = request.getSession().getServletContext();
        Enumeration<String> attributeNames = servletContext.getAttributeNames();
        Object obj = null;
        while (attributeNames.hasMoreElements()) {
            String s = attributeNames.nextElement();
            if (!s.contains(".")) {
                obj =  servletContext.getAttribute(s);
                map.put(s, obj);
            }
        }
        return map;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在IntelliJ IDEA中创建Spring MVC框架的步骤: 1.创建一个新的Maven项目,选择Web应用程序模板。 2.在Maven项目中添加Spring MVC依赖项,包括spring-webmvc和其他相关依赖项。 3.创建一个Java类并将其标记为控制器,使用@Controller注释。 4.创建一个JSP视图来呈现数据。 5.创建一个Spring配置文件来配置Spring MVC,包括视图解析器和控制器映射。 6.在web.xml文件中配置DispatcherServlet和Spring监听器。 7.在main下的resources文件夹中创建一个名为springMVC.xml的Spring配置文件,用于配置Spring MVC。 下面是一个简单的示例,演示如何在IntelliJ IDEA中创建Spring MVC框架: 1.创建一个新的Maven项目,选择Web应用程序模板。 2.在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> ``` 3.创建一个名为HelloControllerJava类,并使用@Controller注释将其标记为控制器: ```java @Controller public class HelloController { @RequestMapping("/hello") public ModelAndView hello() { String message = "Hello Spring MVC!"; return new ModelAndView("hello", "message", message); } } ``` 4.创建一个名为hello.jsp的JSP视图,用于呈现数据: ```html <html> <head> <title>Hello</title> </head> <body> <h2>${message}</h2> </body> </html> ``` 5.创建一个名为spring-servlet.xml的Spring配置文件,用于配置Spring MVC: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.example"/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans> ``` 6.在web.xml文件中配置DispatcherServlet和Spring监听器: ```xml <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> ``` 7.在main下的resources文件夹中创建一个名为springMVC.xml的Spring配置文件,用于配置Spring MVC。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值