Spring Mvc那点事---(24)Spring Mvc监听器监听对象和属性

    这一节我们看下监听器怎样监听域对象中的属性的创建和销毁。对于监听对象中属性,在三个域中同样是三个接口 ServletContextAttributeListener,ServletRequestAttributeListener,HttpSessionAttributeListener。

1.ServletContextAttributeListener

public class firstServletContextAttribute implements
		ServletContextAttributeListener {

	public void attributeAdded(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
           System.out.println("ServletContextAttributeListener--attributeAdded--用户名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

	public void attributeRemoved(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		   System.out.println("ServletContextAttributeListener--attributeRemoved--用户名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		 System.out.println("ServletContextAttributeListener--attributeReplaced--用户名:"+arg0.getName()+"--值:"+arg0.getValue());

	}

}

   <listener> 
          <listener-class>com.selfListener.firstServletContextAttribute</listener-class>
        </listener>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

添加属性:
 <%application.setAttribute("username","liming"); %>
 取代属性
 <% application.setAttribute("username", "lucy"); %>
 删除属性
  <%application.removeAttribute("username");%>
</body>
</html>

输出结果

ServletContextAttributeListener--attributeAdded--用户名:username--值:liming
ServletContextAttributeListener--attributeReplaced--用户名:username--值:liming
ServletContextAttributeListener--attributeRemoved--用户名:username--值:lucyorg.apache.catalina.core.ApplicationContextFacade@3d982a02

2.HttpSessionAttributeListener

public class firstHttpSessionAttribute implements HttpSessionAttributeListener {

	public void attributeAdded(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
        System.out.println("HttpSessionAttributeListener--attributeAdded--名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("HttpSessionAttributeListener--attributeRemoved--名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("HttpSessionAttributeListener--attributeReplaced--名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

}
 <listener> 
          <listener-class>com.selfListener.firstHttpSessionAttribute</listener-class>
        </listener>

 
  <%
   request.getSession().setAttribute("num", 1);
  request.getSession().setAttribute("num", 2);
  request.getSession().removeAttribute("num");
  %>

输出结果

HttpSessionAttributeListener--attributeAdded--名:num--值:1
HttpSessionAttributeListener--attributeReplaced--名:num--值:1
HttpSessionAttributeListener--attributeRemoved--名:num--值:2

3.ServletRequestAttributeListener

public class firstRequestAttribute implements ServletRequestAttributeListener {

	public void attributeAdded(ServletRequestAttributeEvent arg0) {
		// TODO Auto-generated method stub
		 System.out.println("ServletRequestAttributeListener--attributeAdded--名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

	public void attributeRemoved(ServletRequestAttributeEvent arg0) {
		// TODO Auto-generated method stub
		 System.out.println("ServletRequestAttributeListener--attributeRemoved--名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

	public void attributeReplaced(ServletRequestAttributeEvent arg0) {
		// TODO Auto-generated method stub
		 System.out.println("ServletRequestAttributeListener--attributeReplaced--名:"+arg0.getName()+"--值:"+arg0.getValue());
	}

}

        <listener> 
          <listener-class>com.selfListener.firstRequestAttribute</listener-class>
        </listener>


  <%
   request.setAttribute("para", "001");
  request.setAttribute("para", "002");
  request.removeAttribute("para");
  %>

输出结果

ServletRequestAttributeListener--attributeAdded--名:para--值:001
ServletRequestAttributeListener--attributeReplaced--名:para--值:001
ServletRequestAttributeListener--attributeRemoved--名:para--值:002




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值