On Mon, 10 Feb 2003, Jason Vinson wrote:
> Date: Mon, 10 Feb 2003 08:23:10 -0500 (EST) > From: Jason Vinson <[EMAIL PROTECTED]> > Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]> > To: Struts Users Mailing List <[EMAIL PROTECTED]>, > Taylor Cowan <[EMAIL PROTECTED]> > Subject: data acquisition and control > > Hi folks, > > I am having a few issues this morning determining the proper way to > control the data of my current web application. From what I have read, > I see that the proper place to acquire data from the system is in the > Action class. This data should be put on the request (correct?) and > retrieved by the proper ActionForm to populate the jsp. Maybe monday > morning has my brain working slow, but I am a little mixed up on a few > points here. > > 1. Pulling the data from the system in the Action Class actually > requires you to gather data in the Action Class of the page prior to the > one you want to render the information in. Doesn't this defy the > separation of logic and business in the MVC design paradigm? That's what the form bean does for you -- it combines all the "interesting" stuff from the form that was just submitted into one bean, and supports validating it (via the validate() method), *before* your Action is called. > > 2. To be completely honest, I am not seeing a convenient way to gather > the data off the request in the ActionForm class of my page. I feel I > may be completely overlooking the obvious, but I know I need to grab the > request in the ActionForm, and I can't see a convenient way to do that > by studying the javadocs. > The typical pattern for using the UI part of Struts is to put all the input fields into a form bean, so that is what you would typically interact with in your Action (rather than the underlying request object directly), although you can still get to that if you need it. Think of an Action as an adapter between the Servlet API tier (which thinks of things as request parameters and cookies) and the business logic tier (which should be designed to manipulate JavaBean-like objects that are not tied to the web tier, for reusability). A common requirement, then, would be to want to copy the contents of a form bean (after validation) into a JavaBean with native data types. This can be accomplished fairly simply (requires the recent versions of commons-beanutils included in Struts 1.1b3 or later). Assume you have a form bean with input fields for a name and a number. Following the recommended design pattern, you used a String for both fields in the form bean: public class MyActionForm extends ActionForm { public String getName(); public void setName(String name); public String getNumber(); public void setNumber(String number); } Now, your business logic includes a method that accepts a value object with name and number properties, but it wants an integer: public class MyValueObject { public String getName(); public void seName(String name); public int getNumber(); public void setNumber(int number); } To do the conversion, BeanUtils includes a nice simple method for you: MyValueObject myVO = new MyValueObject(); BeanUtils.copyProperties(myVO, form); This is extendable to any number of properties, as long as they have the same name -- see the commons-beanutils APIs for more info. > can anyone clear this up for me?? > Jason Craig --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]