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.
Sounds like you want to look into the ModelDriven interface
Have your action implement ModelDriven this requires having a method
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: