﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-◎ヤ撧吥菔瀭o┊。&#x7;``。-随笔分类-Struts</title><link>http://www.blogjava.net/hllwuxin/category/25992.html</link><description>刪除昨天啲煩惱.﹖選擇今天啲快樂.﹖設置明天啲幸福.﹖ </description><language>zh-cn</language><lastBuildDate>Thu, 20 Sep 2007 05:31:28 GMT</lastBuildDate><pubDate>Thu, 20 Sep 2007 05:31:28 GMT</pubDate><ttl>60</ttl><item><title> LookupDispatchAction使用示例</title><link>http://www.blogjava.net/hllwuxin/archive/2007/09/19/146445.html</link><dc:creator>優雅Ｄě頽廢</dc:creator><author>優雅Ｄě頽廢</author><pubDate>Wed, 19 Sep 2007 05:31:00 GMT</pubDate><guid>http://www.blogjava.net/hllwuxin/archive/2007/09/19/146445.html</guid><wfw:comment>http://www.blogjava.net/hllwuxin/comments/146445.html</wfw:comment><comments>http://www.blogjava.net/hllwuxin/archive/2007/09/19/146445.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hllwuxin/comments/commentRss/146445.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hllwuxin/services/trackbacks/146445.html</trackback:ping><description><![CDATA[<div class="g_t_left c07 content" id="blogtext__fks_C7G4Ko7-pHAFD6ZGnFlv9OGMEn5dYI0i">
<p>LookupDispatchAction是DispatchAction的子类。他的作用是将多个响应用户请求的Action方法放置在同一个Action中。</p>
<p>LookupDispatchAction主要适合用于页面中同一个表单具有多个提交按钮，每个提交按钮要递交给不同的Action处理方法的情况。</p>
<p>LookupDispatchAction所执行的方法也是依据页面传递的参数来确定的。而具体执行哪个Action方法则是由用户提交参数的值、资源文件、定义的映射方法所共同决定的。</p>
<p><strong><font style="background-color: #ffffff" color="#ff0000" size="4">具体请参考下面的例题：</font></strong></p>
<p><font color="#0000ff"><strong>1、先在JSP页面中使用如下的提交按钮：</strong></font></p>
<p><strong><font color="#0000ff">#test.jsp</font></strong></p>
<p>&nbsp; &lt;html:form action="/test"&gt;<br />
&nbsp;&nbsp; &nbsp;&nbsp; &lt;!--property属性的值一定与ActionMapping中的parameter属性的值相同--&gt;<br />
&nbsp;&nbsp;&nbsp; &nbsp;&lt;html:submit property="method"&gt;&lt;bean:message key="button.add"/&gt;&lt;/html:submit&gt; <br />
&nbsp;&nbsp;&nbsp; &nbsp;&lt;html:submit property="method"&gt;&lt;bean:message key="button.delete"/&gt;&lt;/html:submit&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/html:form&gt;</p>
<p><strong><font color="#0000ff">2、在Struts的配置文件中设置paramerter属性：</font></strong></p>
<p><strong><font color="#0000ff">#struts-config.xml</font></strong></p>
<p>&lt;!-- parameter属性指页面中按钮的名称,其中的name属性中的ActionForm随便建个就行我的是空的 --&gt;</p>
<p>&lt;action path="/test" type="action.TestLookUpDispatchAction" scope="request" input="test.jsp" parameter="method" &gt;&lt;/action&gt; </p>
<p><font color="#0000ff"><strong>3、创建一个继承LookupDispatchAction的类，名叫TestLookUpDispatchAction放在action包下：</strong></font></p>
<p><strong><font color="#0000ff">#TestLookUpDispatchAction.java</font></strong></p>
<p>package action;</p>
<p>import java.util.HashMap;<br />
import java.util.Map;</p>
<p>import javax.servlet.http.HttpServletRequest;<br />
import javax.servlet.http.HttpServletResponse;</p>
<p>import org.apache.struts.action.ActionForm;<br />
import org.apache.struts.action.ActionForward;<br />
import org.apache.struts.action.ActionMapping;<br />
import org.apache.struts.actions.LookupDispatchAction;</p>
<p>public class TestLookUpDispatchAction extends LookupDispatchAction {</p>
<blockquote dir="ltr" style="margin-right: 0px">
<p>&nbsp;@Override<br />
&nbsp;protected Map getKeyMethodMap() {// 定义资源文件关键字与Action方法之间的映射关系<br />
&nbsp;&nbsp;&nbsp;&nbsp; Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();<br />
&nbsp;&nbsp;&nbsp;&nbsp; map.put("button.add", "add");<br />
&nbsp;&nbsp;&nbsp; &nbsp;map.put("button.delete", "delete");<br />
&nbsp;&nbsp;&nbsp; &nbsp;return map;<br />
&nbsp;}</p>
<p>&nbsp;// 自定义的方法<br />
&nbsp;public ActionForward add(ActionMapping mapping, ActionForm form,<br />
&nbsp;&nbsp;&nbsp;HttpServletRequest request, HttpServletResponse response)<br />
&nbsp;&nbsp;&nbsp;throws Exception {<br />
&nbsp;&nbsp;System.out.println("执行add方法成功");<br />
&nbsp;&nbsp;return null;<br />
&nbsp;}</p>
</blockquote>
<p dir="ltr">&nbsp;// 自定义的方法<br />
&nbsp;public ActionForward delete(ActionMapping mapping, ActionForm form,<br />
&nbsp;&nbsp;&nbsp;HttpServletRequest request, HttpServletResponse response)<br />
&nbsp;&nbsp;&nbsp;throws Exception {<br />
&nbsp;&nbsp;System.out.println("执行delete方法成功");<br />
&nbsp;&nbsp;return null;<br />
&nbsp;}<br />
}<br />
<font color="#0000ff"><strong>4、在资源文件中为每个标记定义显示信息：</strong></font></p>
<p dir="ltr"><strong><font color="#0000ff">message.properties</font></strong></p>
<p dir="ltr">button.add=add<br />
button.delete=delete</p>
<p dir="ltr">这样就OK了，你可以试一下是否能调用到自己想要的方法。有什么问题可以联系我。</p>
</div>
<img src ="http://www.blogjava.net/hllwuxin/aggbug/146445.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hllwuxin/" target="_blank">優雅Ｄě頽廢</a> 2007-09-19 13:31 <a href="http://www.blogjava.net/hllwuxin/archive/2007/09/19/146445.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>