`
自动放假
  • 浏览: 23376 次
文章分类
社区版块
存档分类
最新评论

action attribute和name 属性区别

 
阅读更多

、在一般情况下,actionForm是被存储在一定的scope中(request或session,通过action的scope属性来配置),当我们在配置时,指定name而不指定attribute,那么指定的name值就作为actionForm存储在scope中的key值,我们可以在action中通过httpServletRequest.getAttribute("指定的name属性值")来获得这个actionForm;当我们既配置了name又配置了attribute,那么actionForm存储在scope中的key值就采用attribute属性指定的值了,这时要通过httpServletRequest.getAttribute("指定的attribute属性值")来获得actionForm,此时通过httpServletRequest.getAttribute("指定的name属性值")是不能获得actionForm的。

所以,是否配置attribute属性就决定了actionForm存储在scope中的key值是采用name,还是采用attribute
 2、 在《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的源代码:


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源代码:


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;

分享到:
评论

相关推荐

    struts基于mvc的开发代码

    <forward name="test1" path="/test1.jsp" /> <forward name="test2" path="/test2.jsp" /> <forward name="test3" path="/test3.jsp" /> <forward name="scope" path="/sure.jsp" /> <action-mappings > ...

    struts2的入门开发

    <action attribute="loginForm" input="/login.jsp" name="loginForm" path="/login" scope="request" type="test.action.LoginAction"> <forward name="success" path="/loginSuccess.jsp"/> <forward name=...

    最新Struts2+jq+ajax+json 学会总要4步‵‵超级简单,里面包含实例

    最新Struts2.3.8 + jquery + ajax + json 学会struts+jq+ajax+json只要4步‵‵经过作者的总结超级简单 1. 导入struts2 及json包 asm-3.3.jar ...如:json_targer.attributeName 访问attaributeName中的信息

    SSH开发纪要整合解决四大问题(中文、jar包冲突、延时加载、模块化)文档

    attribute="addForm" input="/add.jsp" --错误时跳转的页面 name="addForm" --Frombean的名字 path="/add" --路径 scope="request" --作用域 type="com.lmf118.struts.action.AddAction"> ...

    Struts2 国际化字符串 拦截器

    这些都过result里的type(类型)属性(Attribute)定义的。另外,您还可以自定义result类型。 下面让我们来做一个Velocity模板输出的例子,首先在classes/struts.xml中新建一个Action映射(Mapping),将其result...

    SprngMV浅析

    Sp是结构最清晰的MVC Model 2实现。它的Action不叫Action,而是称做Controller;Controller接收request, ...Model则需要通过其它的途径(如request.attribute,Context参数,或Action本身的属性数据)传递过去。

    springMVC架构学习交流

    Spring MVC是结构最清晰的MVC Model 2实现。它的Action也不叫Action,而是称做Controller;...Model则需要通过其它的途径(如request.attribute,Context参数,或Action本身的属性数据)传递上去。

    springMVC入门_Java系列教程

    Spring MVC是结构最清晰的MVC Model 2实现。它的Action也不叫Action,而是称做Controller;...Model则需要通过其它的途径(如request.attribute,Context参数,或Action本身的属性数据)传递上去。

    Struts in Action中文版

    2.6.2. Struts的强项........................................................................................................58 Struts in Action 中文版 Lastest Revised:10/14/2005 10:27:00 AM ...

    struts配置元素详解

    ”1.0” encoding=”ISO-8859-1”?> <!DOCTYPE struts-config PUBLIC "-//Apache Software ...attribute="" type="" name="" scope="" validate="" input="" / > </action-mappings> </struts-config>

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    14 <action attribute="LoginForm" input="/login.jsp" name="LoginForm" path="LoginAction" scope="request" type="com.test.web.action.LoginAction" validate="true"> 15 <forward name="faile" path="faile.jsp...

    file-upload-action:文件上传处理操作

    // add action to controller public function actions() { return array( // ... 'upload' =>array( 'class' => 'ext.yiiext.actions.fileUpload.EFileUploadAction' , // The data model which ...

    自己写的mvc框架基于NVelocity

    基于nvelocity的mvc框架,用到AppDomain动态加载程序集,反射等机制,通过方法和类的Attribute匹配Controller和Action,实现自定义url,自定义Controller和Action,不需要像Asp.net Mvc那样方法名就是ActionName类名...

    SSD7 选择题。Multiple-Choice

    (c) A component attribute can be a composite attribute. (d) A component attribute always contains other components. Correct answer is (c) 3. In the Entity-Relationship model, properties that ...

    vuex-models:vue组件和vuex存储之间的简单双向数据绑定

    Vuex模型 该软件包旨在通过提供可生成v-model兼容的计算属性的getter/action/mutation/state生成器和mapper来简化vuex状态下v-model使用。安装只需使用npm: npm i --save vuex-models 用法使用vuex-models非常简单-...

    struts in Action

    2.6. Struts 的长处和弱点...........................................................................................55 2.6.1. 弱点..........................................................................

    Physics of negative index material

    components, to name only a few of the possibilities. Negative index materials are generally, but not always associated with negative refracting materials, and have the added property of having the ...

    Struts原理、开发及项目实施

    RegUserForm "/> </form-beans> <br/><action-mappings> <action path="/regUserAction" type=" org.cjea.Struts.example.RegUserAction " attribute=" regUserForm " scope="request...

    2009 达内Unix学习笔记

    命令和参数之间必需用空格隔开,参数和参数之间也必需用空格隔开。 一行不能超过256个字符;大小写有区分。 二、特殊字符含义 文件名以“.”开头的都是隐藏文件/目录,只需在文件/目录名前加“.”就可隐藏它。...

    Implementing 802.1X Security Solutions for Wired and Wireless Networks.pdf

    Termination-Action Attribute 90 Contents xiii 68608ftoc.qxd:Layout 1 2/18/08 9:55 PM Page xiii Authentication Server Selection Considerations 90 Attributes 91 EAP-Methods 91 Chapter 5 EAP-Methods ...

Global site tag (gtag.js) - Google Analytics