You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by ta...@jakarta.apache.org on 2005/03/08 16:08:41 UTC

[Jakarta Tapestry Wiki] New: Tapestry31Spring

   Date: 2005-03-08T07:08:41
   Editor: DannyMandel
   Wiki: Jakarta Tapestry Wiki
   Page: Tapestry31Spring
   URL: http://wiki.apache.org/jakarta-tapestry/Tapestry31Spring

   no comment

New Page:

= Combining Tapestry3.1, Hivemind1.1 and Spring =
Many people ask how to combine above solution, I'm also curious about that and spend one-and-a-half-day to figure out the best way to do that. Reasons behind that are:
  1. Tapestry is good framework for web UI.
  2. Spring is a good container framework. Spring is also a good example of software project development.
  3. Hivemind for me, is not mature enough (and proved when I found bug while I do this project). Hivemind has good future, but I doubt it'll 100% replace Spring. Both of them have their own advantage.

For release version, I like combination of Tapestry+Spring+Hibernate, but they are all growing very fast and I really like to try milestone version of Tapestry31+Spring+Hibernate30+(Hivemind11).  I'll omit Hibernate in my explanation since the hard part is between Tapestry and Spring. One Spring is correctly configured, Hibernate solution is very pluggable.

Magic reference for Tapestry and Spring combination is in Spring Reference.  We still can use that solution but we will lost the pretty combination offered by new Tapestry+Hivemind. Another disadvantage is the solution extend BaseEngine which will deprecated in Tapestry 3.1.

[I assume basic knowledge of Java, Tapestry, and Spring]
== Hivemind Configuration ==

Hivemind is related intimately with Tapestry, even HLS has provide some way to easily play with Hivemind in Tapestry. Like Spring, Hivemind is working by special XML metadata. Hivemind also has its own metadata, and so Tapestry.  If we want to contribute to the whole Hivemind registry, the easiest way is by creating hivemodule.xml in your WEB-INF directory. Here is what you need in this project:
{{{
<?xml version="1.0"?>

<module id="testing" version="1.0.0">
	<service-point id="TestingDefaultSpringBeanFactoryHolder" interface="org.apache.hivemind.lib.SpringBeanFactoryHolder">
 		<invoke-factory>
 			<construct class="id.co.nincec.dymension.TestingSpringBeanFactoryHolderImpl">
 				<set-object property="context" value="service:tapestry.globals.ServletContext" />
 			</construct>
 		</invoke-factory>
 	</service-point>
	<service-point id="TestingDefaultSpringBeanFactory" interface="org.springframework.beans.factory.BeanFactory">
 		<invoke-factory service-id="hivemind.lib.ServicePropertyFactory">
 			<construct service-id="testing.TestingDefaultSpringBeanFactoryHolder" property="beanFactory" />
 		</invoke-factory>
 	</service-point>
	<contribution configuration-id="hivemind.ApplicationDefaults">
 		<default symbol="hivemind.lib.spring-bean-factory" value="service:testing.TestingDefaultSpringBeanFactory" />
 	</contribution>
<module>
}}}

First, look at the third section (contribution section). Here, we define a service to override default service in Hivemind. We need to override it since we will work in web application only and we doesn't want any dependancy with class org.apache.tapestry.engine.BaseEngine (like I say, the class will be deprecated in Tapestry3.1)

First and second service point is a definition about Spring BeanFactory which will be used by Hivemind to see Spring's managed bean. Next job is to create TestingSpringBeanFactoryHolderImpl.java. It look like this:

{{{
/**
 * 
 */
package id.co.nincec.dymension;

import javax.servlet.ServletContext;

import org.apache.hivemind.lib.impl.SpringBeanFactoryHolderImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author enefem
 * 
 */
public class TestingSpringBeanFactoryHolderImpl extends
    SpringBeanFactoryHolderImpl {

  private ServletContext context;

  public void setContext(ServletContext context) {
    this.context = context;
  }

  public ServletContext getContext() {
    return context;
  }

  public BeanFactory getBeanFactory() {
    if (super.getBeanFactory() == null) {
      super.setBeanFactory(WebApplicationContextUtils.getWebApplicationContext(getContext()));
    }
    return super.getBeanFactory();
  }
}
}}}
As we can see, this class is extend default SpringBeanFactoryHolder. The important thing is what you see in getBeanFactory() method, there you define where our BeanFactory located.

== Spring Configuration ==
Spring in servlet mode need two things, it needs the XML file for bean configuration and also a filter in web.xml. In web.xml, you will have to add this:
{{{
<context-param>
 	<param-name>contextConfigLocation</param-name>
 	<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
	<listener-class>
 		org.springframework.web.context.ContextLoaderListener
 	</listener-class>
</listener>
}}}

See, how easy to take Spring's bean managed into Tapestry page!! And so natural!!

The page template is trivial, one super-simple-example is like this (Home.html).
{{{
<span jwcid="@Insert" value="ognl:inuyasha" /> 
}}}

And we finish!!!

I wait for comment, suggestion, and any other things related to this simple tutorial.
----
''This information was originally written in an email to the Tapestry User's List on March 7, 2005 by Nanda Firausi''

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org