Spring MVC Struts Mixing - java

This is really a general question.
I have an ecommerce webapp that I built solo about 4 years ago. At the time, I made the decision to use Struts as my mvc framework. As years passed working on other projects, I came to appreciate and feel much more comfortable with the feature set and flexibility offered by Spring MVC.
The service layer is solid, and is built using the spring framework.
I'd like to refactor my current web layer to now use Spring MVC. But as I have mentioned previously, I wrote the app solo and it's more of a side project. The point being that I do not have time, nor do I want to risk throwing away and completely reengineering the web layer from scratch.
So I'm asking the community on what their strategy would be for this refactoring effort?
Ideally, I would like to tackle pieces at a time, and ultimately mix the two technologies until eventually when I can completely turn off Struts.
All new functionality for my site would best be developed in Spring MVC.
The good thing is that my existing views, which is currently using apache tiles, would not have to change much, with the exception of removing struts tag libraries and replacing them with Spring MVC.
One requirement I'd like to keep is that the URL's should not change.
In other words, the ActionServlet and the DispatcherServlet would have to map to separate paths but somehow go to different implementations.
For example, how can I say that mysite.com/show-product maps to a Spring Dispatcher servlet, but mysite.com/show-category maps to a Struts action servlet.
Your thoughts are appreciated.

You can run them both in the same container with appropriate mapping.
I don't understand the question "should I convert action classes to spring controllers one at a time", how else would you do it?
Whether an action class should map to a method of a controller has more to do with your existing app's organization than anything else, or at least how you want the Spring app organized.

No, you don't need to rewrite your actions. See here:
http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/web-integration.html
Look for "18.4 Apache Struts 1.x and 2.x"

Related

Is it better to use spring mvc with hibernate?

I am going to develop small ERP System. The product has only one major requirement to support multiple databases.
I have planned to use
Front side: JSP+JSTL+JQuery [I have good command on that] + I have create my own custom component for re-useability and full control of my component.
Back-end: Using hibernate[ORM] framework [due to Cross database] and I have also good knowledge of it.
I am happy with above and feeling confident to build product, soon.
BUT
My friend made me unhappy ;)
My friend suggest me that you should use spring MVC with hibernate, because when your product become large,it create a problem in future due to many developers involvement.So, you should use framework , so that every developer follow the pattern and then your product remain stable.
But I am already follow hibernate pattern :)
I have no any knowledge of Spring MVC. When I search about spring MVC , I found it is a different thing than my requirement and also find Cross database context issue, if I use Spring MVC with hibernate.
Either should I used spring MVC or not. If yes, I have to learn then Spring MVC and
I do not want to waste my time, if spring is not suitable for me.
That's why I am consulting that forum , to go for right direction.
Actually, Spring MVC does not interfere with any of the components you have chosen already. It just introduces a way to structure your frontend - backend interface in a standard way (MVC pattern).
I actually run the very same setup and is very happy with it. (Jquery, JSP, Spring MVC, Hibernate).
Other than structure, you will get easy return of invested time in Spring MVC when you want to do Ajax heavy things (as I assume you want in an ERP app). It's trivial to build AJAX/JSON stuff with jQuery and SpringMVC.
Using Spring MVC will make your application structured. This will help in future maintenance.
Your code will look clean and you can separate your code into components and Spring will help you wire them together and you might decide to combine them in different ways, or wish to make it easy to swap out one component for another depending on different settings or environments. This would be great for multiple databases when used with hibernate.
Learning spring will not take much time if you have a good knowledge of Java EE.

Architectural Differences in Java MVC Web Frameworks

