|
用户名:snowyvalley 笔名:snowyvalley 地区: 行业:其他 |
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
并非精通java,但却专注java!
从windows下背叛
- 作者: snowyvalley 2006年09月13日, 星期三 16:15 回复(0) | 引用(0) 加入博采
猫生病了
猫生病了,老鼠大声朗诵诗集,并修剪盆花.
猫生病了,老鼠慢慢刷牙洗脸,并打扫房间.
猫生病了,老鼠倚在窗边欣赏夜色,并对流星许愿.
猫生病了,老鼠也病了,
它说:"我还是习惯那种时时紧张,处处小心,夜夜逃亡的生活."
午夜,我听到老鼠在叹息,且不再读诗了
网上发现的一个人的签名,细细想来好有意思呀。
- 作者: snowyvalley 2006年04月11日, 星期二 18:05 回复(1) | 引用(0) 加入博采
一个震惊了整个德国的动人故事
- 作者: snowyvalley 2006年04月2日, 星期日 15:29 回复(1) | 引用(0) 加入博采
要回家过年了!
- 作者: snowyvalley 2006年01月22日, 星期日 16:26 回复(3) | 引用(0) 加入博采
一个关于奔跑的故事
- 作者: snowyvalley 2005年12月12日, 星期一 09:40 回复(0) | 引用(0) 加入博采
手把手教你删除系统顽固文件【多图】
- 作者: snowyvalley 2005年12月12日, 星期一 09:14 回复(0) | 引用(0) 加入博采
how to Use LookupDispatchAction
Use LookupDispatchAction for a JavaScript-free dispatch
Many forms can be put to multiple uses. The fields we need to create a record are usually the same fields we use to update a record. Likewise, the same page can usually used to identify a record to delete, copy, or update a record.
A DispatchAction (see Tip #2) is a convenient way to collect related operations like this into a single Struts Action class. A field in the request identifies the operation, and so all the developer has to do is get the right value into the field. The simplest way to do this is to label the buttons for the operation, and give each button the same name. But that ties the presentation label to the business operation and confounds localization.
As an alternative, most developers use a JavaScript attached to the button to update the dispatch field, which in practice works quite well. If JavaScript is not enabled, the submit fails gracefully, and in practice most applications do rely JavaScript for essential operations.
If JavaScript is not an option, another good alternative is the LookupDispatchAction [org.apache.struts.actions.LookupDispatchAction]. This object is a little more work to setup that the original DispatchAction, but lets you use this technique without using JavaScript as a crutch.
As with the DispatchAction, the first step is to indicate the name of the dispatch parameter in the action-mapping element. In this case the parameter will the name given to each of the buttons, rather than a hidden field. Let's just call our buttons "submit", which is the default name the html:submit tag will use.
name="MyForm"
scope="request"
input="/test.jsp"
parameter="submit"/>
In our JSP, we can refer to the buttons in the usual way
Later, when the user selects a button, the form will pass the submit parameter, along with whatever message is Struts finds for "button.add" or "button.delete" in the resource bundle. In a localized application, this message could vary by the user's selected locale.
Using the conventional DispatchAction, this approach would problematic, since there would have to a Java method named for each message, and not all the messages might valid Java identifiers. This is where the magic of the LookupDispatchAction comes into play.
When you create your LookupDispatchAction subclass, along with the methods for the dispatch operations (see Tip #20) you must also implement a getKeyMethodMap method. This is a "hotspot" that the LookupDispatchAction will call.
Here's an example of the methods you might declare in your subclass:
protected Map getKeyMethodMap(ActionMapping mapping,
ActionForm form,
HttpServletRequest request) {
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");
}
Internally, the base action will lookup the messages for button.add and button.delete, and match those against the submit parameter. When it finds a match, it will then use either "add" or "delete" to call the corresponding methods.
So while the LookupDispatchAction means adding an extra method to your Action, it lets you skip putting a JavaScript in your form.
Both the DispatchAction and LookupDispatchAction are an excellent way to streamline your Struts action classes, and group several related operations into a single umbrella action.
So, how many dispatch actions do you need? Can you use a dispatch action to collect everything into a single action?
Most often not. In practice, you can easily use one dispatch action for any forms that share a common validation. Sharing a dispatch action between different form beans, or form beans that are validated differently, can start to make things harder rather than simpler. But the use of a dispatch action can easily half or quarter the number of action classes in most Struts application.
HTH, Ted.
Credits. The LookupDispatchAction was originated by Erik Hatcher, one of Struts many volunteer contributors. The examples used in this tip relied heavily on Erik's documentation. (Yeah reuse! Yeah JavaDocs!)
- 作者: snowyvalley 2005年11月30日, 星期三 16:42 回复(0) | 引用(0) 加入博采
Hibernate in Action 翻译计划
- 作者: snowyvalley 2005年11月4日, 星期五 12:54 回复(0) | 引用(0) 加入博采