First, I really appreciate the quick response!
I'm a little confused on the suggestion, since save() is called automatically
since it uses a dispatch action. The struts-config.xml has a mapping for
event_save=save, so if it's in save(), I wouldn't really need to test a form
field called Action, since the dispatch occurred, right? I also don't have a
save forward, just a success.
The full save method is:
public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception,
ServletException
{
ActionForward fwd = mapping.getInputForward(); // default to input
if (isFormValid(mapping, form, request)) {
if ( mapping instanceof CRUDActionMapping ) {
log.info("process CRUDActionMapping");
CRUDActionMapping myMapping = (CRUDActionMapping) mapping;
if ( myMapping.getFormType().equalsIgnoreCase("SINGLERECORD")) {
saveFormData(mapping, form, request);
}
else if
(myMapping.getFormType().equalsIgnoreCase("MULTIRECORD")) {
saveMultiRecordForm ( mapping,
form,
request );
} // dispatch based on form type
} // handle CRUDActionMapping type
else {
// original call to save which you override
saveFormData(mapping, form, request);
}
fwd = mapping.findForward("success");
}
return (fwd);
} //save
The supporting methods:
private boolean isFormValid(ActionMapping mapping, ActionForm form,
HttpServletRequest request) {
ActionMessages errors = form.validate(mapping, request);
boolean formIsInvalid = (errors != null && !errors.isEmpty());
if (formIsInvalid) {
saveErrors(request, errors);
}
return (!formIsInvalid);
} // isFormValid
Since I've only written something for multi-record, the default saveFormData
must be over-ridden if the actionMapping is not of type CRUDActionMapping, or
it's not defined as MULTIRECORD. That is, this method is empty in the base
class:
public void saveFormData(ActionMapping mapping,
ActionForm form,
HttpServletRequest request)
throws Exception {
// override in another class.
} // saveFormData
If there's more code that's needed, let me know.
The code has the hierarchy:
Action
BaseEventActionDispatcher (base class for app to setup dispatch)
BaseEventActionDispatcherCbp (the class I refer to above where I'm
writing default multi-record handling/processing)
CbdeqrEngCodeHoursAction (the action for a page)
The only codein BaseEventActionDispatcher is:
public class BaseEventActionDispatcher extends Action {
/**
* Instantiate event dispatcher
*/
protected ActionDispatcher dispatcher =
new EventActionDispatcher(this);
/**
* Use event dispatcher to call an appropriate event handler.
* By using a dispatcher an action class does not need
* to extend DispatchAction.
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return dispatcher.execute(mapping, form, request, response);
}
public ActionForward cancelled(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
IOException,
ServletException,
SQLException {
// find global forward if action cancelled
return mapping.findForward( "cancelpage");
}// cancelled
-----Original Message-----
From: Martin Gainty [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 11, 2006 3:44 PM
To: Struts Users Mailing List
Subject: Re: Code reduction for Action classes
1)rewrite your save() logic to invoke form.getAction() and test on the returned
parameter
if(form.getAction() == "save")
{
//do all your save logic here
return (mapping.findForward("save"));
}
//the last thing you need to do in your save method is to return the mapping
for success as in
return (mapping.findForward("success"));
I cant speculate on any of the other classes as you havent displayed the code
for those.
M-
This e-mail communication and any attachments may contain confidential and
privileged information for the use of the
designated recipients named above. If you are not the intended recipient, you
are hereby notified that you have received
this communication in error and that any review, disclosure, dissemination,
distribution or copying of it or its
contents