Sunday 23 June 2013

Struts2 sitemesh integration

SiteMesh is a Java web application development framework developed by OpenSymphony.

According to OpenSymphony, SiteMesh: Is a web-page layout and decoration framework and web application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required
Intercepts requests to any static or dynamically generated HTML page requested through the web server, parses the page, obtains properties and data from content, and generates appropriate final page with modifications to original—based on Decorator design pattern.


Can also include entire HTML pages as a Panel within another page—similar to Server Side Includes, except HTML document modified to create a visual window (using document's Meta-data as aid) within a page. Using this feature, Portal type web sites can be built very quickly and effectively. This is based upon the well-known Composite design pattern.

Built on Java 2 with Servlet, JSP and XML technologies. This makes it good for use with Java EE applications, however it can be integrated with non-Java server-side web architectures, like CGI (Perl/Python/C/C++/etc), PHP, and ColdFusion. Very extensible, designed for easy extension for custom needs

Apache Struts is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller (MVC) architecture. It was originally created by Craig McClanahan and donated to the Apache Foundation in May, 2000. Formerly located under the Apache Jakarta Project and known as Jakarta Struts.

Tag References :-These tags are used to create page Decorators. A Decorator is typically built up from an HTML layout with these tags inserted to provide place-holders for the data from the original (undecorated) page.

1.Decorator Head :Insert contents of original page's HTML head tag. The enclosing tag will not be be written.
2.Decorator Body :contents of original page's HTML body tag.
3.Decorator Title :Insert title of page being decorated. The value is obtained from title tag.

these three are widely use Click Here To Learn More

Create a web project and add this library download SiteMesh Jar:-



Add a filter mapping for sitemesh to web.xml file like this:-

web.xml

Sitemesh


struts2
org.apache.struts2.dispatcher.FilterDispatcher



sitemesh
com.opensymphony.module.sitemesh.filter.PageFilter




sitemesh
/*
REQUEST
FORWARD



struts2
/*
REQUEST
FORWARD



index.jsp




struts.xml






/dashboard.jsp






SiteMesh out of the box uses sitemesh.xml which contains mappers to how decorators are applied.
sitemesh.xml


 





    
decorators.xml

   
/nodecorate/*   
*.css   
/scripts/*   
/images/*   
/*.htm   
/*.html  
   
   
/*  
 
   
Stop pages from being decorated by adding them to the excludes list superceeds. This is because when pages were mapped to a non-existent decorator they were still buffered internally by Sitemesh. By using the exclude list Sitemesh will not let the request pass straight through to the servlet container without any buffering. Action Class
package com.javastoreroom.action; 
import com.opensymphony.xwork2.ActionSupport; 
public class HomeAction extends ActionSupport 
{    
private static final long serialVersionUID = 1L;   
public String home() 
{   
System.out.println("Check Here -----------> :");   
return SUCCESS;  
} 
}  
index.jsp
<%response.sendRedirect("home.html");%>
dashboard.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>

 
 
 
Insert title here 
 
 

Username :-

Password :-
Set Decorator Template By this page frontlayout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %> 
 
 
 
 
 

<%@include file="../jsp/homeheader.jsp"%> 
<%@include file="../jsp/homefooter.jsp"%>
homeheader.jsp
Home | Menu | Profile
homefooter.jsp
@copyright 2013 javastoreroom.blogspot.com
finally you when executing project following home page pattern are shown :-
Feature 1. Built with Java, the industry standard for enterprise applications.
2. Open source software distributed under the OpenSymphony Software.
3. Simple Eliminates the need to create multiple web-apps for different audiences.Web pages maintaining a consistent look-and-feel across your web-application.
4. Very flexible Map a decorator based on a cookie value.
5. Integrated Supports Velocity and Freemarker & other technologies (CGI, Perl, MS ASP... anything that renders html) for writing decorators.

2 comments: