- 浏览: 693293 次
- 来自: ...
文章分类
最新评论
-
ranguisheng:
可以加点注释,代码不便于阅读.
用java实现数据库连接池的一个简单示例 -
abcd880129:
这样写的话,还要用专门的下载工具,不然,只能下到一个为空的ex ...
ZK下载文件时,不在服务器生成文件直接下载 -
234369425:
同上,是20
java计算阶乘 -
CodeToMyLaw:
如果目录中含有中文呢?
[^\x00-\xff] 中文的 ...
js验证文件目录格式的正确性 -
yanzhoupuzhang:
加了,还是报那个错误!
org.apache.commons.dbcp.BasicDataSource的解决方法
转载:http://hi.baidu.com/javajavajava/blog
org.apache.struts.actions.LookupDispatchAction类别是DispatchAction类别的子类,与DispatchAction类似的是,它透过请求上的参数来决定该执行哪一个方法,不过LookupDispatchAction多了查询讯息资源文件的功能,LookupDispatchAction的用处之后,就是当一个窗体中包括两个以上同名的送出按钮时,可以透过查询讯息资源文件来确定相对应的动作。
直接以实例来说明,在继承LookupDispatchAction之后,您要重新定义getKeyMethodMap()方法,并定义好自己的相关处理方法,例如:
代码:
假设讯息资源文件中包括以下的讯息:
代码:
为了要使用LookupDispatchAction,我们如同DispatchAction一样在struts-config.xml中定义请求参数中该有的名称:
代码:
现在假设您的窗体页面包括以下的内容:
代码:
这些Struts自订卷标在执行后会产生以下的内容:
代码:
所以当您按下任一个按钮时,请求参数中会包括method=Continue或是method=Checkout,假设是method=Continue
好了,LookupDispatchAction会根据它作为value,在讯息信息文件找到对应的key,然后根据key与
getKeyMethodMap()得知要执行的方法为continue()方法。
********************************************************
Struts DispatchAction的如何在表单带method参数提交
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类我想用DispatchAction来做一个从表单添加,修改,删除记录的功能
<html:form action="/MyDispatchAction">
MyDispatchAction中有add,alert,delete等方法,问题是如何让表单提交的时候加上参数呢?
比如:按下add button实现 MyDispathAction?method=add这样的一次提交?
1.使用 DispatchAction
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类。与其它Action类仅提供单个execute()方法实现单个业务不同,DispatchAction允许你在单个Action类中编写多个与业务相关的方法。这样可以减少Action类的数量,并且把相关的业务方法集合在一起使得维护起来更容易。
要使用DispatchAction的功能,需要自己创建一个类,通过继承抽象的DispatchAction得到。对每个要提供的业务方法必须有特定的方法signature。例如,我们想要提供一个方法来实现对购物车添加商品清单,创建了一个类ShoppingCartDispatchAction提供以下的方法:
那么,这个类很可能还需要一个deleteItem()方法从客户的购物车中删除商品清单,还有clearCart()方法清除购物车等等。这时我们就可以把这些方法集合在单个Action类,不用为每个方法都提供一个Action类。
在调用ShoppingCartDispatchAction里的某个方法时,只需在URL中提供方法名作为参数值。就是说,调用addItem()方ǖ?URL看起来可能类似于:
http://myhost/storefront/action/cart?method=addItem
其中method参数指定ShoppingCartDispatchAction中要调用的方法。参数的名称可以任意配置,这里使用的"method"只是一个例子。参数的名称可以在Struts配置文件中自行设定。
=================================================================
2.使用 LookupDispatchAction
org.apache.struts.actions.LookupDispatchAction类:
通常LookupDispatchAction主要应用于在一个表单中有多个提交按钮,而这些按钮又有一个共同的名字的场合,这些按钮的名字和具体的ActionMapping的parameter属性相对应。
配置LookupDispatchAction时,应该在<action>元素中,把parameter属性设置为"action",使它和<html:submit>标签的property属性相一致。
做一个隐藏变量就可以了
然后用JS判断一下
定义一个hidden的元素,JS控制提交的参数
用这个类用多了就比较乱了,form的表单映射会烦死你。
同样用楼上老兄的方法就可以,这个用在同个页面上多个提交时候使用比较合适。
如果仅仅是一个页面一个提交的话,还有个更简单的方法。
<html:form action="/MyDispatchAction?action=add">
加个参数不就行了么~在action里面验证这个参数,如果有form的话加个getset方法同样验证加个注释也可以解决问题。
An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. To configure the use of this action in your struts-config.xml file, create an entry like this:
<action path="/test"
type="org.example.MyAction"
name="MyForm"
scope="request"
input="/test.jsp"
parameter="method"/>
which will use the value of the request parameter named "method" to locate the corresponding key in ApplicationResources. For example, you might have the following ApplicationResources.properties:
button.add=Add Record
button.delete=Delete Record
And your JSP would have the following format for submit buttons:
<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
Your subclass must implement both getKeyMethodMap and the methods defined in the map. An example of such implementations are:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}
Notes - If duplicate values exist for the keys returned by
getKeys, only the first one found will be returned. If no corresponding key
is found then an exception will be thrown. You can override the
method unspecified to provide a custom handler. If the submit
was cancelled (a html:cancel button was pressed), the custom
handler cancelled will be used instead.
org.apache.struts.actions.LookupDispatchAction类别是DispatchAction类别的子类,与DispatchAction类似的是,它透过请求上的参数来决定该执行哪一个方法,不过LookupDispatchAction多了查询讯息资源文件的功能,LookupDispatchAction的用处之后,就是当一个窗体中包括两个以上同名的送出按钮时,可以透过查询讯息资源文件来确定相对应的动作。
直接以实例来说明,在继承LookupDispatchAction之后,您要重新定义getKeyMethodMap()方法,并定义好自己的相关处理方法,例如:
代码:
package onlyfun.caterpillar; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.struts.actions.*; public class ShoppingAction extends LookupDispatchAction { protected Map getKeyMethodMap() { Map map = new HashMap(); map.put("button.continue", "continue"); map.put("button.checkout", "checkout"); return map; } public ActionForward continue(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // ...... } public ActionForward checkout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // ...... } }
假设讯息资源文件中包括以下的讯息:
代码:
button.continue=Continue button.checkout=Checkout
为了要使用LookupDispatchAction,我们如同DispatchAction一样在struts-config.xml中定义请求参数中该有的名称:
代码:
<action path="/shopping" type="onlyfun.caterpillar.ShoppingAction" parameter="method" name="cartForm"/>
现在假设您的窗体页面包括以下的内容:
代码:
<html:form action="/shopping"> <html:submit property="method"> <bean:message key="button.continue"/> </html:submit> <html:submit property="method"> <bean:message key="button.checkout"/> </html:submit> </html:form>
这些Struts自订卷标在执行后会产生以下的内容:
代码:
<form name="cartForm" method="post" action="/HelloStruts/shopping.do"> <input type="submit" name="method" value="Continue"/> <input type="submit" name="method" value="Checkout"/> </form>
所以当您按下任一个按钮时,请求参数中会包括method=Continue或是method=Checkout,假设是method=Continue
好了,LookupDispatchAction会根据它作为value,在讯息信息文件找到对应的key,然后根据key与
getKeyMethodMap()得知要执行的方法为continue()方法。
********************************************************
Struts DispatchAction的如何在表单带method参数提交
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类我想用DispatchAction来做一个从表单添加,修改,删除记录的功能
<html:form action="/MyDispatchAction">
MyDispatchAction中有add,alert,delete等方法,问题是如何让表单提交的时候加上参数呢?
比如:按下add button实现 MyDispathAction?method=add这样的一次提交?
1.使用 DispatchAction
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类。与其它Action类仅提供单个execute()方法实现单个业务不同,DispatchAction允许你在单个Action类中编写多个与业务相关的方法。这样可以减少Action类的数量,并且把相关的业务方法集合在一起使得维护起来更容易。
要使用DispatchAction的功能,需要自己创建一个类,通过继承抽象的DispatchAction得到。对每个要提供的业务方法必须有特定的方法signature。例如,我们想要提供一个方法来实现对购物车添加商品清单,创建了一个类ShoppingCartDispatchAction提供以下的方法:
那么,这个类很可能还需要一个deleteItem()方法从客户的购物车中删除商品清单,还有clearCart()方法清除购物车等等。这时我们就可以把这些方法集合在单个Action类,不用为每个方法都提供一个Action类。
在调用ShoppingCartDispatchAction里的某个方法时,只需在URL中提供方法名作为参数值。就是说,调用addItem()方ǖ?URL看起来可能类似于:
http://myhost/storefront/action/cart?method=addItem
其中method参数指定ShoppingCartDispatchAction中要调用的方法。参数的名称可以任意配置,这里使用的"method"只是一个例子。参数的名称可以在Struts配置文件中自行设定。
=================================================================
2.使用 LookupDispatchAction
org.apache.struts.actions.LookupDispatchAction类:
通常LookupDispatchAction主要应用于在一个表单中有多个提交按钮,而这些按钮又有一个共同的名字的场合,这些按钮的名字和具体的ActionMapping的parameter属性相对应。
配置LookupDispatchAction时,应该在<action>元素中,把parameter属性设置为"action",使它和<html:submit>标签的property属性相一致。
做一个隐藏变量就可以了
然后用JS判断一下
<SCRIPT LANGUAGE="javascript"> <!-- function SetAction(opType) { document.name1.action.value = opType document.name1.submit(); } //--> </SCRIPT> </head> <body> <html:form name="name1" action="/Del" type="XX.XX.Del"> <html:hidden property="action" /> <html:button property="update" value="UDDATE" onclick="SetAction’updateDsp’);"></html:button> <html:button property="add" value="ADD" onclick="SetAction’addDsp’);"></html:button> </html:form> </body> </HTML>
定义一个hidden的元素,JS控制提交的参数
用这个类用多了就比较乱了,form的表单映射会烦死你。
同样用楼上老兄的方法就可以,这个用在同个页面上多个提交时候使用比较合适。
如果仅仅是一个页面一个提交的话,还有个更简单的方法。
<html:form action="/MyDispatchAction?action=add">
加个参数不就行了么~在action里面验证这个参数,如果有form的话加个getset方法同样验证加个注释也可以解决问题。
An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. To configure the use of this action in your struts-config.xml file, create an entry like this:
<action path="/test"
type="org.example.MyAction"
name="MyForm"
scope="request"
input="/test.jsp"
parameter="method"/>
which will use the value of the request parameter named "method" to locate the corresponding key in ApplicationResources. For example, you might have the following ApplicationResources.properties:
button.add=Add Record
button.delete=Delete Record
And your JSP would have the following format for submit buttons:
<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
Your subclass must implement both getKeyMethodMap and the methods defined in the map. An example of such implementations are:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}
Notes - If duplicate values exist for the keys returned by
getKeys, only the first one found will be returned. If no corresponding key
is found then an exception will be thrown. You can override the
method unspecified to provide a custom handler. If the submit
was cancelled (a html:cancel button was pressed), the custom
handler cancelled will be used instead.
发表评论
-
浅析Struts1和Struts2的Action线程安全问题
2013-11-30 22:23 456【问题描述】最近公司安排我面试Java的FreshMan,面试 ... -
Struts 的动态复选框
2009-05-05 17:49 1272在用户界面设计中,复 ... -
struts-config.xml的配置
2009-04-30 11:34 1263在Struts应用启动时,会把Struts配置文件中的配置信息 ... -
Struts源码研究 - logic-Iterator标签篇
2009-02-25 23:13 1379转载:http://hi.baidu.com/ja ... -
Struts源码研究 - Bean-Message标签篇
2009-02-25 12:15 1446转载:http://hi.baidu.com/javajava ... -
struts中的DispatchAction、LookupDispatchAction、Mappin
2009-02-25 12:10 1531转载:http://hi.baidu.com/javajava ... -
ForwardAction、IncludeAction、SwitchAction
2009-02-25 10:00 1315转载:http://hi.baidu.com/javajava ... -
org.apache.commons.dbcp.BasicDataSource的解决方法
2009-02-24 15:39 63815转载: http://hi.baidu.com/javajav ... -
struts中的分页
2009-02-24 15:34 1215转载:http://hi.baidu.com/javajava ... -
一个简单的struts实现文件上传示例
2009-02-24 13:56 1415转载: http://hi.baidu.com/javajav ... -
struts 1 中服务器端限制文件上传的类型及扩展名
2009-02-24 12:53 3370转载: http://hi.baidu.com/javajav ... -
struts 1 中的文件上传 Action中的部分代码
2009-02-24 12:42 1495转载:http://hi.baidu.com/javajava ...
相关推荐
充分描述LookupDispatchAction 的使用!对学习者很有帮助。
List_Map_LookupDispatchAction_Validate的使用~ 具体内容浏览giddy的MSN Space
DispatchAction的作用简单地说就是把原来我们写在多个acton里的操作放在同一个 action里处理。
因此,在对本系统进行架构设计的时候,考虑建立一个抽象的BaseAction类,该类继承LookupDispatchAction,实现LookupDispatchAction类中的getKeyMethodMap方法,在方法中返回本系统中请求参数值与资源文件中参数值的...
struts1学习资料:里面包含struts源码工程 ActionServlet DispatchAction Action LookUpDispatchAction的用法 还包含html logic bean tiles标签的详细使用方法和validate验证框架的具体案例
JavaEE框架 Struts_In_Action(中文版) Struts Action Struts_In_Action LookupDispatchAction DispatchAction 对Action讲的比较仔细,可以深入的了解Struts框架里的基本原理。
struts1.x和mysql整合的登陆例子,包括用户的增删改查,涉及到LookupDispatchAction,的多个submit提交,静态验证,和读取数据库properties文件