struts对对象属性的监听

对对象属性的监听
无参构造方法必须有

Scope的默认值为session
分析:
Scope.setAttribute():
1.从过程区查看
 HttpSessionAttributeListener
 ServletRequestAttributeListener

 

一、在web.xml中注册<listener><listener-class>name</listener-class><listener>

    <action-mappings>
        <action path="/login" type="com.itstruct.LoginAction" name="login" scope="request">
        <forward name="longinSuccess" path="/loginSuccess.jsp"></forward>

【scope="request">】

元:<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app id="Webapp_ID">

替换为:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

添加:<listener>标签可行

=====================================

在struts-config的<action-mapping>内修改  <action path="/addStudent" type="com.itstruct.AddStudentAction" name="addStudentForm" scope="request">,增加“scope="request">”。执行 后输出如下

=======================================

package com.itstruct;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.struts.action.ActionForm;

public class AddObjectListener implements HttpSessionAttributeListener,
  ServletRequestAttributeListener {

 @Override
 public void attributeAdded(ServletRequestAttributeEvent event) {
  // TODO Auto-generated method stub
  String attributename=event.getName();
  System.out.println("attributeAdded(ServletRequestAttributeEvent event)=="+attributename);
  if(event.getValue() instanceof ActionForm)
  {
   System.out.println("attributeAdded(ServletRequestAttributeEvent event)=="+attributename);
   System.out.println("name="+attributename+"\tvalue="+event.getValue());
  }
   

 }

 @Override
 public void attributeRemoved(ServletRequestAttributeEvent event) {
  System.out.println("attributeRemoved(ServletRequestAttributeEvent event)");

 }

 @Override
 public void attributeReplaced(ServletRequestAttributeEvent event) {
  System.out.println("attributeReplaced(ServletRequestAttributeEvent event)");

 }

 @Override
 public void attributeAdded(HttpSessionBindingEvent event) {
  String attributename=event.getName();
  
  if(event.getValue() instanceof ActionForm)
  {
   System.out.println("attributeAdded(HttpSessionBindingEvent event)"+attributename);
   System.out.println("name="+attributename+"\tvalue="+event.getValue());
  }

 }

 @Override
 public void attributeRemoved(HttpSessionBindingEvent event) {
  System.out.println("attributeRemoved(HttpSessionBindingEvent event)");

 }

 @Override
 public void attributeReplaced(HttpSessionBindingEvent event) {
  System.out.println("attributeReplaced(HttpSessionBindingEvent event)");

 }

}

 

========================================

attributeReplaced(ServletRequestAttributeEvent event)
attributeReplaced(HttpSessionBindingEvent event)
here call reset() method
here call setScore() method
here call setSname() method
here call setMajor() method

addstudentform==form

 


001 zhangsan
001 zhangsan
004 wang
003 sun
006 bai
007 tian
008 zhu
120 zheng
119 qian
002 zhao
110 guo
005 ming
138 jian
001 zhangsan
139 jia
八月 30, 2012 5:10:28 下午 org.apache.catalina.core.StandardContext reload
信息: Reloading Context with name [/StructsWeb] has started
八月 30, 2012 5:10:29 下午 org.apache.struts.action.ActionServlet initChain
信息: Loading chain catalog from jar:file:/D:/Documents%20and%20Settings/hanbinfeng/workspace2/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/StructsWeb/WEB-INF/lib/struts-core-1.3.10.jar!/org/apache/struts/chain/chain-config.xml
attributeAdded(ServletRequestAttributeEvent event)==org.apache.struts.action.MODULE
八月 30, 2012 5:10:39 下午 org.apache.struts.chain.ComposableRequestProcessor init
信息: Initializing composable request processor for module prefix ''
attributeReplaced(ServletRequestAttributeEvent event)
attributeAdded(ServletRequestAttributeEvent event)==org.apache.struts.globals.ORIGINAL_URI_KEY
attributeAdded(ServletRequestAttributeEvent event)==org.apache.struts.action.mapping.instance
Generate AddStudentForm()
attributeAdded(ServletRequestAttributeEvent event)==addStudentForm
attributeAdded(ServletRequestAttributeEvent event)==addStudentForm
name=addStudentForm value=com.itstruct.AddStudentForm@978b69
here call reset() method
here call setScore() method
here call setSname() method
here call setMajor() method
八月 30, 2012 5:10:39 下午 org.apache.struts.chain.commands.servlet.CreateAction createAction
信息: Initialize action of type: com.itstruct.AddStudentAction

addstudentform==form

 


001 zhangsan
001 zhangsan
004 wang
003 sun
006 bai
007 tian
008 zhu
120 zheng
119 qian
002 zhao
110 guo
149 kan
005 ming
138 jian
001 zhangsan
139 jia


 


2.从结果区查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值