Java EE architecture and pattern [closed] - model-view-controller

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm developing a web application for a university project and it has been told to us to use Java EE and to provide documentation. I'm having trouble understanding how to fit the MVC pattern in the multi-tier architecture and how to describe them, since I've found articles on the internet saying different things.
From what I read (expecially about BluePrints at sun.java.com), I should use 4 tiers (client, web, business and data) and the business logic is divided between web and business tiers, isn't it? Then I can use the MVC pattern to organize these two layers:
Model = entity beans
View = JSPs
Control = servlets and session beans
Am I right?

There are a couple of approaches to MVC in Java EE.
The somewhat older approach (but depending on the context still valid) uses JSP for the view and Servlets for the controller. It's often debated what the model exactly is, but it's typically taken to be Services (represented by e.g. EJB session beans) that return domain entities (represented by e.g. JPA Entities).
In modern versions of Java EE, there's a default MVC framework called JSF. Following this framework, Facelets is used for the view and the controller is given (you don't need to implement it).
There's an in-between concept called the backing bean, which is often referred to as the model, but isn't a pure model itself. Instead, it delegates to the real model (e.g. EJB services). The backing bean can also take up some controller responsibilities (issuing a redirect, or putting a message in a kind of queue for the view to display).
Sometimes it's thought that creating a web and business tier is overkill, but this absolutely doesn't have to be the case. It's often just a matter of applying sound OO principles. The other extreme, e.g. stuffing everything on a JSP (html code, controller logic AND business code) is far, far worse.
See this example of how simple a 3 tier (3 layer actually) Java EE MVC application can be: Minimal 3-tier Java EE app, without any XML config
A related question is this one: What are the main advantages of MVC pattern over the old fashioned 3-layer pattern

If you use servlets, it's already Java EE.
And don't make unnecessariliy complex. If you only have a web user interface and no other user interface, dividing into both a web and business tier is over-kill.

Related

Spring MVC best practices

I am developing a Spring MVC web application and I keeps JPA entities in repository package, dao's and controller classes in separate packages with jsp's in a folder in WEB-INF.
My question is, in this app, what is the model, what is the controller and what is the view? Where should I keep the business logic (dao or controller)? What is the best practice in terms of clear separation of business logic?
For what you are describing, the layers are as follow :
view : JSP files
controller : the DispatcherServlet (front controller) and you controller classes
model : all what remains
For the latter part of your question, you are in fact asking for opinion based answers. But the following seems to be a common pattern :
If you put business logic in controller you get a Fat Stupid Ugly Controller (just google around). Even if Spring MVC gives you tools to test controllers, the common usage is to avoid it, because the controller is heavily dependent on framework, and it is best to make business logic as independent as possible of framework. One never knows, you may have to use a different framework later for any reason.
It is equally bad to put business logic in DAO for same reason : framework independance.
My advice is to add a service layer between controller and DAO, with a possibility of exception for a true CRUD application. It is a layer with less framework dependance ; and in fact the reason why it is generally not produced by framework : the framework cannot know your business logic. There's another reason for the service class, it is the layer where transaction demarcation should live : controllers hardly support JDK proxying (which is the default transaction demarcation in Spring framework), and it can be relevant to call more than one DAO method in one single transaction.
But I must admit that the above is my opinion and not the big truth (well not only mine, but definitely an opinon). You can certainly find use cases where business logic is so tiny that it does not require the overhead or a service layer : it is just an architectural choice, and provided you know why you make the choice any possibility can be good.

Can Spring Web Flow Work in Non Spring MVC application? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on evaluating Spring Web Flow for handling conversational flows in the web application that is currently used. Currently, the web application that we are using using is a custom company made MVC framework that was developed 10+ years back when there was no Spring MVC or Struts etc. There is a ControllerServlet in the application which is currently doing the work of managing the requests, similar to DispatcherServlet of Spring MVC.
Now, we want to take advantage of the Spring Web Flow features and renew the application and ease the developers life. I would like to know from all you people out here who had similar experiences in the past of migrating legacy MVC application to Spring Web Flow.
Please suggest the configurations and / or changes which I should be looking at to take this into right direction.
Some of the questions which come naturally to my mind are:
Configuration changes to web.xml? or adding a spring related xml file?
Any changes that we need to make to the JSP's? I am assuming Spring Web Flow will require me to make changes in the JSP's. What do you guys feel?
How could we get rid of the ControllerServlet? or better yet ask the ControllerServlet to refer to Spring Web-Flow for all the requests?
If Spring Web Flow is replacing the ControllerServlet, what happens to all the code that is written in it? what equivalenet of Spring Web Flow do we need to write?
Thanks,
Yogendra
Why replace, you can perfectly use Spring Web Flow (and/or Spring MVC for that matter) alongside your own MVC framework. You only have to be careful when you are crossing boundaries between the 2.
What we did in such a scenario was introduce a DispatcherServlet for only handling the Spring Web Flow request and everything else was handled as before. So you basically have 2 servlets instead of 1.
Another solution would/could be to replace your own servlet with a DispatcherServlet and let all requests for the old part of the application delegate to your own ControllerServlet, for this you could use the ServletWrappingController of Spring MVC.
Benefit of the last approach is that you could create a HandlerInterceptor which could move some stuff around from your own MVC framework to SWF and vice-versa.
In each and every car you would need to change the pages that use Spring Web Flow to use the Spring tag libraries so that form can be properly handled, for pages that aren't used/served by Spring Web Flow everything can remain the same.

