Struts2 + Spring/Hibernate

View: New views
4 Messages — Rating Filter:   Alert me  

Struts2 + Spring/Hibernate

by CRANFORD, CHRIS :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I am in the process of implementing Struts2 along with integrated
support with Spring and Hibernate.  While I have found various examples
on the web, I tend to find they vary.  My application will primarily
focus about 75% of the time on data queries and displaying this data to
end users while a smaller 25% will actually support a full CRUD based
system for certain data records.

So far the examples I have seen have focused on implementing a class
structure similar to the following:

  com.company.app.hibernate.dao.PersonDAO.java
  com.company.app.hibernate.dao.GenericDAO.java
  com.company.app.hibernate.model.Person.java
  com.company.app.hibernate.service.PersonService.java
  com.company.app.hibernate.service.GenericService.java
  com.company.app.struts2.actions.PersonAction.java

The GenericDAO class is a template-like class that holds a reference to
the EntityManager along with methods for saving, deleting, and
retreiving objects persisted inside the EntityManager.  The PersonDAO
object extends GenericDAO and provides an additional list method shown
below:

public class PersonDAO extends GenericDAO<Person,Integer> {
  public List<Person> list(int page, int size) {
    Query query = this.em.createQuery("from Person order by lastName,
firstName");
    query.setFirstResult((page-1) * size);
    query.setMaxResults(size);
    return query.getResultList();
  }
}

The Person class itself is annotated as an @Entity object with a unique
property that is marked as the entity's unique ID and all the properties
of the Person table along with get/set methods for each property.

GenericService is another template-like interface class that defines
create/delete/update/getById/list methods.  Then PersonService
implements this GenericService interface with calls to the PersonDAO
object for each of these methods.

And lastly PersonAction extends ActionSupport and implements
StrutsStatics where it gets constructed with the GenericService<Person>
class.

Inside my web\WEB-INF\myAppContext.xml file I have:

  <!-- daos -->
  <bean id="personDao" class="com.company.app.hibernate.dao.PersonDAO"/>
 
  <!-- services -->
  <bean id="personService"
class="com.company.app.hibernate.service.PersonService">
    <property name="dao" ref="personDao"/>
  </bean>

  <!-- actions -->
  <bean id="personAction" scope="prototype"
class="com.company.app.struts2.actions.PersonAction">
    <constructor-arg ref="personService"/>
  </bean>

Is there anything else I should include in myAppContext.xml?
Any special inclusions or statements I need in my
applicationContext.xml?

Per one example I saw, struts.xml should be as follows:

  <package name="persons" namespace="/persons" extends="struts-default">
    <action name="list" class="personAction" method="list">
      <result name="success">/WEB-INF/pages/persons/list.jsp</result>
    </action>
  </package>

Thus far this example seemed easy to understand, particularly because
we're only dealing with a single table.  Before I go into how to take
this example and build from it, do any of you have any input or
suggestions on the approach I am taking with objects?  Any lessons
learned?

Thanks
Chris


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@...
For additional commands, e-mail: user-help@...


Re: Struts2 + Spring/Hibernate

by wild_oscar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Seems fine with me.
CRANFORD, CHRIS wrote:
  <!-- actions -->
  <bean id="personAction" scope="prototype"
class="com.company.app.struts2.actions.PersonAction">
    <constructor-arg ref="personService"/>
  </bean>
I don't see any advantage on creating Actions with Spring. It works fine without it and it seems unnecessary configuration. Perhaps someone else can point out clear advantages of this.

CRANFORD, CHRIS wrote:
Per one example I saw, struts.xml should be as follows:

