牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2016-07-03 投稿人: 浏览次数:156

    这一节我们看下监听器怎样监听域对象中的属性的创建和销毁。对于监听对象中属性,在三个域中同样是三个接口 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




声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。