struts2-s2hibernate

Struts 2 and Hibernate - What is the best practice?


I am using Struts 2 and Hibernate.

I know that URL params are readily available in Action class.

I also have a Model class which has getters and setters which is hooked to database table through Hibernate.

To link URL params to Model Class, I have to again use getters and setters in my Action class and create Model Object.

Is this the best way ? Basically I do not want to add getters and setters at 2 places.

Any good practice to implement this ?


Solution

  • Use ModelDriven.

    http://struts.apache.org/development/2.x/docs/model-driven.html

    That said, whether or not this is the best idea kind of depends; it basically means that unless you explicitly white- or black-list parameter values, your entire object is accessible. While this may be okay in your case, it's often not. I think a lot of people use a different object to expose values to the view and get them back from forms.

    Note that conversion between different objects with the same property names is pretty trivial via things like Apache BeanUtils and things like that.