在AppFuse框架中,struts action 采用了繼承DispatchAction的方法來實(shí)現(xiàn)一個(gè)Action里實(shí)現(xiàn)多種操作。
首先看看DispatchAction原理
DispatchAction在配置上于標(biāo)準(zhǔn)的Action稍有不同,就是要在Action配置中多一個(gè)parametr屬性,這個(gè)屬性將指導(dǎo)DispatchAction找到對應(yīng)的方法,例如這樣配置
<action
path="/persons"

type="com.lms.webapp.action.PersonAction"
name="personForm"
scope="request"
input="mainMenu"
parameter="method"
unknown="false"
validate="false" >
parameter的屬性值是可以任意起的,只要你記得在傳參數(shù)的時(shí)候統(tǒng)一就可以了。比如我寫了一個(gè)類似這樣的Action,它繼承自DispatchAction類,包含了三個(gè)操作方法,有Add(),Update(),Delete()。當(dāng)我想要調(diào)用這個(gè)Action的Update操作時(shí),提交的URL應(yīng)該類似這樣的:
http://localhost:8080/myapp/persons.do?method=update
在AppFuse中的應(yīng)用在AppFuse中,有個(gè)繼承DispatchAction的BaseAction類,每個(gè)Action都繼承BaseAction,因此在執(zhí)行Action的時(shí)候,都先執(zhí)行BaseAction里的execute()方法達(dá)到指導(dǎo)要執(zhí)行哪個(gè)操作。代碼如下
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)

throws Exception
{

if (isCancelled(request))
{

try
{
getMethod("cancel");
return dispatchMethod(mapping, form, request, response, "cancel");

} catch (NoSuchMethodException n)
{
log.warn("No 'cancel' method found, returning null");
return cancelled(mapping, form, request, response);
}
}

// Check to see if methodName indicated by request parameter
String actionMethod = getActionMethodWithMapping(request, mapping);

if (actionMethod != null)
{
return dispatchMethod(mapping, form, request, response, actionMethod);

} else
{

String[] rules =
{"edit", "save", "search", "view"};

for (int i = 0; i < rules.length; i++)
{
// apply the rules for automatically appending the method name

if (request.getServletPath().indexOf(rules[i]) > -1)
{
return dispatchMethod(mapping, form, request, response, rules[i]);
}
}
}
return super.execute(mapping, form, request, response);
}
代碼分析:
isCancelled(request)方法:if the current form's cancel button was pressed!進(jìn)而執(zhí)行cancel方法。
getActionMethodWithMapping方法來確定所要執(zhí)行的操作。在getActionMethod方法里,首先是判斷request里是否有method=methodName,如果有則返回methodName;如果沒有則判斷request里是否有包含method.methodName的parameter,有的話則返回截取"method."后面的methodName。
如果通過getActionMethodWithMapping方法取不到methodName,則判斷servletPath里是否有包含edit....等字符,以決定執(zhí)行什么操作。
如果以上都不能取到methodName,那么將執(zhí)行 return super.execute(mapping, form, request, response);通過查看DispatchAction源碼發(fā)現(xiàn)最終會執(zhí)行
unspecified(),而在AppFuse里的每個(gè)Action都會重寫這個(gè)方法。

private String getActionMethodWithMapping(HttpServletRequest request, ActionMapping mapping)
{
return getActionMethod(request, mapping.getParameter());
}


/** *//**
* Gets the method name based on the prepender passed to it.
*/

protected String getActionMethod(HttpServletRequest request, String prepend)
{
String name = null;
// for backwards compatibility, try with no prepend first
name = request.getParameter(prepend);

if (name != null)
{
// trim any whitespace around - this might happen on buttons
name = name.trim();
// lowercase first letter
return name.replace(name.charAt(0), Character.toLowerCase(name.charAt(0)));
}
Enumeration e = request.getParameterNames();


while (e.hasMoreElements())
{
String currentName = (String) e.nextElement();


if (currentName.startsWith(prepend + "."))
{

if (log.isDebugEnabled())
{
log.debug("calling method: " + currentName);
}

String[] parameterMethodNameAndArgs = StringUtils.split(currentName, ".");
name = parameterMethodNameAndArgs[1];
break;
}
}
return name;
}
綜上,在AppFuse里可以通過以下幾種方法來達(dá)到指定實(shí)現(xiàn)哪種操作:
1:在路徑里包含edit、add、delete字段
如
:http://loaclhost:8080/myapp/editPerson.html2:在request里包含method.methodName參數(shù)
如:
<html:submit styleClass="button" property="method.save" onclick="bCancel=false">
<fmt:message key="button.save"/>
</html:submit>
3:執(zhí)行cancel操作
<html:cancel styleClass="button" onclick="bCancel=true">
<fmt:message key="button.cancel"/>
</html:cancel>
4:都不執(zhí)行以上操作時(shí),執(zhí)行unspecified()方法
public ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return search(mapping, form, request, response);
}
posted on 2007-06-20 16:39
扭曲的鉛筆 閱讀(647)
評論(0) 編輯 收藏 所屬分類:
AppFuse