我是靠谱客的博主 悦耳红牛,最近开发中收集的这篇文章主要介绍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.从结果区查看

最后

以上就是悦耳红牛为你收集整理的struts对对象属性的监听的全部内容,希望文章能够帮你解决struts对对象属性的监听所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部