<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 11">
<meta name="Originator" content="Microsoft Word 11">
本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:
Struts1.x系列教程(18):使用DispatchAction类调用多个Action方法
有时在HTML form中需要提供多个提交按钮,这时LookupDispatchAction类就派上用场了。LookupDispatchAction是DispatchAction的子类,用于处理客户端代码中有一个Form含有多个提交按钮的情况。为了使用LookupDispatchAction,首先来编写客户端代码,代码如下:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
<html:formaction="myaction">
<html:submitproperty="action">
<bean:messagekey="submit.print"/>
</html:submit>
<html:submitproperty="action">
<bean:messagekey="submit.save"/>
</html:submit>
</html:form>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 11">
<meta name="Originator" content="Microsoft Word 11">
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">
<!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Times New Roman";}
/* Page Definitions */
@page
{}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
</style>
<![endif]-->
其中submit.print和submit.save是属性文件中的key。action实际上是提交按钮的name属性值。在提交时,会将property和<bean:message key="..."
/>作为请求参数和参数值传给Struts动作。如save按钮被单击时,请求URL为http://localhost:8080/.../myaction.do?action=save。其中save为submit.save在属性文件中对应的属性值,也就是说submit.save = save。
接下来我们编写一个LookupDispatchAction的子类,代码如下:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
packageaction;
importorg.apache.struts.actions.LookupDispatchAction;
publicclassMyLookupDispatchActionextendsLookupDispatchAction
{
//为了方便描述,在这里未使用属性文件,只是直接将key和value的映射添加到Map对象中
//读者可以在getKeyMethodMap方法中从属性文件中读取相应的key-value对
//来添加到Map对象中
protectedMapgetKeyMethodMap()
{
Mapm=newHashMap();
m.put("submit.print","print");
m.put("submit.save","save");
returnm;
}
publicActionForwardprint(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
{
//处理print代码
}
publicActionForwardsave(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
{
//处理save代码
}
}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Times New Roman";}
/* Page Definitions */
@page
{}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
</style>
<![endif]-->
在编写LookupDispatchAction的子类时要注意必须实现getKeyMethodMap方法,在这个方法中需要进行key和Action方法的映射。下面的代码用来配置MyLookupDispatchAction。
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><actionpath="/moresubmit"type="action.MyLookupDispatchAction"parameter="action"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Times New Roman";}
/* Page Definitions */
@page
{}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
</style>
<![endif]-->
其中parameter属性指定了请求URL中由提交按钮生成的请求参数名。
下一篇:Struts1.x系列教程(20):使用EventDispatchAction类处理一个form多个submit
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!《银河系列原创教程》发布《Java Web开发速学宝典》出版,欢迎定购
分享到:
相关推荐
struts1.x和mysql整合的登陆例子,包括用户的增删改查,涉及到LookupDispatchAction,的多个submit提交,静态验证,和读取数据库properties文件
List_Map_LookupDispatchAction_Validate的使用~ 具体内容浏览giddy的MSN Space
struts1学习资料:里面包含struts源码工程 ActionServlet DispatchAction Action LookUpDispatchAction的用法 还包含html logic bean tiles标签的详细使用方法和validate验证框架的具体案例
充分描述LookupDispatchAction 的使用!对学习者很有帮助。
其中的LookupDispatchAction类是允许你指定一个具有多个方法的类,每一个方法的调用都基于配置文件中指定的一个特殊请求参数值,利用该参数值反向查询资源绑定,并将它与类中的一个方法进行匹配。从这些功能可以看出...
JavaEE框架 Struts_In_Action(中文版) Struts Action Struts_In_Action LookupDispatchAction DispatchAction 对Action讲的比较仔细,可以深入的了解Struts框架里的基本原理。
DispatchAction的作用简单地说就是把原来我们写在多个acton里的操作放在同一个 action里处理。