Spring MVC vs JSF [duplicate] - java

This question already has an answer here:
Difference between Request MVC and Component MVC [closed]
(1 answer)
Closed 6 years ago.
I haved used Struts framework in all my past applications and for the new application,my client has requested to use either Spring MVC or JSF? I am not familiar with these two frameworks but our timelines are strict. So, I am not sure which framework I will choose to build the application.
Can anyone please suggest me which framework will be easy to learn in quick time?
Thanks

Of course, it's going to be different for everyone, but I'd suggest Spring MVC, as it's a request-based framework like Struts. Of course, you'll want to learn about core Spring stuff like Inversion of Control / Dependency Injection (but I'd consider that a plus...) and whatever you're going to use for database access (just JDBC? Hibernate? iBatis? etc.).
JSF is component-based, which is a bit different paradigm from request-based frameworks. If you do plan to go the JSF route, I'd suggest looking at Seam from JBoss. It's more of a front-to-back framework that uses JSF as the web/presentation end and EJB as the backend. And pretty much all the people who've used it claim it makes JSF and EJB more usable than they are by themselves.
Good luck on whichever technology you choose, though. (Sounds like you'll need it - strict timelines and a client that's prescribing web frameworks?)

I'd suggest SpringMVC, because of the timeframe:
you need something with less steep learning curve. SpringMVC is more like Struts than JSF
in order to use the power of JSF you need to get familiar with many "tricks", while SpringMVC is more or less straightforward

I'd suggest JSF + Primefaces component library. I am using this combination to build most of our projects. As I remember, I spent one week to learn the technology and finished my first project in one month. The development time at least 30% faster than Struts.
SpringMVC is not a bad technology and it's quite popular.
Really depends on which one your like the most.

JSF is just the view layer of the MVC and wil need to be used with other technologies like Spring/Hibernate or EJB for a full MVC.
I have been using the Spring MVC for about 1 months now, whilst it's probably not the latyest version of SpringMVC I've found it a little annoying that we have so much XML to deal with. All the managed beans and DAO has XML config to it. Also everything seems to have to go thorugh a method called onSubmit().
JSF with something like EJB is far simplier in my opinion... Everything can be done using Annotations so simply use #ManagedBean=theBean in your backing bean and in your JSF put {thebean.param} and you have access to the backing bean's data. Also you can use the Session beans of your EJB as the backing beans for JSF then have direct acces to the DAO (Model layer) Entity bean. Again simply by using the #Entity annotation and the EntityManager class
Spring MVC is a web framework inside the Spring framework. It does provide features as those in JSF 2.0:
ajax-support
validation
dependency-injection etc
Yet, you can use Spring (not Spring MVC) together with JSF 2.0, with spring providing the dependency-injection, aop, transaction management mechanisms, and JSF providing the web layer.

Of course, you'll want to learn about core Spring stuff like Inversion of Control / Dependency Injection (but I'd consider that a plus...)
JSF is indeed based on IoC, and much simpler than the Spring learning curve.

Related

Struts + Spring + Hibernate integration

For example, we have the next bunch: Struts, Spring, Hibernate. Can you help me understand the role played by each of the elements?
I know that Hibernate is responsible for all on the database.
But, what role Struts and Spring in this case?
Thanks.
Struts is web application framework - used to give you the possibility to expose your application through web interface without dealing with low level classes. When using struts you should be careful as there often bad advices on using you application logic inside struts classes. Don't do this, struts is just web front-end.
Spring is general application framework that in first place helps you decouple classes from one another using dependency injection. But this is not the only reason for using spring, it provides tons of other features: http security, aspect oriented programming, out-of-the-box integrations and support of different frameworks. So this gives you a fast start on using different technologies.
Hibernate is object relational mapping. Using it you map your classes to database relations and avoid working with SQL requests.
My guess:
Struts is used for presentation purposes. Spring is used for AOP, and Depency Injection. Hibernate is obvious: persistence.
Spring:
Dependancy injection
Transaction Management.
Open Session in View Filters
Spring Security.
or in other words nice clue to between application layers.
Struts:
Model and view Design pattern.
Simplifies web flow.
Struts is for webapplication preparation. It has great form validation f/w, and tiles f/w to create webapplication in a great way.
Spring has 6 modules. It provides abstraction on the java-j2ee application. Spring MVC provides abstraction for struts.