Spring vs Java EE 7 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Now I am reading "Begining Java EE 7". And I was wondered that Java EE 7 is a good stack of technologies, it includes CDI, bean validation, JSF for web tier and other specifications.
So I got a question:
Why should I study Spring framework if Java EE 7 exists and covers all capabilities which Spring implements?
I will share little bit of what I know about using Spring. You are right by saying that Java EE 7 has all the technologies to help solve the problems.
Well Spring just enhances these capabilities and makes life more easier for a developer.
As an example when you use Spring MVC framework you can use Spring UI tags to create your JSP and those tags in turn can help you map the values directly to your controller. By controller I mean the Java class which is invoked when you do form submit. It also helps you to validate the form data.
This can be achieved using the Servlet technology also but Spring lets you focus on business logic and it takes care of these.
In my experience as a developer its good to know and understand Java EE 7 but frameworks like Spring utilize some of the best practices and patterns to make life easier for developers.
Would like to hear the opinions from others as well.
Hope this helps.
I think that one simply should know both of them. Spring has a huge community and is used widely, but Java EE is now going forward too, using many technologies that are in Spring in its latest versions.
However, it is incorrect to say that Java EE covers all the fields that Spring does. In my opinion Spring still has something that could make the difference, if you work in particular domains, such as mobile (Spring mobile), social (Spring social), navigation flows management (Spring Webflow) and others.

Migrating EJB2.1 Application [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a legacy financial application which is written using EJB2.1(Making use of Entity Beans, Stateless Session Beans and Couple of MDBs).
I want to migrate the application to latest Java EE or Spring framework. The application consists of around 400 entities and Entity beans are mainly used for Creating and Updating.
For the viewing part a separate DAO layer is there and I don't want to touch that part.Also I want to keep the business logic written in service beans as it is very complex to re write.
i.e., I simply want to replace the ORM part of the system. The application is making use of JTA transactions.
Sorry to ask a very high level question, but which technology I can use to replace the ORM.
Spring/Hibernate
Java EE
The primary considerations for the application would be scalability, performance also ease of deployment.
I just want opinions on who have used these technologies, I don't want to start a war between 'evangelists'.
If you find the input is not suffcient please ask me I can provide more details.
the argument here is really between EJB-3.x versus Spring/Hibernate the first caveat being that one does not necessarily mutually exclude the other. (annotations, container, testing, etc.)
there's lots of support in migrating to EJB 2.1 to EJB 3.x and lots of toolsets to assist. one of the principal challenges that i've seen with EJB is integration testing outside the container. (for example. in a continuous integration environment) There are JTA solutions, JNDI solutions and others to support but on the whole, i've found that there is more 'out-of-container' testing support on the Spring migration path than Java EE. that said, there are foundation frameworks such as Arquillian from JBoss designed to support this.
so i would suggest you look at EJB 2.1 to EJB 3 migration paths and then look at the Arquillian framework for integration testing support

Spring or CDI or EJB3 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am pretty new to the concept of dependency injection and we are currently writing an web app using JSF and maybe Primefaces. We are currently evaluating whether to use Spring or EJB3. I was realy close to choose Spring, but then I heard about CDI. Can you give us some hints, which could be the best for the following situation:
We are currently pretty new to the J2EE world and don't know if we will use Glassfish or JBoss (or can simply stick to Tomcat).
The web app is basically a prototype for an enterprise CRUD application that needs to be able to handle complex business logic. We want to focus on "adaptability", as some requirements are not clear and will be decided about a year later (when we know if we can still use the prototype).
We can't use Hibernate, as we will have to write pretty complex SQL Statements. Currently we made good experiences with the SQL abstraction in Spring.
Maybee I am currently comparing apples and oranges, but there are just too many information's, if you are new to j2ee. I think that EJB's are the standard defined through JCP, Spring is the standard defined by the market and CDI is a standard that is also defined by the JCP to do what Spring can do. But I am most certainly wrong ;-).
Thx,
iuiz
Lincoln Baxter does an excellent job explaining the technical differences in this article: http://ocpsoft.com/java/spring-to-java-ee-a-migration-guide-cdi-jsf-jpa-jta-ejb/ Long story short: both Spring and CDI will both be able to provide dependency injection. One is a Java EE standard, the other a commonly known technology. Glassfish and JBoss both run Spring apps and CDI apps without problem. As far as not being able to use Hibernate, it's not the case that you cannot use native SQL within Hibernate. Save your team a lot of extra dead-simple CRUD code if you can.

Resources