I read the API for DispatchAction unspecified method, which says, "Method which is dispatched to when there is no value for specified request parameter included in the request. Subclasses of
DispatchAction
should override this method if they wish to provide default behavior different than throwing a ServletException."It doesn't quite do what I expect it to do.
java.lang.NoSuchMethodException
will be thrown, because getMethod() cannot find a method with the specified name. Isn't that what the unspecified() is for? Seems like it's not the case. If I try to access the subclass of DispatchAction directly, the unspecified() method will run, but not if I try to pass in an null or invalid value for the parameter. So how should I solve this?I overrided the dispatchMethod(), checked if getMethod() thows a NoSuchMethodException(), and set a default value after after catching the exception, like below.
[java]
protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp, String methodName) throws Exception {
// TODO Auto-generated method stub
String methodToCall=null;
try
{
if (methodName!=null)
{
log.info("methodToCall in TestDispatchAction is:"+methodName);
getMethod(methodName);
}
else
log.info("methodToCall in TestDispatchAction is null");
methodToCall=methodName;
}
catch (NoSuchMethodException exc){
log.info("NoSuchMethodException caught in TestDispatchAction");
methodToCall="defaultMethod";
}
return super.dispatchMethod(mapping, form, req, resp, methodToCall);
}
If you don't declare the parameter at all in the JSP, an empty string is passed in, not null. I wonder why.
Ah, thanks for the tip! Finally I get to know what I've been doing wrong!
ReplyDelete