This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes Benefits of Using Spring and Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Benefits of Using Spring and Struts" Watch "Benefits of Using Spring and Struts" New topic
Author

Benefits of Using Spring and Struts

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi.
I have a question about Using Spring and Struts.
The benefit of integerating Spring with Struts -as I understood it- is, if my Actions use Hibernate (for example),
I can inject Spring Hibernate templates into my actions.
And my Actions will have accessor methods to set my Hibernate Dao.
Right ?
(Well, the same question for using Spring and Tapestry).
Thanks folks.
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by John Todd:
Hi.
I have a question about Using Spring and Struts.
The benefit of integerating Spring with Struts -as I understood it- is, if my Actions use Hibernate (for example),
I can inject Spring Hibernate templates into my actions.
And my Actions will have accessor methods to set my Hibernate Dao.
Right ?
(Well, the same question for using Spring and Tapestry).
Thanks folks.


No, that's exactly what you should not do. Your Actions should not be the service layer, because then it's impossible to use them without Struts.

The proper idiom is to have separate service and persistence layers, without any thought to the web or UI layers. The service will use the DAOs and model objects to accomplish tasks. You can then use them with Struts or Tapestry or Spring MVC or anything else you choose for the front end. You can also expose the services as Web services.

Just don't put that stuff in the Actions. That's a dead end.


%
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Do you know any online examples of using Spring and Struts ?
Thanks.
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by John Todd:
Do you know any online examples of using Spring and Struts ?
Thanks.


Google can find plenty. The Spring docs have examples, too:

http://www.devx.com/Java/Article/29208
http://www.springframework.org/docs/reference/webintegration.html#struts
Sabarish Sasidharan
Ranch Hand

Joined: Aug 29, 2002
Posts: 73
The clearest benefit of integration IMHO is that you can get to springified beans easily from the Action layer.


Sab<br /> <br />Perfection does not come from belief or faith. Talk does not count for anything. Parrots can do that. Perfection comes through selfless work.<br />Swami Vivekananda
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
I'm curious about Spring-tapestry integration. In struts its probably not a big deal to pull action classes out of a spring context by overriding the method that creates the action classes.
How does it work in tapestry. Are tapestry pages configured within Spring?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Karthik Guru:
I'm curious about Spring-tapestry integration. In struts its probably not a big deal to pull action classes out of a spring context by overriding the method that creates the action classes.
How does it work in tapestry. Are tapestry pages configured within Spring?


No. I'd go into more detail but you've somewhat highjacked this thread. If you want to know more about Spring-Tapestry integration, start a new thread.


GenRocket - Experts at Building Test Data
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
Hello,

I am currently working on a project wherein i am integrating struts with Spring. Why people normally integrate spring with struts is because most of the web applications are designed and developed using Struts. But struts is basically a VIEW and Controller framework and doesnt offer too much on the JDBC part. Integrating spring with Struts offers you Spring JDBC which takes care of exception handling, avoiding the try, catch, finally blocks which make your jdbc code look very complex than it actually is. Also Spring provides you great classes which map a row of the DB with a user defined object and return a List to give just one example. So if you are looking for a framework which takes care of the MVC then you may look into the option of integrating Struts and Spring. Struts handles the V and C part and Spring takes care of the M
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Struts handles the V and C part and Spring takes care of the M


What about Spring MVC ? Nobody mentions it.
I don't see the need to use Struts.


[My Blog]
All roads lead to JavaRanch
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
Yes you are absolutely right. There is no need to use Struts at all. But the problem is that developers and architects over the world are comfortable using Struts and Struts as such has become a standard for developing web applications. But there are quite a few exciting features offered by Struts because of which people tend to use Struts. Its more a matter of being comfortable with using a framework.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Struts as such has become a standard for developing web applications.

I would not call Struts a standard I consider it an option, more than a standard, especially when using Spring.
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
Well, when i said Struts is a standard i meant that developers around the world are comfortable using Struts to develop web applications. Spring is new and will take time to catch up and be as popular as Struts.
Silvio Esser
Ranch Hand

Joined: Nov 05, 2005
Posts: 58
Originally posted by dnyan ginde:
Hello,
...
Integrating spring with Struts offers you Spring JDBC which takes care of exception handling, avoiding the try, catch, finally blocks which make your jdbc code look very complex than it actually is. ...M


I do not think a new framework should be introduced to get avoid try-catch-finally block. What is real substance of Spring JDBC?
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
I do not think a new framework should be introduced to get avoid try-catch-finally block.


Simply avoiding a try-catch-finally block is not a reason to consider using Spring JDBC. One valid reason to use Spring JDBC is to avoid the resource leakage that comes with incorrect coding of the try-catch-finally blocks and nested try-catch-finally blocks that are necessary to do it correctly or the inevitable cut-and-paste errors of even correct code. Spring uses the Template design pattern to control the workflow of using JDBC making sure to correctly clean up the resources used so you can concentrate onwriting code that actually does something of value in your app. Typically you just have to implement a call back method that does the important work. You also get to use Spring's exception hierarchy that takes care of decoding database specific error codes for you marshalling them into sensible RunTime exceptions that you can catch at the most apprpriate layer in your system. This helps if you need to change the database or your data access strategy even allowing you to mix strategies, i. e. JDBC and Hibernate if you like.

JDBC is just a tiny part of what Spring has to offer to simplify your app development in every layer of your app. In my experience I can't think of a single good reason to not use Spring for any non-trivial app.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
varun bihani
Ranch Hand

Joined: Aug 11, 2003
Posts: 96
Does that all mean, that we can avoid port number :8080 in the URLs while using struts ?

Or what is the other way to do that ?


<a href="http://www.galaxyweblinks.com" target="_blank" rel="nofollow">www.galaxyweblinks.com</a><br /><a href="http://www.freechatcode.com" target="_blank" rel="nofollow">www.freechatcode.com</a><br /><a href="http://www.onlinefamilytreemaker.com" target="_blank" rel="nofollow">www.onlinefamilytreemaker.com</a>
Jeevan Philip
Ranch Hand

Joined: Nov 17, 2006
Posts: 41
Originally posted by dnyan ginde:
Hello,

I am currently working on a project wherein i am integrating struts with Spring. Why people normally integrate spring with struts is because most of the web applications are designed and developed using Struts. But struts is basically a VIEW and Controller framework and doesnt offer too much on the JDBC part. Integrating spring with Struts offers you Spring JDBC which takes care of exception handling, avoiding the try, catch, finally blocks which make your jdbc code look very complex than it actually is. Also Spring provides you great classes which map a row of the DB with a user defined object and return a List to give just one example. So if you are looking for a framework which takes care of the MVC then you may look into the option of integrating Struts and Spring. Struts handles the V and C part and Spring takes care of the M


This is the most terrible explanation for using Spring with Struts. Why do you want to bring in JDBC/Data layer into the Presentation layer?

The benenfit for using Spring in Struts is to get Service classes/beans which will get injected by the framework. This provides a clean separation of presentation logic and business logic as Actions classes will no longer depend upon concrete Service classes.

Jeevan
Jeevan Philip
Ranch Hand

Joined: Nov 17, 2006
Posts: 41
Originally posted by varun bihani:
Does that all mean, that we can avoid port number :8080 in the URLs while using struts ?

Or what is the other way to do that ?


Not sure what you are talking about. Port 8080 is typically used by tomcat servers.

Struts, like any other generic MVC frameworks, uses urls as the resource location mechanism, unlike newer component based presentation frameworks like JSF. Spring does not save you from this.
[ November 23, 2006: Message edited by: Jeevan Philip ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Benefits of Using Spring and Struts