Tuesday, March 23, 2010

Spring EYE on JAVA EE – Cond.
JSF Integration and Ajax Support

Spring EYE on JAVA EE – Cond. JSF Integration and Ajax Support


JSF provides a different component based MVC framework. Not only it provides event handling concept, but it has build-in support for Ajax as well. The easy integration with Spring gives it a significant lift in terms of the web tier implementation.


First step would be to include necessary jars as well as reconfigure web.xml so certain URL patterns can be parsed properly by jsf FacesServlet. Following code indicate what need to be added in web.xml to properly handle all *.jsf request.


web.xml




The intended new functionalities for the demo will be to update product. In order to demonstrate the rich Ajax support that jsf has, the product id is set to automatically submit Ajax calls to the backend to get the other product info to be displayed on the page. By taking advantage of JSF Ajax support, this means only one additional code to add the Ajax tag to response to the default “valueChange” event.


product.xhtml




The last line of the <h:outputText> is for the validation error message.

Ajax backing bean follows the flat bean mechanism so it can too take the advantage of the uniform DAO access. In addition, UI related methods are added to handle JSF UI event. One important member, uiId, is added to identify if server call to update product info is necessary by comparing it with the product real id. This also serves as the entry point to validate the user input id. For some reason, the Ajax tag does not work very well with the standard validation so here I found alternative.


JSF back bean: ProductBean.java




Method detail is intentionally omit.



In theory, flat bean does not likely completely reflect the domain entity, therefore, should not be used directly to update domain entity.


One example of this would be the FlatOrderBean that has a member field in String represents a concatenation of all the product names. To regenerate the products for the original order from this member field would be not only difficulty, but against the data integrity as well. Therefore, this reverse mapping has to be stopped.


However, for other flat beans that are inherently similar to the domain entity, share the same member fields, it makes sense to modify the domain object by providing just UI flat bean.


Product and ProductBean is a perfect pair.


In order to introduce this new functionality, in I_FlatBean interface new method reCreateBaseObject() is added to reversely convert flat bean to domain object. For the case this reverse mapping is not appropriate, e.g. FlatOrderBean, exception would be thrown claiming there is not enough information to recreate Order object; while for the other situation, it is sufficient to update the domain entity, the Product.


I_FlatBean




FlatOrderBean.java




ProductBean.java @ManageBean indicates that JSF framework will handle the life circle of this bean object.




DAO need to be adjusted as well.


HibernateEntityDAO.java




JSF provides convenience but challenge as well. One challenge is how to integrate JSF UI with Spring managed services. Since JSF back beans are managed by JSF framework (notice the @ManagedBean annotation in ProductBean class? This indicates that JSF framework will handle the life circle of this bean object), spring’s characteristic DI will not take place. Therefore, it is difficult to inject services as references into JSF back beans.


In this sample, we create a central place, which is a singleton factory, that holds references to all the web tier services, and provide direct access to those references by expose them through their name. As a result, JSF back beans can find those services by their name. Since this central factory is a POJO, it can take advantage of the Spring DI.


WebTierServiceFactory.java @Service tells Spring framework this is a service that needs to be loaded. @PostConstruct will register the services to the singleton factory.




To access this services is straightforward:


FlatOrderBean.java See how within Save() the reference to the service object is obtained.




Other approach like using JNDI is possible too but not discussed here.


In summary, this example successfully integrated JSF UI component with the Spring managed services without break any Spring in-house MVC component.



Previous: View & Flat Bean    Next: Service Pooling

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home