How does Spring fit into my application architecture?

I'm currently rebuilding an existing PHP application using Java. I therefore have an existing frontend GUI and an existing database schema that I'm working with.
Here is the technology stack I'm working towards:
jQuery, client-side
Wicket, front-end
Spring, ???
Hibernate, ORM
MySQL, database
Before reading about Spring in both Wicket In Action and the Hibernate documentation, I envisioned wiring the two together through my own business logic. I have experience with JBoss Seam, but am told that Spring is hardly comparable (although the documentation suggests otherwise, IMO). Short of adding a book about Spring to my reading list (I haven't found an appropriate one with good reviews yet), I'm at a loss.
What benefit(s) will Spring provide in this technology stack?
Subjective & optional follow up question: what reference material (book, website, etc) will get me started with the portion of Spring 3 I may utilize?
First, you can make your web application without Spring. But Spring will greatly facilitate things. Spring framework is a lightweight, non-invasive. Spring is like a kind of conductor. Among other things Spring helps you in:
To keep your objects loosely coupled. This will make your application more flexible and open to future changes
Powerful support for transactions through the AOP (Aspect Oriented Programming).
Object-relational mapping (ORM) integration module. Spring doesn’t attempt to implement its own ORM solution, but does provide hooks into several popular ORM frameworks, including Hibernate, Java Persistence API, Java Data Objects, and iBATIS SQL Maps. Spring’s Transaction management supports each of these ORM frameworks as well as JDBC.
The Spring MVC framework. Even though Spring integrates with several popular MVC frameworks, it also comes with its own very capable MVC framework that promotes Spring’s loosely coupled techniques in the web layer of an application.
A good book about Spring: Pro Spring
Spring, as noted in this review is non-invasive. It just wires your application components. And provides useful classes that make using other frameworks easier (JMS, JPA, etc). Spring doesn't force you to use its classes or interfaces anywhere.
What it handles is the creation of your components (objects), so that you can refer to a class' dependencies, without instantiating them. I.e. you say what your class needs, not how it is obtaining it. This makes the application very flexible.
That's in short - for more, read the linked article. It's not about the latest version, but that doesn't matter.
In addition to dependency injection, Spring offers features like declarative transaction management, simple integration with ORM, aspect-oriented programming support and many other nice things.
For documentation see Spring reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html
#Dolph, in the simplest term, think of Spring as your application framework at the highest degree. This "framework" provides several "component buckets" where you can easily plug in different types of implementations. For example, for ORM, you may choose to use Hibernate over JPA or TopLink, for front end, you may choose Wicket over Struts or SpringMVC, and so forth.
The whole beauty of this (besides all the goodies stated in other posts) is it allows you easily swap out any of these implementations easily in the future. So, you can essentially rip out Hibernate one day and replace with TopLink, and it will never cause ripple effect to other components.
Another beauty of using Spring is your code becomes less clutter and has loose dependencies with other classes because you spend less time creating new objects yourself, Spring handles that for you. That said, you will quickly realize how easy for you to test your code because your API to be tested becomes very atomic. This is one primary reason why folks get discouraged in writing testcases, because they quickly realize that in order for them to test one API, they have to construct whole lot of things just to test that. Because of that, the whole process is brittle, imagine if you change that API, you need to reconstruct everything before testing it again.
Pro Spring book is good, mentioned by #JLBarros. I like Spring in Action very much. It is very very easy to read when I first got started with Spring. This is probably one reference book that I read from skin to skin.

Does Java Spring 3.0 MVC support annotation/attribute based client side validation like Asp.net MVC 2.0?

In Asp.Net MVC 2.0, at least in the beta, you could decoration your model classes with data annotation attributes and enable client side validation that leverages that criteria defined in your model data annotation attibutes. Is there anything similar for Java Spring MVC 3.0?
Is it possible to inject a component into the response pipeline that can inspect the model's annotated properties and render client side validation logic to complement the server side validation logic that is invoked prior to the controller handling the request?
Actually, there is something a little bit like it, based on the JSR-303 Bean Validation spec, which is now final and fully supported by Spring 3.0.
Recently, as part of Spring Webflow, Springsource has released Spring JS (javascript) which uses Dojo. You can use that for your client side validation. Check out this howto
Obviously, component-frameworks such as Wicket (like fraido mentioned) have better support for this kind of usecase. So if you have to implement a lot of similar usecases, that is probably a better fit for your project. However, if it is sporadic, Spring JS / Bean validation could be the way to go.
Spring MVC is a fairly low level framework. It doesn't extend to doing client side validation.
There are other Java Web frameworks that do this such as Tapestry, Wicket and JSF (IceFaces, etc). These are what I tend to call "component frameworks".
In Spring MVC 3.0 there's nothing like that at the moment. As cletus said frameworks like Tapestry (ex), Wicket (ex) and others have some sort of Client Validation that uses JavaScript to validate forms etc.
You can write your own validation Jstl taglib maybe with the help of a JS Library (jQuery, ...) and Plugins like : jQuery Validation Plugin
edit: I've just found this: Mediawidget. Maybe it's worth having a look.
It says: "Metawidget reads Bean Validation annotations and generates forms with
widgets that are pre-configured for minimum/maximum values, lengths etc."
Spring integration Link1, Link2
This project looks to be exactly what you're looking for: http://kenai.com/projects/jsr303js
2011-12-06: I'm just about to try it myself
2013-12-09: Update on this answer: I did try the library out, and it worked very well. It needed some tweaking (it's a prototype patch for Spring, not a finished plugin), but it was the best solution I could find at the time. Perhaps there is a new solution, or perhaps someone has finished off this prototype since I posted? If not, I can recommend this one.
Ok, this is a variation on what's already been said, but one of the big strengths of Spring-MVC is its ability to integrate with other libraries, like the different JSF variations that do give client side validation.

Can anyone recommend a Java web framework that is based on MVC and supports REST?

We are looking to host a Java Domain Model (written using DDD) inside a web application. Ideally I would like to support RESTful resources and requests, having a single site to support both users and a REST api.
Ideally the same url could be used for both end users and the RESTful API of a given feature (with HTTP content types / negotiation specifying the difference).
I have done similar apps in Ruby on Rails and Asp.mvc, but have no experience doing anything like this in Java. Anybody have an experience or advice on where to start ? (Googling suggests that Spring v3 might be an answer, anybody have any thoughts on Spring 3 ?)
Spring 3 is not quite ready yet, but the current milestone build (M3) is stable enough to use for real. We're using its REST support in a production application already. It's pretty goodm, and integrates very nicely with Spring MVC. It's not JAX-RS compliant, but I don't see that as a problem.
Restlets Framework http://www.restlet.org/
I've used this framework extensively, easy to use, really flexible and supports loads of features and more you'd expect from file upload to gzipping responses.
This module has Spring support too, really easy. For example:
web.xml
<servlet>
<servlet-name>webapi</servlet-name>
<servlet-class>
com.noelios.restlet.ext.spring.RestletFrameworkServlet
</servlet-class>
</servlet>
spring context
<bean id="root" class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/{variable}/your/restful/call/{variable2}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="createResource" bean="yourBean" />
</bean>
</entry>
</map>
</bean>
Framework also has a great documentation, first steps for newbies and a great WIKI.
This framework is very mature and actively improved, checkout the up and coming features.
It's also very easy to unit test your Restlet Resource endpoints using jmock.
Hope that helps.
For web services, Jersey is nice and easy. Spring 3 sounds like it will be good, but it's not out yet, and Jersey is full featured and supports SOAP and JSON out of the box. It's all annotation based aside from adding the servlet to your web.xml file which makes it probably easier to configure than even Spring plug-ins, but to avoid getting yelled at, I'll say maybe not.
For (MVC) web pages (user UI), I use Spring MVC or Struts.
Spring is great. I've used it for some projects and recently also together with Liferay portal server for developing a portlet.
Why is Spring good?
It is a non-invasive framework: This means your application code doesn't depend on the framework (uses the IoC - Inversion of Control - concept). Spring does just what a good framework should do: support the development and not create further dependencies.
Dependency Injection: Spring uses the dependency injection concept which is great for avoiding dependencies on your code. You will define the dependencies in a spring xml configuration file where you define your beans and connections/relations among beans. This greatly facilitates reuse, lowers strong coupling among your objects which leads to better maintainability of your code.
It's not just a web framework: Spring MVC provides a lot of different controllers which are suitable in different contexts. But it isn't just a web framework but it supports the development on all the different layers (presentation, service and data access layers). For instance on the data access layers it nicely integrates with ORM mappers like Hibernate and it uses Aspect-oriented approaches for providing transaction management.
Lower-coupling -> increases testability: By avoiding strong coupling, the testability of your code will be increased. You can nicely inject mock objects for testing the different layers.
All in all, I just had positive experiences, because Spring really promotes best practices.
I m a monorail MVC user ( castleproject.org), so i guess we come from a similar background.
A few months ago we started working on the java stack in a different project, particularly with Spring.
Feature wise it s got 90% of what I m used to in monorail, however It is much more flexible, the downside to that is that there is a lot of configuration to get used to.
Documentation is extensive sometimes too much so you dont know where to find something.
Hope it helps

How does a java web project architecture look like without EJB3?

A friend and I are building a fairly complex website based on java. (PHP would have been more obvious but we chose for java because the educational aspect of this project is important to us)
We have already decided to use JSF (with richfaces) for the front end and JPA for the backend and so far we have decided not to use EJB3 for the business layer.
The reason we've decided not to use EJB3 is because - and please correct me if I am wrong - if we use EJB3 we can only run it on a full blown java application server like jboss and if we don't use EJB3 we can still run it on a lightweight server like tomcat. We want to keep speed and cost of our future web server in mind.
So far I've worked on two Java EE projects and both used the full stack with
web
business logic
factories/persistence service
entities
with every layer a seperate module.
Now here is my question, if you dont use EJB3 in the business logic layer. What does the layer look like? Please tell what is common practice when developing java web projects without ejb3? Do you think business logic layer can be thrown out altogether and have business logic in the backing beans? If you keep the layer, do you have all business methods static? Or do you initialize each business class as needed in the backing beans in every session as needed?
EJB3 is split into two layers:
persistance (JPA, old Entity beans)
business logic (old Session beans)
You can have the same architecture, without using explicitely EJB3.
Persistance
JPA is very close to Hibernate. We use, and prefer it to JPA.
The annotations come from JPA, but there is no need for a container, it's not really EJB.
Business
Spring also is very close to the Business layer of EJB3. With many more capabilities...
Some even say they are better than EJB3 !! ;-) They say that EJB3 was created from these two solutions (but still have to reach at their level on many points!).
EJB enhanced a lot over the last 5 years. Especially with version 3 they became a lot more lightweight.
Java EE 6 has a web-profile that makes it possible to even run on a tomcat server a Java EE stack. http://www.oracle.com/technetwork/java/javaee/resources-jsp-139799.html
Further it is possible to install all the necessary components on tomcat:
http://tomee.apache.org/apache-tomee.html
I have an example project including EJBs on github that shows how to use them:
https://github.com/ManuelB/facebook-recommender-demo
Further I would recommend the screencasts of Adam Bien:
http://www.adam-bien.com/roller/abien/
If redeployment times are the reason why you aren't using EJB you should look into:
http://zeroturnaround.com/software/jrebel/
To still answer your question you should look into the following Java Web Frameworks as EJB alternatives:
Apache Wicket https://wicket.apache.org/
Apache Tapestry http://tapestry.apache.org/
The play framework http://www.playframework.com/
Spring http://projects.spring.io/spring-framework/
People that don't use EJB usually use Spring or another dependency injection framework. JSF allows you to create beans and wire them together, however it would be better to use this only for the UI and use Spring for your business layer. A lot of information is available for integrating JSF with Spring.

Categories

Resources