I'm trying to choose an AJAX-friendly Java framework for my first web application and am interested in first
understanding the architectural differences between the different flavors that are out there.
I like the concept of MVC frameworks, and so am primarily considering the following:
Any JSF variety (ICEFaces, RichFaces, PrimeFaces, etc.)
Spring Web Flow
ZK
Wicket
I've downloaded each of these projects and tried to follow their samples/tutorials, and there is
so much information to ingest I figured I'd take a breather and come here to cover some preliminaries
first.
I'm interested in how each of these frameworks implements the MVC pattern. Obviously, something rooted
in JSF (like ICEFaces) is going to have a different architecture than Spring. I'm sure that this is a
huge question, so I'm not looking for a full-blown tutorial on each of these frameworks; I'm just
curious as to what sort of artifacts (Java sources, XML config files, etc.) a developer has to write in
order to build a single AJAX-driven page using these. I'm interested in the differences to their approach,
nothing more.
For instance, I would imagine that each framework at some point uses a FrontController (or its likes) to
map HttpRequests to the right Controller implementation. That Controller (bean) would then need to do
some processing, possibly hit the database for some information (using ormapping and forming the Model), and
then construct a View/HttpResponse to send back to the client. This is an oversimplification I'm sure, but
there has to be an easy way to explain the high-level architecture for how each of these frameworks accomplishes
that.
Struts uses the ActionServlet (with Struts2 now its just Action) as the controller and model and jsp is the view.
For Spring MVC is achieved by DispatcherServlet which does the routing and Model is not bound to any framework related object you can use any.
JSF - UI jsp or jsf itself, Model - ManagedBean, Controller - FacesServlet.
I did some similar search for my own project a while ago, have a look at the links below:
Comparison based on multiple parameters : http://static.raibledesigns.com/repository/presentations/ComparingJavaWebFrameworks.pdf
Difference between JSF and Struts
http://struts.apache.org/2.0.14/docs/what-are-the-fundamental-differences-between-struts-and-jsf.html
Somewhat related post
https://stackoverflow.com/questions/7633583/which-mvc-is-better-spring-or-struts
Spring and JSF
http://blog.springsource.org/2007/04/21/what-spring-web-flow-offers-jsf-developers/
Spring MVC : http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html
Best Fit For JSF Component Library: Primefaces based on my own experience
From IBM Clearing the FUD : http://www.ibm.com/developerworks/library/j-jsf1/
Hope this gives you some insight.
Have a look at Matt Raible's talk on Comparing JVM Web Frameworks here. You can also consider looking at Spring MVC and 'Tapestry`.
Also, this link gives you a matrix on capabilities of various java web frameworks.
You should also check out the Play framework. I have used it a little and really like it.
It is very easy to get started with minimal configuration (reminds me of Rails).
http://www.playframework.org/

What web frameworks can be used successfully on Google app engine? What about Struts/Spring?

I'm interested in porting an existing application which was written using Spring/Struts2/Hibernate to GAE. I didn't read many successful stories about that so first of all I'm wondering if it is wise to use any of them there. I want to get rid of hibernate and to use the google data store instead since it's obvious hibernate is not a good choice on GAE. So my main questions refers to Struts2(webworks)/Spring. My app uses simple JSP pages, no other template mechanism.
My question refers to the following aspects:
Currently each page has struts actions associated. Each request is taken by struts dispatcher an then it is dispatched to specific action class after which it invokes a specific jsp, according to the config file.
Everything is instantiated by Spring(actions, factories, hibernate layers,...)
The security is managed through a Spring Security(former acegi).
My first impression is that I should get rid of Spring because GAE should manage all the objects in order to work in a distributed environment. I have a few antagonistic thoughts regarding Struts. However, if I get rid of them ,I'm not sure if I should do it and in case I will, what to choose instead. Is GAE good enough to provide all the things I needed? I assume instantiation should be handled by GAE to have a truly sclalable solution. What about session management, security & user privileges? GAE provides good mechanism to manage them behind the scene or I should manually handle them like in a classic jsp solution? Will I end in having a huge web.xml?
Are there any frameworks that fits well in GAE and which can replace Spring/Struts? I would be interested to find out from someone who experienced those problems in GAE.
Here is a list of frameworks which are known to work or to have issues:
http://code.google.com/p/googleappengine/wiki/WillItPlayInJava
In general Hibernate will not work, but you could potentially use Java Persistence instead. Struts should be OK, but Spring Security is not fully compatible while other Spring parts are fine.
As far as I know, Struts is generally a layer over standard Servlet API - so it should be running without many changes. Spring also is not a problem, in fact, on the web there are many blog entries about spring on GAE. Hibernate - yeah, better use JPA.
If you want to rewrite whole app, you can always go with GWT, even with Spring ( GWT/GAE Spring IoC powered )

why do we always put Hibernate, Spring and Strut in one app?

Definitely, I'm talking about working with MVC pattern.
Definitely, Hibernate make our life easier with Model layer.
But, Spring and Strut both work with Controller and View.
So, my question is: "I cant understand why other guy always put both Spring & Strut in one application while we need just one of them (Strut or Spring). If anyone understand that, please tell me, thanks!"
I'm just a kid in Java world, so any comments are appreciated.
But, Spring and Strut both work with Controller and View.
Spring is many things, as you can see in this diagram:
One of these things is the Spring MVC framework. I agree, it does not (usually) make sense to use Spring MVC together with Struts (although in large sites, different departments may have different requirements).
However, Spring is also many other things, most of all an IOC framework, and as such it makes perfect sense to integrate different model and view technologies.
Spring is both Spring - the IOC container and Spring MVC - the web action framework. Struts is only a web action framework. So if you prefer Struts over Spring MVC, but also want an IOC container, you will use Struts with Spring.
Additionally, Spring also provides declarative transaction management, a security framework, a set of JDBC helper classes, etc., that you might want to use in a Struts/Hibernate application.
I wouldn't say always. Personally, I have never put Spring and Struts together in the same application, and I am willing to bet that most Spring/Hibernate projects also do not also use Struts.
Spring isn't just MVC. It has much more integrations, such as database, security, DI etc. Usually you want to use one of that features if you use Spring (which doesn't also mean, that you have to use Spring MVC).
Lets say that Spring and Struts are both frameworks that do overlap in some aspects. Even if I think that, if you are utilizing spring to its full extent, there should be no need for struts at all. But people tend to stick with the stuff they are used to. As Struts has been around for quite some time there are a lot of applications based on this and a lot of people that have made a profession out of this and would never commit throwing that away. That's why I have seen quite a lot of these hybrid application around.
I think you misunderstand MVC Pattern in first place. Model is not about persistence, but about the business logic in first place. It usually involves some persistences and service classes. For this purpose, many people choose Hibernate for persistence and Spring IoC for dependency injection purpose.
For the View and Controller part of web application, a well known web mvc framework is Struts and Spring MVC. Spring itself is consists of many components, Spring IoC and Spring MVC is two of them. Spring MVC is an equivalent with Struts so you don't use them together. But it is ok to combine Struts and Spring IoC.
Struts - usually provides MVC framework (most of Production support & maintenance applications are already integrated with it).
Spring - to inject/ add new componenets without disturbing the existing java classes/ code.
IT mostly depends on your project requirement, in our project we have used JQuery there are lots of Struts tags are used at the UI Layer and that is the main reason we are using struts2 because struts2 is having very good integration with JQuery
Struts2-JQuery Tag Library is very useful hence we are using Struts2
+
Spring framework provides an easy way to manage the dependency. (because of its DI and IoC)
It can be easily integrated with struts 2 framework.
The ContextLoaderListener class is used to communicate spring application with struts 2.

How does spring framework assist in application development?

Am a bit confused here! how does spring framework assist in general development of an application? I use django framework and i can quickly explain to a layman how all parts fit together(Django, Python, templates, packages etc) to produce an excellent web application, but when i look at spring i get a bit lost! Am looking answers but not limited to the following;
Can someone please tell me how they have used spring to produce applications?
Can someone please point to me some real world applications done in spring ( iread somewhere linkedin.com is done with spring!
Can someone please tell me how this pieces come together ( Strut, javascript, glassfish/jboss, apache, etc and ofcourse spring) to produce an application?
How many separate pieces of software do you need to produce an application using spring?
How easy is it to produce application using spring framework?
I need the gory details :)
Gath
There are a number of Spring projects, but the initial Spring (POJO) project came about because of the perceived difficulties of working with J2EE. You'll find a number of projects on the SpringSource website that have grown out of this, but rather than being lumped into one framework they've taken a more modular approach. For all the products they produce see:
http://www.springsource.org/projects
Q. Can someone please tell me how they have used spring to produce applications?
Spring provides a number of features, but the most oft used one is that of dependency injection. This allows you to wire together components (e.g. Javabeans) by declaring the relationships in XML/Annotations. The Spring container then reads this information and constructs the bean hierarchy at runtime. A standard way to describe the beans in XML is the Application Context.
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html
Q. Can someone please point to me some real world applications done in spring ( iread somewhere linkedin.com is done with spring!
There are lots of applications built using Spring. I'm not sure of big commercial projects, but I expect there to be many.
Q. Can someone please tell me how this pieces come together ( Strut, javascript, glassfish/jboss, apache, etc and of course spring) to produce an application?
Spring is normally integrated with other frameworks, there are various hooks into these frameworks and you need to look at each one separately in order to understand what they are all about. Struts and Spring framework integration can be found here:
http://www.ibm.com/developerworks/web/library/j-sr2.html
With Glasshfish/JBoss it's more about how you configure your application in relation to Spring rather than the application server. See this:
http://static.springframework.org/spring/docs/2.5.x/reference/web-integration.html
Q. How many separate pieces of software do you need to produce an application using spring?
e.g. A web application would consist of Spring MVC + Spring Backend. A desktop application - Java Swing + Spring backend. In terms of the Spring framework itself (configured with XML) it would involve:
Create your standard JavaBean classes (for services/DTO's/DAO's)
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
Declaring beans within your application context file
Integrate Spring with your web application/application via web.xml etc...
These are taken from the Spring docs btw... see:
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html
Q. How easy is it to produce application using spring framework?
Very easy, but again dependent on what you're building. If you're just using the Spring POJO framework to build a service and integration tier then it's fairly simple. If you have to build a web application layer, then it's a little more complicated (not hugely at all) to understand the internals.
Hope that helps...
Can someone please tell me how they have used spring to produce applications?
Put the Spring JARs in your CLASSPATH, follow the Spring idiom (e.g., interfaces to delineate layers), and use it to glue your code together.
Can someone please point to me some real world applications done in spring ( iread somewhere linkedin.com is done with spring!
Here's one. Running in production now for three years and counting.
Can someone please tell me how this pieces come together ( Strut, javascript, glassfish/jboss, apache, etc and ofcourse spring) to produce an application?
Struts is one choice for web tier; JavaScript is something you can use to make your client dynamic; Glassfish/JBOSS/WebLogic/WebSphere/Tomcat/Spring DM are all app server choices for deploying your Spring app; Apache is an HTTP web server; Spring and your code go on the app server.
How many separate pieces of software do you need to produce an application using spring?
You need an app server and probably a database, a browser, your code and Spring.
How easy is it to produce application using spring framework?
How good a programmer are you? Depends on your knowledge.
Spring certainly made my life easier once I understood it.
Spring is a pretty large framework, it's going to be hard for anyone to summarize everything here. I think the biggest plus for using Spring is its dependency-injection support. It can be used in any type of application, and provides a ton of framework features and utilities. If you are really interested, I'd recommend starting with the docs on springsource.org:
http://www.springsource.org/documentation
There are a lot of tutorials out on the web too. To fully appreciate it, you should try writing a few example apps to get the feel for it, and see what's available.
Just to add another dimension - if you are interested in building java web apps, and have no constraints (like legacy code to integrate with/update), then take a look at grails. its the best-est web framework to write web apps in. Of course, it uses spring internally too, but its fairly abstracted away from you. In fact, i'd liken it to django.
You can start with this http://static.springframework.org/docs/Spring-MVC-step-by-step/. This will provide the basic framework for you and then spring is huge so depending upon what you need you can explore spring and use it.
This has worked for me.
If you're talking about Struts and JavaScript at the same time, it means that you don't know what you're talking about, for Web Development in general.
What is what?
- You have frameworks in Java such as Spring, Struts, etc. which help you build Java based Web applications. Those Java Frameworks integrate generally Web with ORM tools, and other technologies.
- You have Servlet Containers such as Tomcat, Jetty, etc on which you can run classic Java Web Applications. J2EE containers such as JBoss, Glassfish embed a servlet Container such as Tomcat and provide a J2EE container and sometimes other tools, to deploy Java Enterprise applications.
Choosing a Framework
Choosing a Framework depends on how you feel about it and how it fits your needs. For example why are you using Django instead of TurboGears? Some people will tell you that Django sucks while TurboGears rocks, etc.
Whatever you do it's your call, read at the documentation and pick one application server and one or many frameworks to build your application.

Categories