how to package backing bean controller manager facade business logic - jsf

I have looked at several JavaEE 6 login tutorials using servlet 3.0 and JSFtechnology. Often it shows a request scoped credentials bean and a user manager session bean. Most do not provide packaging info or for simplicity sake create everything in one package. I have been struggling with the packaging between the web tier and the business logic. I do not know if backing bean, controller, manager, and facade are all talking about the same thing or not. A short answer could tell me how to package a user manager session bean and the credentials bean, but a more appreciated answer would help me navigate the web tier and the business logic. Thanks in advance.

For packaging I like to break first by functionality (like core, gui) and then by business unit level.
for e.g
com.comp.db.beans // place your database beans here (if using any orm )
com.comp.web.ui.controller // place your controller, managedbeans here, this can be again broken into functionality wise like login, processing e.t.c.
com.comp.web.ui.beans //you place your vo here
In order to start packaging you have to first write down the different functionality of your system.
Then break them into business unit wise
Then break those into more finer level, to distinguish if functionality is going to be very specific to ui, or does it belong to core.

Related

Where to use FacesContext object? [duplicate]

I am not sure whether my approach with the MVC environment in JSF is the best way to go. Since I am trying to get the most out of JSF I would like to know how my Service Layer (or Model, speaking in MVC terms) should be 'designed'.
I know the View-Controller ratio should be 1 to 1 (exceptions ruled out).
Now in what way should I design my Service Layer? Should I use one big service (don't think so)? If not, based on what should I split my services?
Note, my Service will be called from the Beans (Controllers in MVC terms) and the Service itself will call DAO's using JPA when necessary.
Thanks in advance
The service layer (the business model) should be designed around the main entity (the data model). E.g. UserService for User, ProductService for Product, OrderService for Order, etc. You should absolutely not have one huge service class or so. That's extreme tight coupling.
As to the service layer API itself, Java EE 6 offers EJB 3.1 as service layer API. In the dark J2EE ages, long time ago when EJB 2.0 was terrible to develop with, Spring was more often been used as service layer API. Some still use it nowadays, but since Java EE 6 has incorporated all the nice lessons learnt from Spring, it has become superfluous. Note that EJB (and JPA) is not available in barebones servletcontainers such as Tomcat. You'd need to install for example OpenEJB on top of it (or just upgrade to TomEE).
Regardless of the service layer API choice, best would be to keep your JSF backing bean (action)listener methods as slick as possible by performing the business job entirely in the service layer. Note that the service layer should by itself not have any JSF dependencies. So any (in)direct imports of javax.faces.* in the service layer code indicates bad design. You should keep the particular code lines in the backing bean (it's usually code which adds a faces message depending on the service call result). This way the service layer is reuseable for other front ends, such as JAX-RS or even plain servlets.
You should understand that the main advantage of the service layer in a Java EE application is the availability of container managed transactions. One service method call on a #Stateless EJB counts effectively as a single DB transaction. So if an exception occurs during one of any DAO operations using #PersistenceContext EntityManager which is invoked by the service method call, then a complete rollback will be triggered. This way you end up with a clean DB state instead of a dirty DB state because for example the first DB manipulation query succeeded, but the second not.
See also:
Creating master-detail pages for entities, how to link them and which bean scope to choose
When is it necessary or convenient to use Spring or EJB3 or all of them together?
JSF Controller, Service and DAO
The 1:1 ratio between services and model entities maybe not bad if you have few entities in your app. But if it is a big app, there would be too much services.
The number of services depends upon the use cases of the app you are designing. Once you have identified them in the analysis phase, you must group them in several groups according to their functionality. Each group of use cases will be a Service, and each use case will be a method in that service. Each Service can manage several model entities (and you have to inject in it the DAOs it needs to perform its functionality). Usually the uses cases of a Service manage model entities inter-realationated in the class diagram of the model. The Services might follow the good practice of "max cohesion / min coupling".
The ratio between DAOs and model entities is 1:1. Each DAO perform CRUD operations and queries of its entity. If a method needs to query 2 relationated entities, put it in the more suitable DAO depending on the business concepts.
In the JSF presentation layer I neither have a 1:1 ratio between pages and controllers, that would be too much controllers. I group into one contrller all the pages needed to perform the use cases of each service. So the ratio 1:1 is between controllers and services, injecting each service in the controller whose pages perform its use cases.
Of course, these are general principles. You may have some particular cases in the app that broke them, but they are few.
You might not have too much services and controllers, but not too few neither because then they would have too much logic and fields. You must acchieve a compromise.

JSF's javax.faces.ViewState antipatterns

This question probably does not fit SO rules, but I'll ask it anyway. Answers might help other people struggling with JSF.
We are using JSF (MyFaces, PrettyFaces, PrimeFaces and Spring) on one of our biggest project for two years now (migrated from Tapestry 3). I can say that we are "recovering" from this migration to this day.
In my opinion one of our major mistakes was misunderstanding of JSF's VIEW SCOPE. JSF offers two basic mechanisms how you can persist VIEW STATE - CLIENT and SERVER. We went for the SERVER method, which was our first mistake as ViewExpiredException never stopped coming from that moment on. The next mistake was to store data on VIEW SCOPE as that prevented us from easily switching to CLIENT state saving method.
So I was thinking whether there are some best practices and guidelines on what should and shouldn't be stored on VIEW SCOPE (and thus serialized into VIEW STATE). Official documentation and specification does not provide that. But I came to a quite nice conclusion:
You should only store on VIEW SCOPE such information that you would normally (without JSF) pass as request parameters.
When you have a basic CRUD application without JSF, you would do this:
state of the form is preserved between requests by form values in POST parameters
state of the list (filtering, sorting, paging) is preserved between requests by query parameters
Is my conclusion correct? Do you have any other guidelines on what to store and what to never store inside VIEW SCOPE? Do any component frameworks have such guidelines?
i use the following Guidelines:
avoid VIEW SCOPE as it is only available for Faces Manages Beans.
better: avoid the usage of Faces Managed Backing Beans. use CDI Managed Backing Beans to ensure portability. (sure, this is only possible if you have any cdi container available... in Java EE 6 and newer...)
avoid AJAX with JSF. (or only use it with caution for simple UIs...)

Java EE 6 app with OSGi

I had develop an application base on JSF, EJB and JPA. The JSF side with ManagedBeans and Facelets lies in the war-module and my EJBs are in ejb-module. It is not a big thing, let's say 20 pages, 15 ManagedBeans and few entities and EJBs. It's part of my bachelor thesis and one of the requirements says that I need to make this system modular and I should use for that OSGi technology.
So my knowledge about OSGi is almost none and after few hours of googling I didn't make any progress. So I'd like to ask you, how would you divide this app into modules?And can you point me to some tutorial or article where such a procedure is described?Because I didn't find anything that would fit my scenario.
It's app dedicated to managing student projects at university. So there are two roles, teacher and student. Both of them can create projects, teacher confirmes them, students them submits them.
Here is my project structure
I don't want you of course to give me a whole solution but I'll be glad if could give me something for the start.
Thanks a lot
In a nutshell, OSGI modules are provided as bundles, which can be started, stopped, modified without disturbing other parts of the application.
When you break your scenario into bundles you will have:
Service (EJB in your case)
ServiceLocator (which will be used by clients like your jsf in this case, this can be part of your client itself)
Client (JSF in your case)
i. Depending upon complexity of project, you may also break your web into separate bundles like view, dao e.t.c, but in your case I dont think this is required.
Depending upon common code that you may have in your application you may also have a common bundle.
A very good place to start with Java EE and OSGI is # Oracle Wiki

Java EE: separating presentation logic from business logic using beans

I've been developing my first Java EE app, which has a number of JPA entity classes, each of which have a corresponding EJB class for dealing with the business logic. What I've done is changed one of those beans from Stateless to SessionScoped, so I can use it to allow a user to work their way through a series of form fields, all of which are rendered on a JSF form.
However, I'm starting to think this is clearly wrong, as I'm doing things like implementing methods such as "submitStep1" and "goBackToStep2" in my EJB. These methods set indicators which are used by render elements of various tags on the JSF page, so they are clearly "presentation logic".
My question is, how should I re-structure my code? I'm thinking that I should just have one SessionScoped (or should that be stateful?) bean, which deals with the presentation logic of my JSF page, and is able to use all the other ejbs (and by extension my JPA classes). This bean would be in the presentation-tier level of my app, meaning my business logic tier wouldn't need any Session Scoped Session Beans.
Now this all makes sense to me, and is what I am probably going to do. However the reason for my question is that on my JSF xhtml pages I use JSF EL tags a lot to refer to EJB content. Are there any JPA-related pitfalls I need to watch out for when writing presentation tier classes?
I know my question is pretty vague, and not really related to a specific example. And although I've found quite a lot out about Stateful v Stateless beans on this and other sites I just want to know my intended structure is the best one.
Why don't you use backing beans for presentation purpose, as they are intended for it, and you can easyly configure its scope, and leave the EJBs to business tier?
When using entities directly on the presentation tier, you should be aware of the transaction scope, specially regarding lazy relationships. That is, normally it is used one transaction per request, what will mean that amongst different requests, the entities will become detached, so you will need to reatach them to be able to load lazy relationships. You could use a filter that does it automatically each request or handle it by hand. You could also keep the same transaction during different requests but a long transaction is normally not a good idea, specially if there are updates/creations in the DB during this transacion.

Caching instances in a Java EE web app

Consider the scenario of a typical webapp with JSFs on the front and EJB 3, with Hibernate as JPA provider, talking to backend database such as mysql, etc. The main user actions are login and mostly CRUD operations (minus any D(elete) operations). And the App Server is GlassFish of course.
Given this scenario, how and where all would one go about providing caching to improve performance? From what I have googled, I have seen that hibernate provides some sort of caching through different cache providers. Is there any sort of caching that can be provided for the jsf pages? How about session beans or entity beans on the ejb side of things?
Also, I just read about memcached and was wondering if this was something to consider?
This article on second level caching by Jacob Orshalick is worth a read.
Seam has a JSF tag <s:cache> that allows page-fragment caching. The caching chapter of the Seam Docs is also worth reading.

Resources