  <package name="persons" namespace="/persons" extends="struts-default">
    <action name="list" class="personAction" method="list">
      <result name="success">/WEB-INF/pages/persons/list.jsp</result>
    </action>
  </package>
I would suggest using wildcards to reduce the configuration of your actions, and also giving your actions a better name for when you have more than one domain class (otherwise you don't know if "list" is related to Person or to Address). For example:

  <package name="persons" namespace="/persons" extends="struts-default">
    <action name="*-*" class="{1}Action" method="{2}">
      <result name="success">/WEB-INF/pages/{1]/{2}.jsp</result>
    </action>
  </package>
This example would allow any action named Something-someaction to be mapped to  method someaction of class SomethingAction and have a result of pages/Something/someaction

Re: Struts2 + Spring/Hibernate

by jofi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I've just created similar skeleton couple of days ago.

To let Spring generate your Service and DAO beans, you have to specify also
the GenericService and GenericDAO in your applicationContext xml. You (can)
set them as abstract and you have to specify the inheritance relationship
between the PersonDAO and GenericDAO, the same for Service of course.

At least this worked for me...

Regarding your design... I thought that DAO design pattern is here to handle
access to data objects, i.e. CRUD. So I don't see the benefit of the Service
here? Can someone explain this? In my application I don't have any business
logic but CRUD. Is the Service layer needed here or can DAO act as the
Service as well?

And finally I've got one more question, which I believe is related to
this... One thing everyone suggest is to use wildcards in the Struts2
web.xml for CRUD operations. But this reference to many action classes with
the same functionality. My question is, if we remove the duplicity in
web.xml, can't we remove the duplicity in the Java code as well? Can't we
create generic CRUD action as well? (Similar to generic DAO or generic
Service).

Thanks,
Jozef

On Fri, Nov 13, 2009 at 12:47 AM, CRANFORD, CHRIS <Chris.Cranford@...
> wrote:

>
> I am in the process of implementing Struts2 along with integrated
> support with Spring and Hibernate.  While I have found various examples
> on the web, I tend to find they vary.  My application will primarily
> focus about 75% of the time on data queries and displaying this data to
> end users while a smaller 25% will actually support a full CRUD based
> system for certain data records.
>
> So far the examples I have seen have focused on implementing a class
> structure similar to the following:
>
>  com.company.app.hibernate.dao.PersonDAO.java
>  com.company.app.hibernate.dao.GenericDAO.java
>  com.company.app.hibernate.model.Person.java
>  com.company.app.hibernate.service.PersonService.java
>  com.company.app.hibernate.service.GenericService.java
>  com.company.app.struts2.actions.PersonAction.java
>
> The GenericDAO class is a template-like class that holds a reference to
> the EntityManager along with methods for saving, deleting, and
> retreiving objects persisted inside the EntityManager.  The PersonDAO
> object extends GenericDAO and provides an additional list method shown
> below:
>
> public class PersonDAO extends GenericDAO<Person,Integer> {
>  public List<Person> list(int page, int size) {
>    Query query = this.em.createQuery("from Person order by lastName,
> firstName");
>    query.setFirstResult((page-1) * size);
>    query.setMaxResults(size);
>    return query.getResultList();
>  }
> }
>
> The Person class itself is annotated as an @Entity object with a unique
> property that is marked as the entity's unique ID and all the properties
> of the Person table along with get/set methods for each property.
>
> GenericService is another template-like interface class that defines
> create/delete/update/getById/list methods.  Then PersonService
> implements this GenericService interface with calls to the PersonDAO
> object for each of these methods.
>
> And lastly PersonAction extends ActionSupport and implements
> StrutsStatics where it gets constructed with the GenericService<Person>
> class.
>
> Inside my web\WEB-INF\myAppContext.xml file I have:
>
>  <!-- daos -->
>  <bean id="personDao" class="com.company.app.hibernate.dao.PersonDAO"/>
>
>  <!-- services -->
>  <bean id="personService"
> class="com.company.app.hibernate.service.PersonService">
>    <property name="dao" ref="personDao"/>
>  </bean>
>
>  <!-- actions -->
>  <bean id="personAction" scope="prototype"
> class="com.company.app.struts2.actions.PersonAction">
>    <constructor-arg ref="personService"/>
>  </bean>
>
> Is there anything else I should include in myAppContext.xml?
> Any special inclusions or statements I need in my
> applicationContext.xml?
>
> Per one example I saw, struts.xml should be as follows:
>
>  <package name="persons" namespace="/persons" extends="struts-default">
>    <action name="list" class="personAction" method="list">
>      <result name="success">/WEB-INF/pages/persons/list.jsp</result>
>    </action>
>  </package>
>
> Thus far this example seemed easy to understand, particularly because
> we're only dealing with a single table.  Before I go into how to take
> this example and build from it, do any of you have any input or
> suggestions on the approach I am taking with objects?  Any lessons
> learned?
>
> Thanks
> Chris
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@...
> For additional commands, e-mail: user-help@...
>
>

RE: Struts2 + Spring/Hibernate

by CRANFORD, CHRIS :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Wild Oscar, thanks for your input.

First, I agree my design needs work with respect to naming conventions and thank you for the suggestions.  This was more of a very crude example of how I should be relating components in the Hibernate/Spring/Struts2 design pattern so I can grasp the concept.  In my Struts1.2 days, we didn't leverage spring nor hibernate and wrote our own DAO object framework.  While it worked nicely, Hibernate has far more benefits.

Now taking the example deeper, lets assume that Person is also related to two other tables in my database.  For example, one table that stores Payroll and another that stores MenuPermissions.  I would need to create two additional Entity objects, create their DAO and Service layers and then in all three entity objects, I would need to annotate the relationship amongst the 3 tables, correct?

When I finally get to the point where this framework will hit the road is when I will have 4 or 5 tables in a database, all with relevant information about a key record or set of records in the main table and I will need to join all these records together.  In the past we typically created a single record object for each query we had and while that worked nicely, there were lots of duplicity that I aim to avoid.

Is this the right path and expectation for hibernate/spring/struts when I have a join between multiple database tables?

Chris

-----Original Message-----
From: wild_oscar [mailto:miguel@...]
Sent: Fri 11/13/2009 3:42 AM
To: user@...
Subject: Re: Struts2 + Spring/Hibernate


Seems fine with me.

CRANFORD, CHRIS wrote:
>
>   <!-- actions -->
>   <bean id="personAction" scope="prototype"
> class="com.company.app.struts2.actions.PersonAction">
>     <constructor-arg ref="personService"/>
>   </bean>
>

I don't see any advantage on creating Actions with Spring. It works fine
without it and it seems unnecessary configuration. Perhaps someone else can
point out clear advantages of this.


CRANFORD, CHRIS wrote:
>
> Per one example I saw, struts.xml should be as follows:
>
>   <package name="persons" namespace="/persons" extends="struts-default">
>     <action name="list" class="personAction" method="list">
>       <result name="success">/WEB-INF/pages/persons/list.jsp</result>
>     </action>
>   </package>
>

I would suggest using wildcards to reduce the configuration of your actions,
and also giving your actions a better name for when you have more than one
domain class (otherwise you don't know if "list" is related to Person or to
Address). For example:

  <package name="persons" namespace="/persons" extends="struts-default">
    <action name="*-*" class="{1}Action" method="{2}">
      <result name="success">/WEB-INF/pages/{1]/{2}.jsp</result>
    </action>
  </package>
This example would allow any action named Something-someaction to be mapped
to  method someaction of class SomethingAction and have a result of
pages/Something/someaction
--
View this message in context: http://old.nabble.com/Struts2-%2B-Spring-Hibernate-tp26329368p26333817.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@...
For additional commands, e-mail: user-help@...





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@...
For additional commands, e-mail: user-help@...