This page describes how to hook sitetree into spring. The examples here will probably be included in the next release.
We have tested the examples that follow in Spring 1.2.8.
This section assumes that you are already familar with the basics of both Spring and Sitetree .
If you are not already familiar with Sitetree , you can start learning about it
here.
The first step is create a new class that extends Jade's
SitetreeFactory
and Spring's org.springframework.web.servlet.HandlerMapping
in order to return the appropriate Sitetree resource when it is required by Spring:
public class SitetreeMapping extends SitetreeFactory
implements HandlerMapping, InitializingBean, ApplicationContextAware {
private String defaultHandlerName;
private WebApplicationContext context;
public void setApplicationContext( ApplicationContext applicationContext ) {
context = (WebApplicationContext) applicationContext;
context.getServletContext().setAttribute( this.getServletContextName(), this );
}
public void afterPropertiesSet() {
init();
}
public HandlerExecutionChain getHandler( HttpServletRequest request ) {
List handlerInterceptors = null;
// Find a resource to look for
String resource = null;
try {
resource = this.getSitetree( request ).getResource( request );
} catch ( final InvalidPathException ex ) {
// handle exception
} catch ( final SecureRequestRequiredException ex ) {
// handle exception
}
// look for a bean of this name on the web application context
Object bean = null;
try {
bean = context.getBean( resource );
} catch ( NoSuchBeanDefinitionException ex ) {
// handle exception
}
// if bean is a PageController set getResource
if ( bean instanceof PageHandler ) {
((PageHandler)bean).setResource( resource );
}
return new HandlerExecutionChain( bean, new HandlerInterceptor[] { newTreeViewResolver() } );
}
public HandlerInterceptor newTreeViewResolver() {
return new HandlerInterceptorAdapter() {
public boolean preHandle( HttpServletRequest request,
HttpServletResponse response, Object handler ) {
try {
TreeView treeView = SitetreeMapping.this.getSitetree( request )
.resolve( request, response );
if ( treeView == null ) {
return false;
}
return true;
} catch ( InvalidPathException ex ) {
// handle exception
} catch ( IOException ex ) {
// handle exception
}
}
};
}
public void setDefaultHandlerName( String defaultHandlerName ) {
this.defaultHandlerName = defaultHandlerName;
}
public String getDefaultHandler( ) {
return defaultHandlerName;
}
}
The purpose of the SitetreeMapping class is to marry the concepts of Sitetree
and HandlerMapping together. It is used to find the resource defined in Sitetree 's
configuration that is associated with the request being handled. The resource that is found is then used to
find the bean that has been defined in Spring's configuration to handle the requested resource.
Here is the definition of PageHandler referenced above in SitetreeMapping :
public interface PageHandler {
public void setResource( String resource );
public String getResource( );
}
PageHandler is a marker interface that is used to indicate that the bean used to handle a
Sitetree resource. Here is a PageController that implements
PageHandler and uses the Sitetree resource that is specified to setup
a View that is used to handle the request:
public class JstlPageController implements Controller, PageHandler {
private String resource;
public void setResource( String resource ) {
this.resource = resource;
}
public String getResource( ) {
return resource;
}
public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response )
throws Exception {
JstlView view = new JstlView();
view.setUrl( resource );
return new ModelAndView( view, null );
}
}
Here are XML blocks that should appear in the spring-servlet.xml configuration file to
ensure that SitetreeMapping and PageHandler are created and initialized properly:
<bean id="sitetreeMappings" class="com.djinnsoft.jade.sitetree.spring.SitetreeMapping">
<property name="servletContextName" value="jadeDefaultSitetreeFactory"/>
<property name="noAccessLocation" value="/noAccess.html"/>
<property name="invalidUriLocation" value="/invalidUri.html"/>
<property name="sessionTimeoutLocation" value="/timeout.html"/>
<property name="defaultSitetreeSegmentNames" value="publicSegment"/>
<property name="defaultHandlerName" value="pageHandler"/>
</bean>
<!-- handles all JSP only requests
- the 'resource' property is set to the resource name from SitetreeMapping
- the resource name must be a URI
-->
<bean id="pageHandler" class="com.djinnsoft.jade.sitetree.spring.JstlPageController"/>
The first block creates the SitetreeMapping class and setup all the locations and names that
are name when setting up Sitetree .
The second block sets up the PageController that we are going to use to handle the
Sitetree resources.
In this example we have not included any spring controllers. The SitetreeMapping hands requests to and resource by name
- so this can just be any spring controller.
|