You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sid Stuart <si...@aynot.com> on 2002/07/24 22:41:21 UTC
Re: Populating a Form with values
Hi all,
I also initially wrote two action classes for each JSP page, one to
intialize it and the other to process the form. This got a bit messy, so
I wrote template that looks at the HTTP method and calls an
initialization method if it is a "GET" or calls a form processing method
if it is "POST". A class extending the template can be used both to
initialize the form needed by a JSP page as well as process the form. An
excerpt of the code is below.
Sid
public abstract class ActionTemplate extends Action {
private final String GET = "GET";
private final String POST = "POST";
//-----------------------------------------------------------------
Public Methods
/**
* Override the Action method's perform method. This looks at the
HTTP method used to initiate the
* transaction and calls one of two abstract methods, depending on
whether a get or post
* was sent.
*
* @return An ActionForward object.
*/
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
log.info ("Entering perform:");
// The result is returned at the end.
ActionForward result = null;
// Get the session
final HttpSession session = request.getSession ();
// At this point, the perform method calls one of two abstract
methods.
// One does initialization and displays the JSP page. The other
processes
// a form submitted by the JSP page. Which method to call is
decided by whether the
// action was initiated by a get or a post.
//
final String method = request.getMethod ();
// Initialize a JSP page.
if (method.compareToIgnoreCase(GET) == 0 ) {
result = initializeJSP (mapping, form, request, response,
session);
}
// Process a form.
else if (method.compareToIgnoreCase(POST) == 0) {
result = processForm (mapping, form, request, response,
session);
}
else {
log.error("Unknown method received by Action: " + method);
}
log.info ("Leaving perform");
return result;
}
//-------------------------------------------------------------------
Abstract Methods
/**
* The initializeJSP method is called before the JSP page is
displayed. It should
* initialize any objects needed by the page.
* @return An ActionForward object
*/
protected abstract ActionForward initializeJSP (ActionMapping
mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
HttpSession session);
/**
* The processForm method is called when the form is submitted from
the JSP page. It should contain
* the code needed to process the form.
* @return An ActionForward object
*/
protected abstract ActionForward processForm (ActionMapping
mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
HttpSession session);
}
Keith Bacon wrote:
>Most people say best practice is to always start your jsp from the struts action. (always use the
>/myAction.do not /myAction.jsp).
>
>I think you need to do a lot of studying & messing around to get the hang of struts, it's not
>totally simple - but web apps never were.
>Try http://husted.com/struts/resources.htm
>
>Keith.
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>