Action 跟 Actionsupport 的区别

更新时间:2023-10-21 15:18:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Action 跟 Actionsupport 的区别

当我们在写action的时候,可以实现Action接口,也可以继承Actionsupport这个类.到底这两个有什么区别呢? Action接口有:

public static final java.lang.String SUCCESS = \public static final java.lang.String NONE = \public static final java.lang.String ERROR = \public static final java.lang.String INPUT = \public static final java.lang.String LOGIN = \

public abstract java.lang.String execute() throws java.lang.Exception 而Actionsupport这个工具类在实现了Action接口的基础上还定义了一个validate()方法,重写该方法,它会在execute()方法之前执行,如校验失败,会转入input处,必须在配置该Action时配置input属性。

另外,Actionsupport还提供了一个getText(String key)方法还实现国际化,该方法从资源文件上获取国际化信息.

这样在自定义标签时可以定义一个变量为new actionsupport对象实现国际化 ActionSupport类的作用

struts2 不要求我们自己设计的action类继承任何的struts基类或struts接口,但是我们为了方便实现我们自己的action,大多数情况下都会继承

com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。因为此类中实现了很多的实用借口,提供了很多默认方法,这些默认方法包括国际化信息的方法、默认的处理用户请求的方法等,这样可 以大大的简化Acion的开发。

Struts2中通常直接使用Action来封装HTTP请求参数,因此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法 ==========================

关于struts2标签取值的备忘录

取值要通过标签内使用%{};

当Action的valueStack中有该属性的值时,只需直接使用该属性的名字即可 当Action的valueStack中没有该属性的值时,比如在session,application范围中的属性值时,需要加#或者#attr.; 例子:

假设某Action中有person成员变量,在application中存在company属性 那么我们可以通过以下方法取值: //无法取到,因为company不在action的valueStack中

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

一次Action调用都会创建一个ActionContext

调用:ActionContext context = ActionContext.getContext() ValueStack由OGNL框架实现 可以把它简单的看作一个集合

Stack Object:放入stack中的对象,一般是action Stack Context(map):stack上下文,它包含一些列对象,包括request/session/attr/application map等。

EL:存取对象的任意属性,调用对象的方法,遍历整个对象结? ===========

WebWork2.2笔记(二)ActionSupport及其他基础知识

绝 大多数情况下,WebWork不是直接实现com.opensymphony.xwork.Action接口,而是扩展 com.opensymphony.xwork.ActionSupport类。ActionSupport实现了除Action以外的其他几个接口,主 要的几个接口是: com.opensymphony.xwork.Validateable com.opensymphony.xwork.Validateaware com.opensymphony.xwork.TextProvider com.opensymphony.xwork.LocaleProvider

这 些接口主要是提供给WebWork的拦截器使用的,接口配合拦截器可以实现AOP功能。比如Validateable接口和Validateaware 接口配合

DefailtWorkflowInterceptor就可以实现对用户输入进行检验的功能,当用户调用Action时,首先执行 Validateable接口定义的validate()方法,如果在这个方法中用户使用接口Validateaware中的方法设置了错误信息,则

DefaultWorkflowInterceptor会自动终止Action的执行,并产生一个INPUT的result,只有没有任何错误信息才会执 行Action的剩余部分。

前面我们定义的xwork.xml配置文件中包含了一句:

file=\,这个webwork-default.xml包含在webwork的jar包中。 webwork-default.xml预建了很多常用的result-type、interceptor和interceptor-stack。其中 interceptor和interceptor-stack用于定义webwork将要使用那些拦截器。interceptor定义一个拦截器,而 interceptor定义一组拦截器。如果一个Action使用一组拦截器,则这些拦截器将以其定义的顺序执行,可见,interceptor- stack中拦截器的顺序时很重要的。 在webwork-default.xml中有如下定义:

class=\name=\......

而名为validationWorkflowStack和completeStack这两个预建的interceptor-stack则包含了workflow这个interceptor。

下面做一个检查用户输入的练习,如果用户没有输入内容,则让用户重新输入,并且提示错误信息。 web.xml

