# ZK/How-Tos/Integrate-Other-Frameworks
## Integration with Other Frameworks
### Spring

1. Add Spring framework jar and all its dependencies to the classpath of your server. (See documentation at http://www.springframework.org for details.)
2. Add the following to your web.xml
3. Put the following applicationContext.xml file into your WEB-INF directory:
4. Example of test.BusinessServiceImpl and test.BusinessService:
5. Compile and deploy.
6. And finally a ZUL file example which calls methods of Spring-managed BusinessService:
where the forEach loop is looping over the collection returned by a call to the spring bean method and the ${each} attribute causes toString to be called on each object in the collection. You can also use zscript to directly work with your spring bean. This code does the same as the above
If you choose to use the MVC pattern (see Composer and GenericAutowireComposer examples in this wiki) then you can have Spring create your model and controller beans. Consider the following applicationContext.xml:
Here we tell spring to create a single com.acme.MyModelImpl object for a given user HttpSession but to create a new com.acme.MyControllerImpl bean for every application look-up. Each "myController" bean is created using a constructor that is passed the "myModel" bean scoped to the HttpSession. We need to activate the RequestContextListener in the web.xml for session scoping to work:
Now within any given page we can cause ZK to ask Spring for a new myController bean to use as the controller backing a given window object:
The above code uses the DelegatingVariableResolver. It resolves the myController reference in the "apply" attribute via spring (see the MVC and 'apply' attribute documentation). Spring creates a new object on demand wired to the session scoped myModel bean. The "forward" attribute will delegate the textbox onChange event to the mainWindow's custom "onUserEntersDiscountCouponNumber" event handler. There is no such event handler on the Window component class however the object specified in the "apply" attribute can add one to the window object that it is applied to. The simplest way to do this is for the com.acme.MyControllerImpl controller class to subclass GenericAutowireComposer and provide an event handler "public void onUserEntersDiscountCouponNumber(Event event) throws Exception". The logic in the superclass will then automatically add a event handler to the mainWindow object that calls the method you provide. See the MVC examples her on this wiki for further details and the MVC SmallTalks published at http://www.zkoss.org

### Struts + Tiles + JSP (+ Spring)

N.B. A lighter approach than what follows here could be to turn on the ZKFilter and have it post process the URLs of your actions. That works only if you JSPs output pure XHTML (use jTidy -asxml to convert html to xhtml then put CDATA sections around any script source code within the page).
N.B. With ZK3.0 consider using the ZK custom tag lib that lets you write ZK components into the page using a tag lib.
ZK provides a very rich set of features. You can use it to build an entire single page Ajax driven rich web application without using any web programming at all. Many companies however have significant investments in existing J2EE web technologies like Struts, Tiles and JSP. These companies may wish to leverage their existing investments by starting out with ZK XUL within a central tile on those pages that would benefit from XUL components. This section outlines one way of doing this. You need to be familiar with the support for Struts within Spring to be able to follow this example.
As this How-To covers ZK, Struts and Spring you need to specify them in your web.xml (please note that the zkFilter is not loaded as Tiles will cause zkLoader to render pure zul):
Create your struts configuration file struts-config.xml
Then in your Spring configuration file applicationContext.xml
Note that in the Struts configuration we have two action mappings for "success" or "errors". Success is mapped to search-results-tile.jsp which contains
This basically says that this page looks like site-layout but with search-results.zul in the middle of the page. This is because in site-layout.jsp there is:
With that information we see that in the specific search-results-tile.jsp we stated that it is based on site-layout but with search-results.zul in the body-content position.
Next we need to ensure that the Struts Action is passing results via the HTTP Session to search-results.zul. To do this the Struts Action can do something like:
Note that this.searchDelegate will be set by Spring as defined within the applicationContext.xml
So we now know that our Struts class will put search results list the Http Session where our search-results.zul can locate it:
You should be aware however that letting Struts do the search for us our server is doing a lot more stressful page rebuild work than is necessary. A more AJAX centric approach is to write a single ZUL page that first presents the user with a search input form. Then in zscript event handlers run the search and update the ZK destop with the search results. Only the html for the search results will then be sent back to the browser on the AJAX response. An pattern to achieve this is to move the code that was originally within a subclass of Struts Action into a subclass of org.zkoss.zul.Window then render the search results within the zscript event handler:
Note - using Tiles the ZUL page might note notice that it is being included and it might write <html> and <head> tags into the page. To avoid this rather than having tiles include a raw zul page include a jsp page that simply JSP includes the zul page which will behave well.

### JasperReport

see this Small Talk for an introduction.

### Hibernate

ZK Hibernate support without Spring: Hibernate Session handling. Preliminary!
The ZK team has provided since version 2.1.1 a set of classes to support the Hibernate without Spring case. The details is explained by Henri Chen in his Hibernate + ZK article.

### Acegi Security for Spring

see this Small Talk for an introduction
There is also another small talk written by Hari Gangadharan - Making Acegi work with ZK

### ZK 2.x.x + Spring 2.x.x + Hibernate 3.x + JUnit Test

by Marcos de Sousa (Maputo - Mozambique)
01/06/2007
The motivation behind this entry is to provide a simple step-by-step guide for integrate ZK Framework 2.x.x + Spring Framework 2.x.x + Hibernate 3.x. I will show you how things is done in real j2ee world application.
A. Initial Setup:
1. Download the ZK Framework JARs and add to your classpath: zk.jar, zul.jar, zhtml.jar, zkplus.jar, zweb.jar, zcommon.jar, bsh.jar, jcommon.jar, commons-el.jar, commons-fileupload.jar, commons-io.jar, Filters.jar
2. Download Spring Framework and add to your classpath: spring.jar, spring-mock.jar
3. Download Hibernate 3.x.x and Hibernate Annotation 3.x.x then add to your classpath: antlr-2.x.x.jar, asm.jar, asm-attrs.jar, cglib-2.x.x.jar, commons-collections 2.x.x.jar, commons-logging-1.x.x.jar, dom4j-1.x.x.jar, ejb3-persistence.jar, hibernate3.jar, hibernate-annotations.jar, jdbc2_0-stdext.jar, jta.jar, xerces-2.x.x, xml-apis.jar
4. Add the JAR containing your database driver (I am using SQL Server sqljdbc.jar in the example, but only minor changes are necessary to adapt for another database).
5. Add the JAR junit-3.x.x.jar for Unit Test.
B. Code - Domain Model:
This example will be based on a purposefully simple domain model of just 2 classes. Notice the use of annotations. One may choose to use either annotations or an XML file to specify the object-relational mapping metadata - or even a combination of both approaches. Here, I have chosen to use solely annotations - for which brief descriptions will be provided immediately following the domain model code listings.
First, the Employee class:
Second, the Role class:
In the Employee class, there is a @ManyToOne annotation to describe the relationship to an Role. Look cascade rules. For example, when a Role is deleted, the associated Employees will also be deleted.
C. Code - Data Access Object Layer:
For accessing instances of the domain model, it is best to create a generic interface that hides all details about the underlying persistence mechanism. That way, if switching later to something other than Hibernate Annotation, there will be no impact on the architecture, with minor changes are necessary to adapt to migrate to JPA. This also makes it easier to test the service layer, since it enables the creation of stub implementations of this data access interface - or even dynamic mock implementations.
Here is the interface. Notice that there are no dependencies on any Hibernate Annotation or Spring classes. In fact, the only dependencies here that are not core Java classes are the classes of my domain model.
For the implementation of this interface, I am going to extend Spring’s HibernateDaoSupport class. This provides a convenience method for retrieving the HibernateTemplate. If you have used Spring with JDBC or other ORM technologies, then you will probably be quite familiar with this approach.
It should be noted that use of the HibernateDaoSupport is optional.
Here is the implementation:
D. The Service Layer:
The service layer play a critical role in the system architecture. It would be the point where transactions are demarcated - and typically, they would be demarcated declaratively in the Spring configuration. In the next step, when you have a look at the configuration, you will notice that I have provided a transactionManager bean. It would wrap service layer methods with transactions. The main point to take away is that there is NO transaction-related code in the data-access tier. Using the Spring HibernateTemplate ensures that the same EntityManager is shared across all DAOs. Therefore transaction propagation occurs automatically - as dictated by the service layer. In other words, it will actually behave exactly the same as other persistence mechanisms configured within the Spring framework.
Here is the interface and the implementation:
E. Configuration:
Since I opted for the annotation-based mappings, you have actually already seen most of the Hibernate Annotation-specific configuration when the domain classes were presented. As mentioned above, it also would have been possible to configure those mappings via XML. The only other required configuration is in META-INF/context.xml, the spring application context spring-sql.xml (must be in classpath or where packages start) and web.xml.
Here is the context.xml:
Here is the spring-sql.xml (must be in the classpath root):
Here is the web.xml:
F. Integration Testing:
Perhaps the best way to learn a new API is to write a bunch of testcases. The TestEmployeeManager class provides some basic tests. In order to learn more about Hibernate Annotation, modify the code and/or configuration and observe the impact on these tests.
Here is the TestManager class:
Here is the TestEmployeeManager class:
Here is the AllTests class:
Until here, if you run your Unit Test every thing will work.
// TODO: Tomorrow I will continue...
// TODO: Create Database, Table, Show Integration with ZK Framework as Front End, Conclusions and Tips and Tricks, etc.
// TODO: Maybe show Acegi Security? OSWORKFLOW? IBM AS400? Ant? Maven 2? SSL/HTTPS? CSS? Eclipse? in action.