Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Basic Struts Forms question

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just getting to grips with Struts, so this may be a *very* basic question, but here goes:

I have a Data Transfer Object (DTO) that I want to use to pass data to the web tier, and retrieve data from it. My question involves how I should use this with struts forms. The DTO is a complex object consisting of String fields and Collections. Something like this:

class someDTO {
private String name;
private Collection interests;
}

What approach do I take. Do I:

a) Create a struts form that holds the DTO as a member variable, looking like this:

public class MyForm extends ActionForm {
private SomeDTO someDTO;
}

This will be used to pass data to the web tier (fine, I have this working) and to pass data from the web tier back to the model. For this latter part my struts html tags access each property like this:

<html:text property="someDTO.name" />

I can't get this to work, and am now wondering if I am using the forms in the correct manner. Should I instead ...

b) Create a struts form that has values for each of the DTO fields as individual member variables, like this:

public class MyForm extends ActionForm {
private String name;
etc ...
}

The problem with this, of course, is how I reference Collections.

Basically, I'm a bit confused over the correct usage of forms in struts. And basic pointers would be much appreciated.
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Approach a) is perfectly valid and used often, atleast in my workplace. What exactly is the problem you are facing? A error message or stack trace would be useful.

Common errors would be:
- <html:text> is not used within <html:form>
- "someDTO" might be null in the form bean

Sheldon Fernandes
 
Tony Walters
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically the object the struts form needs to represent is complex, consisting of objects within objects. Something like this:

class MyDTO {
private SomeOtherObject theObject;
public void setTheObject(SomeOtherObject theObject) {...
public SomeOtherObject getTheObject() {...
}

class SomeOtherObject {
private String theString;
}


I already have a DTO to represent this object passed to the web tier from an EJB. I suppose my question is why can't I just use the struts form as a wrapper for my complex DTO, such that it has a single field, like:

class MyForm extends ActionForm {
private MyDTO theDTO;
public void setTheDTO(theDTO) {this.theDTO=theDTO;}
public MyDTO getTheDTO() {return theDTO;}
}

I guess I'm confused about how I would code this using struts html tags. Or even if this is an appropriate use of struts forms. Perhaps I should 'flatten' the object hierarchy for the form?
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many valid methods for doing what you are talking about. I personally prefer the second option. Here's why: The DTO is a representation coupled to your business model, whereas the ActionForm is coupled to your presentation. It may seem like a subtle difference, but in my mind at least, it's definitely there.

For example, let's say you have some kind of invoice object that has a collection of line items associated with it. These line items contain information about products, quantity, pricing, and other information relating to the order being invoiced. Now, let's say for the sake of argument that your customer has decided that the form is too busy if all line item data for each and every line item is always displayed. Your customer would like the ability to expand or collapse individual line item details in the display.

Each line item has a lot of information associated with it, so it doesn't make sense to transfer all this info to the presentation layer if it's not going to be displayed. You can see now that our business object and what we are presenting starts to diverge a bit. Whereas I may have an InvoiceDTO with a collection of LineItemDTO objects for the entire invoice, I may decide to have an InvoiceForm that containsa a collection of LineItemSummaries (containing only a small subset of LineItem info that I always want displayed), as well as a collection of LineItemForm objects (one for each LineItem that is actually being displayed). This greatly reduces the amount of data that needs to be transferred to my presentation layer, and is more in line with what my display actually consists of. In other words, if I'm not going to display it, it doesn't need to be part of the ActionForm.

The other thing I try to avoid is exposing my business object so that it may be directly modified by the user. This is a possibility when you use your business object as your ActionForm. Now whether or not this is a problem depends on whether or not the data is read-only, and also depends on your architecture and how you manage your DTO. As a general rule of thumb though, this is a best practice.

Now that I've explained my reasoning for my choice, let's talk implementation. I use Assembler objects to move validated data between my DTOs/business objects and ActionForms. Basically an Assembler just copies the fields from one object into another as necessary. You may have an assembleForm(Object obj) method in your assembler that copies the data from your business object to your ActionForm as necessary.

Now as far as handling the onscreen representation of these complex ActionForms, I suggest you read up on nested tags. Check out this page for more info on nested tags.

HTH
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U could use the BeanUtils.copyProperties method for copying the form-bean data to ur DTO and hen passing this DTO on to ur model.
 
Tony Walters
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestions, its helping clarify things a lot. I'm working through a couple of struts books but, like many books, they don't really give much by way of practical tips on using struts.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic