Understanding Java Web development and separating logical tiers - java

When developing Java EE applications how do I separate Business Logic so it can be reused?
I inherited an application that is mostly Model 1. Business logic is located in JSPs, Servlets and DAO code.
I want to separate the business logic but I am confused by all of the frameworks etc. that exist.
I am looking into Hibernate with JPA to handle all database persistence. Currently all SQL is hand coded and separate SQL is used for different RDBMS. My DAOs will call the code necessary for persistence.
I am thinking of using Struts for my web layer. The part I don't understand is the Business Logic.
I don't want my logic tied to the Web Layer because I want to reuse the logic in a Java SE application.
I thought about putting business logic in Entity classes but that seems like a bad idea.
Is there some technology or pattern that can be used as a guideline for creating reusable business logic?
If I am not clear I will edit.
Thank you.

To separate your frontend code (the view) from your business logic (controller) and your data (model) you can follow the MVC pattern.
You can have your controllers access other classes that contain the reusable business logic that will be used within your Java SE applications.
There are a lot of frameworks that help you to build web applications in this style like Grails (uses Groovy), Play or Roo. But because you said 'enterprise' you should have a look at the Spring framework and its MVC module. Spring offers good integration with Hibernate and allows you to follow the MVC pattern with your web applications.

I would say take it piece-meal. Solve the biggest problems first, which is in your case having business logic in the jsp pages. You can accomplish this using any web MVC framework of your choice (Struts, Spring MVC, Grails are all good. Pick one that you are most comfortable with).
The next problem is organizing your business logic in a separate Model layer that your controllers can invoke. Spring is a good DI framework for organizing and bootstrapping your application. Also, Spring supports a number of web MVC frameworks including Struts, JSF etc.
The last problem is your Dao layer. You mentioned you want to use Hibernate/JPA. I dont know how familiar you are with Hibernate, but make sure that you are trying to solve an actual problem by switching to Hibernate (since switching to Hibernate usually comes at a significant cost and headaches).

Related

Java Spring Boot Hibernate, JPA, MVC, REST confusion

I'm learning Java SE and Spring Boot for a half year now, and watched different courses, and they teaching different ways, and I'm just confused which one does what?
In one course, we're using Eclipse, Spring MVC and Hibernate with MySQL, and writing everything like Servlet, Hibernate configure file, factory, session, and it's just a bit complicated how to do a query for example. In the other course, we're using Spring Initializr, Maven, REST API with PostgreSQL, and it's so much easier, we implementing CRUD repository, and just one line, we can do the query.
And I'm lost at this point. These what I just mention, what exactly we use them for? Why we don't use the simple way in the first course? What we done in the second that I don't have to create a factory and a session to do a simple query?
Is there any post, video or anything about this, for me to understand it?
There are always different ways to solve the same problem. Spring Boot offers you a lot of features to simplify your development. But you don't have to use them. You can always try to implement stuff by yourself. But most of the time, the built in features like the CRUD repository are sufficient to solve your problem.
I can't tell you the exact reasons, why the author of the first course did it this way. Maybe he or she wanted to show the principles, that are hidden beneath the features. Maybe it is just an older course or it is for Spring and not Spring Boot. Spring Boot simplified the setup for Spring and made many advancements.
Spring Framework has been there for a really long time, the ways you have seen are both valid ways, and as far as I understood by your statements is, one way is working with Spring MVC and the second is working with spring boot and spring boot makes things really easy.
You need to understand the difference between the spring MVC framework and spring-boot.
In spring MVC Framework we manage things with configuration files, like XML files and we also fire queries by opening a session first, and then only we can query. But in Spring-boot these things happens behind the scene and that is why it becomes so easy to work with spring-boot but anyhow we still need to understand all this stuff to be able to work properly with this framework.
Spring MVC is a complete HTTP-oriented MVC framework managed by the Spring Framework and based in Servlets. It would be equivalent to JSF in the JavaEE stack. The most popular elements in it are classes annotated with #Controller, where you implement methods you can access using different HTTP requests. It has an equivalent #RestController to implement REST-based APIs.
Spring boot is a utility for setting up applications quickly, offering an out-of-the-box configuration in order to build Spring-powered applications. As you may know, Spring integrates a wide range of different modules under its umbrella, as spring-core, spring-data, spring-web (which includes Spring MVC, by the way), and so on. With this tool, you can tell Spring how many of them to use and you'll get a fast setup for them (you are allowed to change it by yourself later on).
Spring boot is just an auto-configuration tool. Spring MVC is a web framework
Spring boot = Spring MVC + Auto Configuration(Don't need to write xml file for configurations) + Server(You can have embedded server).
Java EE, Spring and Springboot are not the same.
Spring is based on Java EE.
Spring boot is an 'extension' of Spring, especially with auto-configuration.
There are multiple frameworks or libraries which comes with its own advantage and disadvantages, however you need to choose the TechStack that suits your particular application's requirements.
So if you need to build a web app you can use Java Servlet ,but you have to handles multiple concerns yourself and it involves lot of configuration , but there are many frameworks like Spring,Struts,etc which makes the take easy
Similar way you can manually manage dependencies or you can use Maven or gradle to handle the dependencies and building process
Similar way if you need to connect to a Database you can directly use JDBC but there ar multiple ORM(Object Relational Mappers) available which will make the task easier like Hibernate, Jooq, etc
Regarding your question there is Spring framework and also SpringBoot , main motto of Springboot is that it prefers "conventions over configuration" meaning you only need to write very little code to get started and it comes with many starter-packs which basically are pretty much preconfigured, so you can build application easily
Different frameworks and libraries comes with its own learning curve but they reduce the time required for configuration and troubleshooting
Read about Spring MVC and Spring-boot framework. What you have mentioned is first is spring mvc and other is spring boot framework. make you understanding with the questions like, what problem spring boot solves ?? that was or is there in spring mvc.
JPA: The Java Persistence API (JPA) is one possible approach to ORM. Via JPA the developer can map, store, update and retrieve data from relational databases to Java objects and vice versa.
Hibernate: Hibernate is an open-source object-relational mapping(ORM) tool for Java. It provides a framework for mapping an object-oriented domain model to a traditional relational database.
MVC: The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller.
JAVA: One of the most widely used programming languages, Java is used as the server-side language for most back-end development projects, including those involving big data and Android development.
Springboot: Spring Boot is an open-source micro-framework. Spring Boot helps developers create applications that just run. Springboot is a JAVA framework.
REST: Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services.

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.

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.

Best view layer with simple URLs for an EJB3 application

I would like to get your input on what would be the most fitting view layer for EJB 3.0 [1] Java application for me.
JSF is no good because it is a mess when it comes to web URIs. I would like a view framework which would help with automating html form submission and validation while using clean URIs like example.com/story/1 or example.com/?story=1 (using GET method).
My first guess was to go with Spring MVC. Spring is great but it feels like an overkill since JPA plus EJB already does the heavy lifting. I only need couple of things, well, four of them actually:
JPA, EJB 3.0 for business layer
Clean URIs: example.com/story/1 preferably
HTML form helpers (validation, converters, etc)
Templating similar to Apache Tiles or JSF's ui:composition
So, which one would you pick?
Ad [1]: It's used mainly for JPA and stateless/stateful local session beans
My first guess was to go with Spring MVC. Spring is great but it feels like an overkill since JPA plus EJB already does the heavy lifting.
I disagree with this statement. Spring need not be an "all or none" proposition. I see nothing wrong with using Spring web MVC if that's all you need.
I'd put your EJBs and JPA behind Spring service interfaces and let the web tier deal with those, using the usual Spring idiom.
Apache Wicket supports RESTful URL:s out of the box using a combination of BookmarkablePageLinks and PageParameters constructor of the WebPage object. Wicket does also support Velocity templates and since it's entirely OO/POJO based it's easy to maintain in general.
Wicket fits into the Model and View parts of the MVC Model 2 and contains some samples of generic validators and specified form validators and as such it has sufficient mechanisms for supporting such features.

Add framework to java project

I've been working on a java web project.
Currently this project doesnt use any framework. Its a standard MVC application, using servlets and DAO with jdbc access to database (all queries are handwritten).
The project has a good code (all project developed using TDD), but its way too slow to add any feature, since all have to be done by hand.
In this case, which framework would you suggest to add this project?
I can't use a framework that requires me to rewrite all current code base to fit in this framework.
I think that Hibernate is a great choice for persistence.
But what else? Spring? VRaptor? Struts?
You might add interfaces for your classes, use Hibernate for the persistence layer replacing your DaoSqlImplementation by DaoHibernateImplementation one per time. As long as you wire your application with interfaces you won't have any problem.
Also I recommend you to use Spring, this way you can switch between implementations declaratively by just modifying the XML. One of the principles Spring follows is IoC (Inversion of Control). In this case means your application controls the framework and not the framework controls the application which is exactly what you requested.
One important thing is that you must justify every framework you decide to add to the application and not just add it because it is very cool.

Categories