Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to implement pojo to web form mapping in struts2

Ranch Hand
Posts: 42
Android Google Web Toolkit Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic