File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes how to implement pojo to web form mapping in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to implement pojo to web form mapping in struts2" Watch "how to implement pojo to web form mapping in struts2" New topic

how to implement pojo to web form mapping in struts2

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Hi all,

I've been looking for a decent way to map a pojo to a web form, and in the struts2 release info you can read "POJO forms - No more ActionForms! Use any JavaBean to capture form input or put properties directly on an Action class. Use both binary and String properties!". Does anybody know of an online example concerning this? It would be nice to automatically populate my pojos directly with web input somehow.

Thanks a whole lot!
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Sounds like you want to look into the ModelDriven interface

Have your action implement ModelDriven this requires having a method

T getModel()

where T is the domain model you want populated. Now when your request comes in, it will call the getters and setters on the return from getModel instead of directly on the action. (any property not found on the model will fall back to trying to find that property on the action.)

Often you'll also want to pair this with the Preparable interface. Under the standard/default interceptor stack, one of the key section does "params; prepare; params". The first params call will throw all the parameters onto the action (that have properties there). Prepare can then be used to initialize either a new model object, or grab one from the DB based on the set parameters on hte action, the following params interceptor will set the parameters onto the model action. This normally reduces the execute method for Create/update type actions to a simple call to save the object with no other object retrieveal/setters needing to be used.

I'm not seeing any good/complete examples on-line right now. But both of the released Struts 2 books ( "Practical Apache Struts2 Web 2.0 Projects" and "A Tutorial: Struts 2 Design and Programming") have good examples of this.
subject: how to implement pojo to web form mapping in struts2
It's not a secret anymore!