action-mappings之attribute属性

2023-10-28 19:08

本文主要是介绍action-mappings之attribute属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

action-mappings之attribute属性 [转]

关键字: action-mappings,attribuite

action-mappings之attribute属性(zz)
在很多时候,action mapping中的attribute 和 name 这两个属性一直困扰我,今天我觉得是该痛下决心拔掉这颗钉子的时候了。

翻看一些资料。。。。。

在《Programming Jakarta Struts》这本书中的第四章“Configuring the Struts Application”中这样一段说明来分别阐述这两
个属性:(102页)
++++++++
atribute:
++++++++
The name of the request or session scope attribute under which the form bean for this action can be accessed.
A value is only allowed here if there is a form bean specified in the name attribute. This attribute is
optional and has no default value.

++++++++
name:
++++++++
The name of the form bean, if any, that is associated with this action. This value must be the name attribute
from one of the form-bean elements defined earlier. This attribute is optional and has no default value.

最初看这些真的还是不好区分这两者。 不过在仔细看过struts的源代码以后,豁然开朗。。。

下面主要对attribute进行解释,应为没有人会对name属性不了解的(呵呵。。。)


解释:在struts实例化actionform的时候,有两种情况:如果已经存在,那么从内存中取回;如果第一次实例化,那么创建,并放入内存。
这样就有一个问题了,struts是根据什么来取回并创建actionform的呢,答案就是attribute的值。让我们进入struts的源代码:

/**
*创建或者取回formbean方法
*该方法在:org.apache.struts.util.RequestUtils中
*/
   public static Actionform createActionform(
       HttpServletRequest request,
       ActionMapping mapping,
       ModuleConfig moduleConfig,
       ActionServlet servlet) {
       。。。。
       。。。
       // Is there a form bean associated with this mapping?
       //得到action mapping中attribute的值
       String attribute = mapping.getAttribute();
       。。。。
       。。。。
       Actionform instance = null;
       HttpSession session = null;
       //yes!!就在这里了,把创建以后的actionform放在request或者session里,看到放入的名字了么,就是mapping.getAttribute();
       if ("request".equals(mapping.getScope())) {
           instance = (Actionform) request.getAttribute(attribute);
       } else {
           session = request.getSession();
           instance = (Actionform) session.getAttribute(attribute);
       }
       。。。
       。。。
       
       
       }
       
       
下面又有一个问题浮出水面:如果我没有在action mapping中指定attribute呢,那struts 是如何解决的?
答案很简单,如果单从结果上看,此时struts使用的name的值,为什么呢,看struts源代码

   /**
    * The request-scope or session-scope attribute name under which our
    * form bean is accessed, if it is different from the form bean's
    * specified <code>name</code>.
    *该代码在:org.apache.struts.config.ActionConfig中
    */
   protected String attribute = null;

   public String getAttribute() {
   //yes!!!!就在这里,看到了吧,如果你没有设定attribute,那么struts 会把name的值拿过来用。呵呵。。。
       if (this.attribute == null) {
           return (this.name);
       } else {
           return (this.attribute);
       }
   }

   public void setAttribute(String attribute) {
       if (configured) {
           throw new IllegalStateException("Configuration is frozen");
       }
       this.attribute = attribute;
   }

这篇关于action-mappings之attribute属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/295315

相关文章

滚雪球学Java(87):Java事务处理:JDBC的ACID属性与实战技巧!真有两下子!

咦咦咦,各位小可爱,我是你们的好伙伴——bug菌,今天又来给大家普及Java SE啦,别躲起来啊,听我讲干货还不快点赞,赞多了我就有动力讲得更嗨啦!所以呀,养成先点赞后阅读的好习惯,别被干货淹没了哦~ 🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!! 环境说明:Windows 10

【Python报错已解决】AttributeError: ‘list‘ object has no attribute ‘text‘

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 文章目录 前言一、问题描述1.1 报错示例1.2 报错分析1.3 解决思路 二、解决方法2.1 方法一:检查属性名2.2 步骤二:访问列表元素的属性 三、其他解决方法四、总结 前言 在Python编程中,属性错误(At

HTML5自定义属性对象Dataset

原文转自HTML5自定义属性对象Dataset简介 一、html5 自定义属性介绍 之前翻译的“你必须知道的28个HTML5特征、窍门和技术”一文中对于HTML5中自定义合法属性data-已经做过些介绍,就是在HTML5中我们可以使用data-前缀设置我们需要的自定义属性,来进行一些数据的存放,例如我们要在一个文字按钮上存放相对应的id: <a href="javascript:" d

Unable to instantiate Action, goodsTypeAction, defined for 'goodsType_findAdvanced' in namespace '/

报错: Unable to instantiate Action, goodsTypeAction,  defined for 'goodsType_findAdvanced' in namespace '/'goodsTypeAction......... Caused by: java.lang.ClassNotFoundException: goodsTypeAction.......

Python中的属性装饰器:解锁更优雅的编程之道

引言 在Python的世界里,装饰器是一个强大的工具,它允许我们以一种非侵入性的方式修改函数或方法的行为。而当我们谈论“属性装饰器”时,则是在探讨如何使用装饰器来增强类中属性的功能。这不仅让我们的代码更加简洁、易读,同时也提供了强大的功能扩展能力。本文将带你深入了解属性装饰器的核心概念,并通过一系列实例展示其在不同场景下的应用,从基础到进阶,再到实际项目的实战经验分享,帮助你解锁Python编程

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

ConstraintLayout布局里的一个属性app:layout_constraintDimensionRatio

ConstraintLayout 这是一个约束布局,可以尽可能的减少布局的嵌套。有一个属性特别好用,可以用来动态限制宽或者高app:layout_constraintDimensionRatio 关于app:layout_constraintDimensionRatio参数 app:layout_constraintDimensionRatio=“h,1:1” 表示高度height是动态变化

Python中的私有属性与方法:解锁面向对象编程的秘密

在Python的广阔世界里,面向对象编程(OOP)是一种强大而灵活的方法论,它帮助我们更好地组织代码、管理状态,并构建可复用的软件组件。而在这个框架内,私有属性与方法则是实现封装的关键机制之一。它们不仅有助于隐藏类内部的具体实现细节,还能保护数据免受外部干扰。今天,让我们一起探索Python中私有属性与方法的魅力所在,了解它们如何在实际开发中发挥重要作用。 引言 随着软件系统变得越来越复杂,维

转:android ro.debuggable属性调试修改(mprop逆向)

android ro属性调试修改(mprop逆向)      大家都知道如果需要调试android 的程序,以下两个条件满足一个就行。第一是apk的配置文件内的AndroidManifest.xml的 android:debuggable=”true”,第二就是/default.prop中ro.debuggable=1。两种方式第一种通常是解包添加属性再打包,随着加壳软件以及apk校验等,容易出

用ajax json给后台action传数据要注意的问题

必须要有get和set方法   1 action中定义bean变量,注意写get和set方法 2 js中写ajax方法,传json类型数据 3 配置action在struts2中