tags:

views:

1440

answers:

3

Hi friends,

I needed to create an application using Struts2 as MVC,Hibernate for data access and spring in the business logic. And also I needed to use Velocity for presentaion and sitemesh for templating.

Integrating Hibernate and Spring is done easily but integrating spring, sitemesh and velocity together with Struts2 is not clear for me but I can use velocity,spring and sitemsh individually with Struts2.

Of course as illustrated in this example http://www.rkcole.com/articles/struts/crudTutorial/step4.html sitemesh and spring can be integrated with struts2 configuring web.xml as

<listener> 
<listener-class> 
org.springframework.web.context.ContextLoaderListener 
</listener-class> 
</listener> 

<filter> 
<filter-name>sitemesh</filter-name> 
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class> 
</filter> 


<filter-mapping> 
<filter-name>sitemesh</filter-name> 
<url-pattern>/*</url-pattern> 
<dispatcher>FORWARD</dispatcher> 
</filter-mapping>

Now my task is to integrate velocity with this combination...............

Normally to integrate Velocity and struts2 I use the following

<servlet-class> 
org.apache.velocity.tools.view.servlet.VelocityViewServlet 
</servlet-class> 
<load-on-startup>10</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>velocity</servlet-name> 
<url-pattern>*.vm</url-pattern> 
</servlet-mapping>

.............................................................................................

Now my question is how to set `

 <servlet-mapping>

`, its only for velocity, or simemesh or has to be set differently

Please let me know how to proceed,if can please reply with complete web.xml and others steps to be followed.

Regards

T.Thamilvaanan

A: 

My question is about how to set

<servlet-mapping> and  <filter-mapping>

for the above mentioned purpose.

Please let me know how to proceed, if can please reply with complete web.xml and others steps to be followed.

I am trying with the answer given here http://stackoverflow.com/questions/619699/using-sitemesh-with-requestdispatchers-forward

But I still didn't get the right idea

T.Thamilvaanan
A: 

With this I got some ideas from http://struts.apache.org/2.0.6/docs/sitemesh-plugin.html

and also from a reply in the following link helped me to proceed http://www.coderanch.com/t/460289/Struts/Velocity-Struts-Sitemesh-Spring-Hibernate

Thanks

Regards Thamilvaanan

T.Thamilvaanan
+2  A: 

Ya, finally I got this web.xml after lots of reading and searching............

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;


<!-- A part in Spring Integration-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>



<!-- All the filters starts here-->

<filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>org.apache.struts2.dispatcher.StrutsPrepareFilter</filter-class>
</filter>


<!-- This is used to integrate sitemesh with Struts2-->

<!-- 
I am using Velocity to create sitemesh decorators so I have to use 

  VelocityPageFilter    to integrate

  Sitemesh  (i.e. Sitemesh in velocity)  +  Struts2    
In the web.xml, the VelocityPageFilter should be placed between the
  ActionContextCleanUp (StrutsPrepareFilter since 2.1.3  )  and
and the    FilterDispatcher (StrutsExecuteFilter  since 2.1.3)  
 -->

<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.apache.struts2.sitemesh.VelocityPageFilter</filter-class>
</filter>

<filter>       
<filter-name>struts2</filter-name>   
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class> 
</filter> 

<filter-mapping>
<filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
 <filter-name>sitemesh</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>



<!-- Spring Integration-->
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



    <!--Finally since I am velocity pages in struts2 MVC I am using
 VelocityViewServlet        to Integrate struts2 with Velocity  -->

  <servlet>
  <servlet-name>velocity</servlet-name>
  <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet
  </servlet-class>
  <init-param>
    <param-name>org.apache.velocity.toolbox</param-name>
    <param-value>/WEB-INF/tools.xml</param-value>
  </init-param>
   <init-param>
    <param-name>org.apache.velocity.properties</param-name>
    <param-value>/WEB-INF/velocity.properties</param-value>
  </init-param>
  </servlet>


<!-- Map *.vm files to Velocity -->
<servlet-mapping>
  <servlet-name>velocity</servlet-name>
  <url-pattern>*.vm</url-pattern>
</servlet-mapping>



    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>


    <welcome-file-list>
        <welcome-file>index.vm</welcome-file>
    </welcome-file-list>


   </web-app>

Hope this is fine,will test and let you know.

Cheers............

Regards

Thamilvaanan

T.Thamilvaanan