与上次的相同。 xwork.xml

xml 代码 1.

2. \3.

4.

5.

7.

9.

10. 14.

15. package> 16. xwork>

input.jsp xml 代码

1. <%@taglib prefix=\2.

3. Input Somethingtitle>head> 4. <body> 5. </p><p>6. <ww:form action=\7. <ww:textfield label=\name=\8. <ww:submit>ww:submit> 9. ww:form> 10. </p><p>11. body> 12. html> </p><p>success.jsp xml 代码 </p><p> </p><p>1. <%@taglib prefix=\2. <html> </p><p>3. <head><title>Success!title>head> 4. <body> </p><p>5. <ww:property value=\6. body> 7. html> </p><p>============================== Action 的实例,总是放到value stack中。因为Action放在stack中,而stack是root(根对象),所以对Action中的属性的访问就可以省略#标记。但是,要访问 ActionContext中其它对象的属性,就必须要带上#标记,以便让OGNL知道,不是从根对象,而是从其它对象中去寻找。 </p><p>Struts2中OGNL,valueStack,stackContext的学习 </p><p>学习Struts2,一直不明白表单中的值是怎么传给Action的,上网查了些资料,基本了解了!下面基本是从几个人的BOLG转载过来,以后记不清了再来看~ [color=red]先看看我做的实验jsp页面 Java代码 </p><p><s:form action=\<s:textfield name=\点\<s:textfield name=\<s:textfield name=\<s:textfield name=\年龄\<s:textfield name=\日期\<s:submit name=\提交\</s:form> </p><p>结果图(是通过<s:debug></s:debug>得到的) value stack: </p><p>Stack context: </p><p>通过图中我们可以看到 </p><p>valuestack中包括我传递的值(point,point2,point3,age,date) </p><p>stack context中包括了 request application OgnlValueStack(root) session parameters 等属性 </p><p>值栈(ValueStack) </p><p>Struts2将OGNL上下文设置为Struts2中的ActionContext(内部使用的仍然是OgnlContext),并将值栈设为OGNL的根对象。 </p><p>我 们知道,OGNL上下文中的根对象可以直接访问,不需要使用任何特殊的“标</p><p>记”,而引用上下文中的其他对象则需要使用“#”来标记。由于值栈是上下文中的 根对象,因此可以直接访问。那么对于值栈中的对象该如何访问呢?Struts2提供了一个特殊的OGNLPropertyAccessor,它可以自动查 找栈内的所有对象(从栈顶到栈底),直接找到一个具有你所查找的属性的对象。也就是说,对于值栈中的任何对象都可以直接访问,而不需要使用 “#”。 假设值栈中有两个对象:student和employee,两个对象都有name属性,student有学号属性number, 而 employee有薪水属性salary。employee先入栈,student后入栈,位于栈顶,那么对于表达式name,访问的就是student 的name属性,因为student对象位于栈顶;表达式salary,访问的就是employee的salary属性。正如你所见,访问值栈中的对象属 性或方法,无须指明对象,也不用“#”,就好像值栈中的对象都是OGNL上下文中的根对象一样。这就是Struts2在OGNL基础上做出的改进。 </p><p>值栈中的Action实例 </p><p>Struts2框架总是把Action实例放在栈顶。因为Action在值栈中,而值栈又是OGNL中的根,所以引用Action的属性可以省略“#”标记,这也是为什么我们在结果页面中可以直接访问Action的属性的原因。 </p><p>Struts2中的命名对象 </p><p>Struts2还提供了一些命名对象,这些对象没有保存在值栈中,而是保存在ActionContext中,因此访问这些对象需要使用“#”标记。这些命名对象都是Map类型。 </p><p>parameters </p><p>用于访问请求参数。如:#parameters['id']或#parameters.id,相当于调用了HttpServletRequest对象的getParameter()方法。 </p><p>注意,parameters本质上是一个使用HttpServletRequest对象中的请求参数构造的Map对象,一量对象被创建(在调用Action实例之前就已经创建好了),它和HttpServletRequest对象就没有了任何关系。 </p><p>request </p><p>用于访问请求属性。如:#request['user']或#request.user,相当于调用了HttpServletRequest对象的getAttribute()方法。 </p><p>session </p><p>用于访问session属性。如:#session['user']或#session.user,相当于调用了HttpSession对象的getAttribute()方法。 </p><p>application </p><p>用于访问application属性。如:#application['user']或#application.user,相当于调用了ServletContext的getAttribute()方法。 </p><p>attr </p><p>如果PageContext可用,则访问PageContext,否则依次搜索request、session</p><p></p> <p>本文来源:<a href="https://www.bwwdw.com/article/akgf.html">https://www.bwwdw.com/article/akgf.html</a></p><span class="doc-download-e"></span> </div> <script type="text/javascript">s("download_bottom");</script> <div class="related_article"> <div class="related_top"><code>相关文章:</code></div> <ul><li><a href="https://www.bwwdw.com/article/akgf.html" target="_blank" title="Action 跟 Actionsupport 的区别">Action 跟 Actionsupport 的区别</a></li><li><a href="https://www.bwwdw.com/article/egja.html" target="_blank" title="自助游和跟团的区别">自助游和跟团的区别</a></li><li><a href="https://www.bwwdw.com/article/9uh5.html" target="_blank" title="自助游和跟团的区别">自助游和跟团的区别</a></li><li><a href="https://www.bwwdw.com/article/izaf.html" target="_blank" title="03定额跟10定额区别">03定额跟10定额区别</a></li><li><a href="https://www.bwwdw.com/article/rshd.html" target="_blank" title="纯银保健杯跟普通的杯子有哪些区别?">纯银保健杯跟普通的杯子有哪些区别?</a></li><li><a href="https://www.bwwdw.com/article/e022.html" target="_blank" title="action单元测试">action单元测试</a></li><li><a href="https://www.bwwdw.com/article/8wo4.html" target="_blank" title="Iterative-Refinement for Action Timing Discretization">Iterative-Refinement for Action Timing Discretization</a></li><li><a href="https://www.bwwdw.com/article/dj31.html" target="_blank" title="Double action Strike form歌词">Double action Strike form歌词</a></li><li><a href="https://www.bwwdw.com/article/iis1.html" target="_blank" title="QTP的Action间的信息共享的4种方法">QTP的Action间的信息共享的4种方法</a></li><li><a href="https://www.bwwdw.com/article/zqnb.html" target="_blank" title="原单正品代购是什么意思?跟专柜的有什么区别?哪里有渠道?">原单正品代购是什么意思?跟专柜的有什么区别?哪里有渠道?</a></li></ul> </div> <div class="in_reading"><p class="rel_art_line">正在阅读:</p><p><a target="_blank" href="https://www.bwwdw.com/article/akgf.html" title="Action 跟 Actionsupport 的区别">Action 跟 Actionsupport 的区别</a><span>10-21</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/ls9l.html" title="艺术高中是职高吗 二者有什么差别">艺术高中是职高吗 二者有什么差别</a><span>03-30</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/0r3b.html" title="读《雪》有感">读《雪》有感</a><span>02-22</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/v4ex.html" title="《1. 排序》教案1">《1. 排序》教案1</a><span>12-29</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/3se2.html" title="施工现场安全生产管理协议书">施工现场安全生产管理协议书</a><span>10-28</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/yup6.html" title="华南理工大学嵌入式 期中测试 整理和答案">华南理工大学嵌入式 期中测试 整理和答案</a><span>03-08</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/e6ve.html" title="七上Unit9My-favorite-subject-is-science-导学案">七上Unit9My-favorite-subject-is-science-导学案</a><span>05-08</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/red2.html" title="浙江省旅游局行政执法责任制实施方案">浙江省旅游局行政执法责任制实施方案</a><span>10-31</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/s2l.html" title="机能学实验基础知识与基本操作">机能学实验基础知识与基本操作</a><span>03-08</span></p><p><a target="_blank" href="https://www.bwwdw.com/article/3lpv.html" title="一例中学生考试焦虑咨询案例报告">一例中学生考试焦虑咨询案例报告</a><span>11-22</span></p></div> <div class="previous"> <span class="pre">上一篇:<a title="隔离开关触头温度检测系统" href="https://www.bwwdw.com/article/8kgf.html">隔离开关触头温度检测系统</a></span> <span class="next">下一篇:<a title="计算机网络课程设计任务书(1)" href="https://www.bwwdw.com/article/bkgf.html">计算机网络课程设计任务书(1)</a></span> </div> </div> </div> <div class="right-side"> <div class="right_fix"> <script type="text/javascript">s("right_top");</script> <div class="hotSearch"><div class="hotSearch_tl"><span></span>相关文章</div><ul><li><span>1</span><a href="https://www.bwwdw.com/article/72t1.html" title="【YUSA】080416『Double-Action Climax Form』" target="_blank">【YUSA】080416『Double-Action Climax Form』</a></li><li><span>2</span><a href="https://www.bwwdw.com/article/ay3n.html" title="跟岗培训的总结" target="_blank">跟岗培训的总结</a></li><li><span>3</span><a href="https://www.bwwdw.com/article/vc3q.html" title="闭环纠正措施CLCA(Close Loop Corrective Action)" target="_blank">闭环纠正措施CLCA(Close Loop Corrective Action)</a></li><li><span>4</span><a href="https://www.bwwdw.com/article/xscf.html" title="跟植物说话的男生" target="_blank">跟植物说话的男生</a></li><li><span>5</span><a href="https://www.bwwdw.com/article/43w2.html" title="跟外教学口语的网站" target="_blank">跟外教学口语的网站</a></li><li><span>6</span><a href="https://www.bwwdw.com/article/40j1.html" title="MES数据库存储迁移Action_PROD" target="_blank">MES数据库存储迁移Action_PROD</a></li><li><span>7</span><a href="https://www.bwwdw.com/article/etbm.html" title="眼力的区别" target="_blank">眼力的区别</a></li><li><span>8</span><a href="https://www.bwwdw.com/article/iu3f.html" title="岛礁的区别" target="_blank">岛礁的区别</a></li><li><span>9</span><a href="https://www.bwwdw.com/article/10g3.html" title="“质”的区别" target="_blank">“质”的区别</a></li><li><span>10</span><a href="https://www.bwwdw.com/article/l34e.html" title="国家承认高技毕业生的文凭吗_高技毕业生文凭跟大专文凭有什么区别" target="_blank">国家承认高技毕业生的文凭吗_高技毕业生文凭跟大专文凭有什么区别</a></li></ul></div> <script type="text/javascript">s("right_mid");</script> <div class="right_list"><div class="right_list_t"><i></i><span>最新文章</span></div><ul><li><a href="https://www.bwwdw.com/article/m4n6.html" target="_blank" title="发电电气运行规程1">发电电气运行规程1</a></li><li><a href="https://www.bwwdw.com/article/j4n6.html" target="_blank" title="英文简历">英文简历</a></li><li><a href="https://www.bwwdw.com/article/44n6.html" target="_blank" title="最全辅导员招聘考试题库">最全辅导员招聘考试题库</a></li><li><a href="https://www.bwwdw.com/article/14n6.html" target="_blank" title="4.3崇明岛的未来的样子">4.3崇明岛的未来的样子</a></li><li><a href="https://www.bwwdw.com/article/q4n6.html" target="_blank" title="2012年上海市普通高校招生二本批次各校投档分数线">2012年上海市普通高校招生二本批次各校投档分数线</a></li><li><a href="https://www.bwwdw.com/article/e4n6.html" target="_blank" title="江苏省如皋中学2017-2018学年第一学期高三第二次阶段测试12月数">江苏省如皋中学2017-2018学年第一学期高三第二次阶段测试12月数</a></li><li><a href="https://www.bwwdw.com/article/n4n6.html" target="_blank" title="农业转移人口社会参与机制浅谈">农业转移人口社会参与机制浅谈</a></li><li><a href="https://www.bwwdw.com/article/l4n6.html" target="_blank" title="2017-2018学年度牛津译林版8B英语初二期中试卷及答案">2017-2018学年度牛津译林版8B英语初二期中试卷及答案</a></li><li><a href="https://www.bwwdw.com/article/s4n6.html" target="_blank" title="家长委员会上的讲话">家长委员会上的讲话</a></li><li><a href="https://www.bwwdw.com/article/k4n6.html" target="_blank" title="05继电保护设备检修规程">05继电保护设备检修规程</a></li><li><a href="https://www.bwwdw.com/article/z4n6.html" target="_blank" title="组织行为学考试重点(陈春花)">组织行为学考试重点(陈春花)</a></li><li><a href="https://www.bwwdw.com/article/04n6.html" target="_blank" title="2016年云南省公务员考试《行测》模拟试卷(十七)">2016年云南省公务员考试《行测》模拟试卷(十七)</a></li><li><a href="https://www.bwwdw.com/article/c4n6.html" target="_blank" title="规避“10号文”红筹系列之案例分析">规避“10号文”红筹系列之案例分析</a></li><li><a href="https://www.bwwdw.com/article/94n6.html" target="_blank" title="钱寨小学学生读书活动评价方案">钱寨小学学生读书活动评价方案</a></li><li><a href="https://www.bwwdw.com/article/uen6.html" target="_blank" title="五大联赛派系">五大联赛派系</a></li><li><a href="https://www.bwwdw.com/article/y4n6.html" target="_blank" title="国际结算课件新">国际结算课件新</a></li><li><a href="https://www.bwwdw.com/article/6en6.html" target="_blank" title="材料科学导论 - 图文">材料科学导论 - 图文</a></li><li><a href="https://www.bwwdw.com/article/3en6.html" target="_blank" title="领导干部任前廉政法规考试模拟试题">领导干部任前廉政法规考试模拟试题</a></li><li><a href="https://www.bwwdw.com/article/gen6.html" target="_blank" title="汽车综合实训">汽车综合实训</a></li><li><a href="https://www.bwwdw.com/article/7en6.html" target="_blank" title="医疗质量管理目录">医疗质量管理目录</a></li><li><a href="https://www.bwwdw.com/Actionsupport/" target="_blank" title="Actionsupport">Actionsupport</a></li><li><a href="https://www.bwwdw.com/%E5%8C%BA%E5%88%AB/" target="_blank" title="区别">区别</a></li><li><a href="https://www.bwwdw.com/Action/" target="_blank" title="Action">Action</a></li></ul></div> <script type="text/javascript">s("right_bottom");</script> <div class="right_list"><div class="right_list_t"><i></i><span>推荐文章</span></div><ul><li><a href="https://www.bwwdw.com/article/wkgf.html" target="_blank" title="化工原理课程设计 - 图文">化工原理课程设计 - 图文</a></li><li><a href="https://www.bwwdw.com/article/okgf.html" target="_blank" title="浙江省2011年1月高等教育自学考试光纤通信原理试题">浙江省2011年1月高等教育自学考试光纤通信原理试题</a></li><li><a href="https://www.bwwdw.com/article/xkgf.html" target="_blank" title="最新医嘱书写规范">最新医嘱书写规范</a></li><li><a href="https://www.bwwdw.com/article/5kgf.html" target="_blank" title="南大网院行政法学第二次作业(1)">南大网院行政法学第二次作业(1)</a></li><li><a href="https://www.bwwdw.com/article/tkgf.html" target="_blank" title="南京三模作文审题及范文">南京三模作文审题及范文</a></li><li><a href="https://www.bwwdw.com/article/vkgf.html" target="_blank" title="牛津英语教学案7B Unit 5表格式">牛津英语教学案7B Unit 5表格式</a></li><li><a href="https://www.bwwdw.com/article/2kgf.html" target="_blank" title="读《小故事大成功》有感">读《小故事大成功》有感</a></li><li><a href="https://www.bwwdw.com/article/fkgf.html" target="_blank" title="2018年福建省宁德柘荣县中小学教师资格认定通知">2018年福建省宁德柘荣县中小学教师资格认定通知</a></li><li><a href="https://www.bwwdw.com/article/dkgf.html" target="_blank" title="keilc编译常见错误">keilc编译常见错误</a></li><li><a href="https://www.bwwdw.com/article/hkgf.html" target="_blank" title="雨量计知识总结">雨量计知识总结</a></li><li><a href="https://www.bwwdw.com/article/rkgf.html" target="_blank" title="对大学英语教学中跨文化交际能力培养的思考">对大学英语教学中跨文化交际能力培养的思考</a></li><li><a href="https://www.bwwdw.com/article/pkgf.html" target="_blank" title="石开公司发12号关于印发石油开发中心有限公司井控管理规定等2个规范性文件的通知">石开公司发12号关于印发石油开发中心有限公司井控管理规定等2个规范性文件的通知</a></li><li><a href="https://www.bwwdw.com/article/gkgf.html" target="_blank" title="互换性与技术测量(第五版)课后习题答案">互换性与技术测量(第五版)课后习题答案</a></li><li><a href="https://www.bwwdw.com/article/7kgf.html" target="_blank" title="2015年秋七年级语文期中考试试卷(附答案)">2015年秋七年级语文期中考试试卷(附答案)</a></li><li><a href="https://www.bwwdw.com/article/6kgf.html" target="_blank" title="苏教版四(下)语文精典复习(最新整理)">苏教版四(下)语文精典复习(最新整理)</a></li><li><a href="https://www.bwwdw.com/article/3kgf.html" target="_blank" title="青年文明号创建工作汇报">青年文明号创建工作汇报</a></li><li><a href="https://www.bwwdw.com/article/ukgf.html" target="_blank" title="软件工程第一讲教案">软件工程第一讲教案</a></li><li><a href="https://www.bwwdw.com/article/yngf.html" target="_blank" title="2011年上半年土木期末试卷AB及答案">2011年上半年土木期末试卷AB及答案</a></li><li><a href="https://www.bwwdw.com/article/cngf.html" target="_blank" title="手持GPS坐标系转换的心得体会">手持GPS坐标系转换的心得体会</a></li><li><a href="https://www.bwwdw.com/article/9ngf.html" target="_blank" title="高层建筑消防给水的超压起因与防治措施">高层建筑消防给水的超压起因与防治措施</a></li></ul></div> </div> </div> </div> <div class="footer"> <p>Copyright©<script>timestamp2date(1);</script><a href="https://www.bwwdw.com/" target="_blank" title="博文网">博文网</a>bwwdw.com 版权所有</p> <p class="gray"><a href="https://www.bwwdw.com/article/" target="_blank">最新更新</a> | <a href="https://www.bwwdw.com/hot/" target="_blank">热点专题</a> | <a href="https://www.bwwdw.com/sitemap.html" target="_blank">网站地图</a> | <a href="https://www.bwwdw.com/tag/" target="_blank">TAG专题</a> | <a href="https://www.bwwdw.com/sitemap.xml" target="_blank">XML地图</a> | <a href="https://so.bwwdw.com" target="_blank">范文搜索</a><script type="text/javascript">tj();</script></p> </div> <a href="#0" class="cd-top">Top</a> <script src="/static/fanwen/js/jquery-1.9.1.min.js"></script> <script type="text/javascript"> document.write('<script type="text/javascript" src="/static/fanwen/js/pubuliu.js?'+RAND_STR+'"><\/script>'); document.write('<script type="text/javascript" src="/static/fanwen/js/lazyimg.js?'+RAND_STR+'"><\/script>'); document.write('<script type="text/javascript" src="/static/fanwen/js/gotop.js?'+RAND_STR+'"><\/script>'); </script> <script type="text/javascript"> $.ajax({ "url":"https://www.bwwdw.com/open/doc/readViews.json?id=akgf", "type":"get", "data":"", "dataType":"json", "success":function(res){ $("#read_views").html(res.data); } }); </script> <script type="text/javascript">bottomAction();</script> </body> </html>