How to create reusable components in Spring MVC 3.0 - java

Ok I really don't know where to start. I have been trying to develop reusable component-like pages in Spring MVC. I ended up using tiles. I had the idea from "Spring in action" book on writing ComponentControllerSupport class. But the problem starts with tile2 and spring 3.0. All old features of tile1 is deprecated and tile2 has changed a lot(like "page" attribute changed into "template" and "controllerClass" changed into "preparer" etc). If anybody knows how to write that "preparer" class or other ways to approach such reusable components please help me.

Do you know Spring Roo ? It generates a skeleton of application using best practices that should be good starting point for further development. See http://www.springsource.org/roo/why

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/

Spring MVC Struts Mixing

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"

What are the prerequisite for Spring Roo

I have got my hands dirty in Spring Roo, I have managed to do the persistence set up for Oracle Database. Although I have gone through the Roo documentation
but it not enough for me to understand the working of this framework.
I'm not aware of Spring-MVC ,Spring-Web Flow and AspectJ rather I have knowledge of frameworks such Struts, Tapestry and GWT. Could this be a hindrances in learning Spring Roo.
Please advice as I'm running out of patience of hacking the generated code by Spring Roo. Is it good idea for me to jump in a Spring Roo project?
The Spring Roo is more than a code generator. If you make changes in your Entities, it will change the codes automatically. More like a stateful code generator. First you need to find out the commands available in Spring Roo to configure a project.
You can configure which database you want to use, which orm you want to use, whether you want to use unit-tests or which view technology you want to use. Give the commands and Roo takes care of it. You don't need to worry about which libraries to download, how to make pom file for Maven.
The configurations you got using Spring Roo is quite a standard one. I used it in the previous projects. The code generated is not optimal, for example , you can use generic daos instead of Roo generated DAO if you consider the generated codes are verbose.
Another example says, in Flex, you can use Roo to generate configurations for the communication between Flex client and J2EE server. For my case, I use parsley and observer patterns for flex client instead of the codes generated by Spring Roo. But by looking at the codes what the Roo-Flex team wrote taught me quite a lot of things too. The same things goes for GWT and Spring MVC.
So what am I saying is that as the programmers, we need not only to code we also need to read what the other wrote too . I strongly believe that it is a good way to learn and share best practices. You might get some boilerpate codes, but if you know how to apply design patterns or can enhance on them, those codes will become the gems.
Learning curve ? Not at all. The commands are as simple as DOS commands.
Spring ROO is just a command line interface for generating a lot of the boiler plate code you usually need to write to get things done in java projects.
If you want to add GWT into your project, just type "gwt setup" after you've created your models and you have full-on GWT support in your project.
The default view being generated in Roo uses Apache tiles, similar to Struts, Tapestry and JSF.
If you want to use web flow, just type in "web flow" and it'll be integrated for you, for struts support, you'll need a plugin, I've seen a couple of them floating around the web, haven't tested any of them; same goes for tapestry, you can either use a tapestry plugin or do the integration by hand.
AspectJ in the context of Roo generated scaffold is used to hide all the boilerplate code that makes code look like spaghetti, you can move code out of the aspectJ generated files into your own java files and modify them if you need to.
As for the hacking, it takes a lot of patience and time to fully understand where you should change stuff and where you shouldn't, all I can say is, happy hacking :-)
springroo is based on many technologies, but all what springroo does is to help you to generate scaffolding code using lots of addons you can install
at least it's my view of this framework after three days of learning it

Getting started with Spring 3 Web MVC - Setting everything up

I'm trying to get started with JAVA/Eclipse/Spring MVC but can't seem to find a "dummies" or "step-by-step" guide for setting everything up and creating the simplest proof-of-concept application.
I found http://static.springsource.org/docs/...-step-by-step/ but there seem to be differences between Spring 2.5 and Spring 3 so i'm looking for something for Spring 3.
Also, the tutorial for Spring 2.5 focuses on building the application usign Ant - This i will certainly be doing for releasing but when working within Eclipse i want to have set up the project so that i can use breakpoints/debugging and this is not covered at all.
I've downloaded Eclipse, Tomcat and the Spring 3 Framework but that's about it and within the downloaded Spring Framework documentation it states:
2.3 New getting started tutorial
There is now a new getting started tutorial
for developing a basic Spring 3.0 MVC
web application. This tutorial is a
separate document that can be found at
the Spring Documentation page
However, i can't seem to find such a tutorial on the linked page. My main problem at the moment, as seems to be the case for many people trying to get started with Spring, is that i can't seem to get everything set up.
I've got a copy of Expert Spring MVC and Web Flow and i've also found this tutorial but i don't know if the setups they describe are still relevant and whether they will allow debugging.
Can anyone please give some direction?
Thanks
Get the mvc-basic or the petcare sample apps from the Spring Samples SVN repository. These samples have been updated to Spring 3.0 and are directly importable & runnable with SpringSource Tool Suite.
I am also started to learn Spring 3.0 and i am not getting any help means any tutorials. but i find this site http://viralpatel.net/blogs/2010/06/spring-3-mvc-create-hello-world-application-spring-3-mvc.html and its very help full for start up development. but still i am finding another example so that i come to know more about it
if anybody knows some example in spring 3.0 then please provide me.
Thanks
Dhrumil Shah
These days, I think the easiest way to find out how to set up a spring application is by trying out Spring Roo. It may not be what you finally end up using when coding your project but it shows you exactly how to setup a Spring MVC web app.
I've also been looking all over for a full Spring MVC 3.0 tutorial with Eclipse that I could follow and haven't had too much luck in my research. The closest and most helpful example that I could find is here: http://krams915.blogspot.com/2011/01/spring-mvc-3-hibernate-annotations.html. This is also a maven project, but I was able to move some things around in my Dynamic Web Project within Eclipse and at least get the initial page to come up. Currently, I'm having issues with spring finding my controller class, but I think that's a part of changing the configuration files to meet my project needs. But try it and I hope it works for you!
I found these tutorials really helpful:
http://krams915.blogspot.ch/p/tutorials.html
I wanted to use REST and MongoDB and followed following tutorial with success:
http://krams915.blogspot.ch/2012/01/spring-mvc-31-implement-crud-with_4739.html
I hope it helps. I know how you feel :) Had the same issues. But it's worth sticking with Spring and learning it. It's a great framework once you know it.
Personally I made not so good experiences using Spring Roo. It can help you maybe a little bit for investigation how to setup a project but if you are new to Aspect Oriented Development you'll have a hard time figuring out what Spring Roo does. It can help you to setup a project really fast and easy but if you don't understand what's behind the facade it's hard working with it. This is not really for a beginner and no necessity for any project. But Aspect Oriented Development is worth wile learning and can be very useful to avoid boiler plate code.
Another route might be use appfuse, specifically using maven to generate the application template code from the appfuse archetype. E.g. this link.
There is a major difference in Spring 2.5 and Spring 3.x, Spring 3 or later introduces Annotations based bean wiring and dependency injection.
The nice step by step tutorial i found is http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial

Categories